requestsで保持したセッションをseleniumに渡してログイン状態を保持したい。
pinterestというサイトにrequestsでpostログインし、その後ログインセッションseleniumに渡し操作したいのですが上手く行きません。
以下のコードを実行して見るのですが、エラーが出ず固まって動かなくなってしまいます。
詳しい方見て頂けないでしょうか?よろしくお願いします。
import requests
from selenium import webdriver
from bs4 import BeautifulSoup
#サーバの認識するブラウザ情報
ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
headers = {'User-Agent': ua}
##seleniumを使用する準備
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome('selenium/chromedriver', options=options)
login_url = 'https://accounts.pinterest.com/v3/login/handshake/'
userdata = {'username_or_email':'xxxxxxxxxxx', 'password':'yyyyyyyyyyyyy'}
authorization_url = 'https://api.pinterest.com/oauth/'
payload = '?response_type=code&client_id=xxxxxxxxxxxx&redirect_uri=https://x.x.x.x:9000/callbackpage&scope=read_public,write_public,read_relationships,write_relationships'
def get_code():
with requests.Session() as s:
p = s.post(login_url, params=userdata)
driver.delete_all_cookies()
driver.get('https://www.pinterest.jp/')
[s.cookies.set(c['name'], c['value']) for c in driver.get_cookies()]
c_dict = s.cookies.get_dict()
cookie = [{'name': name, 'value': value} for name, value in c_dict.items()]
[driver.add_cookie(c) for c in cookie]
print('ok')
#ログインが成功してるならここで承認が必要なページになる。
authorization_url_payload = authorization_url + payload
driver.get(authorization_url_payload)
soup = BeautifulSoup(driver.page_source, 'lxml')
print(soup.body)
get_code()
途中で止めた際に吐き出されたエラー
^C^CTraceback (most recent call last):
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/site-packages/urllib3/connectionpool.py", line 379, in _make_request
httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "getcode.py", line 76, in <module>
get_code()
File "getcode.py", line 43, in get_code
[driver.add_cookie(c) for c in cookie]
File "getcode.py", line 43, in <listcomp>
[driver.add_cookie(c) for c in cookie]
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 894, in add_cookie
self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
response = self.command_executor.execute(driver_command, params)
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
return self._request(command_info[0], url, body=data)
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 397, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/site-packages/urllib3/request.py", line 72, in request
**urlopen_kw)
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/site-packages/urllib3/request.py", line 150, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/site-packages/urllib3/poolmanager.py", line 326, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/site-packages/urllib3/connectionpool.py", line 603, in urlopen
chunked=chunked)
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/site-packages/urllib3/connectionpool.py", line 383, in _make_request
httplib_response = conn.getresponse()
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/http/client.py", line 1336, in getresponse
response.begin()
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/http/client.py", line 306, in begin
version, status, reason = self._read_status()
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/http/client.py", line 267, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/home/vagrant/anaconda3/envs/pinterest/lib/python3.7/socket.py", line 589, in readinto
return self._sock.recv_into(b)
KeyboardInterrupt