requests 带上 cookies 请求

requests 带上 cookies 请求

写爬虫自然少不了登录,登录就可能会用到 cookies,下面讲下 requests 如何带 cookies 做请求。

cookies_file = 'cookies.json'

def save_cookies_file():
"""
手动登录保存cookies文件

:return:
"""

login_url = ''
driver = webdriver.Chrome()
try:
driver.get(login_url)

# 现在很多站点的登录很麻烦,那么就手动登录
time.sleep(30)

# 获取到cookies
cookies = driver.get_cookies()

json_cookies = json.dumps(cookies)
with open(cookies_file, 'w') as f:
f.write(json_cookies)
finally:
driver.close()


def generate_cookie_jar():
"""
生成cookie_jar

:return:
"""
# 获取cookies
with open(cookies_file, 'r', encoding='utf-8') as f:
cookies = json.loads(f.read())
if len(cookies) > 0:
# 设置cookies
jar = requests.cookies.RequestsCookieJar()
for c in cookies:
jar.set(c['name'], c['value'], path=c['path'], domain=c['domain'], secure=c['secure'], rest={'HttpOnly': c['httpOnly']})
return jar
else:
return None


url = ''
with requests.get(url, cookies=cookie_jar) as r:
pass

除了 requests,还有 selenium 也是可以带 cookies

参考

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×