模拟登录(selenium & requests & pickle)
使用selenium打开chrome
打开网页
设置时间延迟
手动登录(解决验证码的问题)
获得cookies:传入requests直接使用 / pickle保存至本地
#!/usr/bin/python
import requests
import selenium
from selenium import webdriver
import time
import pickle
wd = webdriver.Chrome()
wd.get(“https://www.zhihu.com”)
time.sleep(45)
cookies = wd.get_cookies()
pickle.dump(cookies, open(“cookies.pkl”, “wb”)) # pickle save cookies
cookies = pickle.load(open(“cookies.pkl”, “rb”)) # pickle load cookies
s = requests.Session()
for cookie in cookies:
s.cookies.set(cookie[‘name’], cookie[‘value’])
s.headers.clear()
test = s.get(“https://www.zhihu.com”)
print test.text
作者:stanf1l
链接:https://www.jianshu.com/p/5e090c51eecc
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。