我的服务器上有代码
#!/usr/bin/python
# -*- coding= utf-8 -*-
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from seleniumrequests import Firefox
from pyvirtualdisplay import Display
import requests
import browsercookie
display = Display(visible=0, size=(1024, 768))
display.start()
browser = webdriver.Firefox()
browser.get("https://www.youporn.com/login/")
time.sleep(10)
username = browser.find_element_by_id("login_username")
password = browser.find_element_by_id("login_password")
button = browser.find_element_by_class_name("btnLogin")
username.send_keys("phillipssadella")
password.send_keys("GmiN97RUC")
button.click()
time.sleep(10)
txt = open("driver.html", "w")
txt.write(browser.page_source)
cookies = browser.get_cookies()
print cookies
s = requests.Session()
cook = [s.cookies.set(c['name'], c['value']) for c in cookies]
print cook
r = requests.get("https://www.youporn.com/", cookies=cook)
txt = open("requests.html", "w")
txt.write(r.content)
browser.close()
display.stop()
我使用selenium登录并在调试(driver.html)中 - 我看到我在帐户内部。没关系。
现在我想将此会话转移到请求中。我希望请求使用已经授权的会话。
我从Firefox中提取Cookie并尝试将其推送到请求中。但我看到错误
Traceback (most recent call last):
File "test.py", line 36, in <module>
r = requests.get("https://www.youporn.com/", cookies=cook)
File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 494, in request
prep = self.prepare_request(req)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 415, in prepare_request
cookies = cookiejar_from_dict(cookies)
File "/usr/local/lib/python2.7/dist-packages/requests/cookies.py", line 518, in cookiejar_from_dict
cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
TypeError: object cannot be interpreted as an index
我试过
for cookie in cookies:
s.cookies.set(cookie['name'], cookie['value'])
print cookie
r = s.get("https://www.youporn.com/", cookies=cookies)
结果
{u'domain': u'.youporn.com', u'name': u'sid', u'value': u'6759444914875662336', u'expiry': 1924956113, u'path': u'/', u'httpOnly': False, u'secure': False}
{u'domain': u'.youporn.com', u'name': u'_gat', u'value': u'1', u'expiry': 1509245409, u'path': u'/', u'httpOnly': False, u'secure': False}
{u'domain': u'www.youporn.com', u'name': u'performance_timing', u'value': u'Other', u'expiry': None, u'path': u'/', u'httpOnly': False, u'secure': False}
{u'domain': u'.youporn.com', u'name': u'login', u'value': u'phillipssadella%7C60a49468b4179a3a048d0dd7b0b14441c87f4a3b%7C4c7fc449ca6db2a1b20ea1611669aa29', u'expiry': 1511837364, u'path': u'/', u'httpOnly': True, u'secure': True}
{u'domain': u'.youporn.com', u'name': u'username', u'value': u'phillipssadella', u'expiry': 1511837364, u'path': u'/', u'httpOnly': False, u'secure': True}
{u'domain': u'.youporn.com', u'name': u'_ga', u'value': u'GA1.2.1764019650.1509245349', u'expiry': 1572317365, u'path': u'/', u'httpOnly': False, u'secure': False}
{u'domain': u'.youporn.com', u'name': u'_gid', u'value': u'GA1.2.1990953556.1509245349', u'expiry': 1509331765, u'path': u'/', u'httpOnly': False, u'secure': False}
{u'domain': u'.youporn.com', u'name': u'yp-device', u'value': u'pc', u'expiry': 1509331769, u'path': u'/', u'httpOnly': False, u'secure': False}
{u'domain': u'.youporn.com', u'name': u'country', u'value': u'IT', u'expiry': 1924956114, u'path': u'/', u'httpOnly': False, u'secure': False}
追溯
Traceback (most recent call last):
File "test.py", line 38, in <module>
r = s.get("https://www.youporn.com/", cookies=cookies)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 521, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 494, in request
prep = self.prepare_request(req)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 415, in prepare_request
cookies = cookiejar_from_dict(cookies)
File "/usr/local/lib/python2.7/dist-packages/requests/cookies.py", line 518, in cookiejar_from_dict
cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
TypeError: list indices must be integers, not dict
我该如何正确地做到这一点?