今日头条cookie登录和豆瓣session维持会话

#1.利用selenium添加cookie登录头条
在这里插入图片描述

  1. 注意点就是添加cookie方法和格式,add_cookie,字典形式
  2. 注意selenium等待时间加载
from selenium import webdriver
import time
window=webdriver.Chrome()
window.get("https://www.toutiao.com/")
c1={u"name":u"uid_tt",u"value": u"1f46d8d4bfcbefe9bc65e11e68f7d548",u"domain":u".toutiao.com"}
c2={u"name":u"ccid",u"value": u"66c7871a52ca20abc48d678a36a501f2",u"domain":u".toutiao.com"}
c3={u"name":u"ckmts",u"value": u"PUJw85AQ,qrJw85AQ,L6Cw85AQ",u"domain":u".toutiao.com"}
c4={u"name":u"sid_tt",u"value":u"f89559a8c6f4507d2797ab84faedb6f3",u"domain":u".toutiao.com"}

window.add_cookie(c1)
window.add_cookie(c2)
window.add_cookie(c3)
window.add_cookie(c4)
window.get("https://www.toutiao.com/")
time.sleep(3)


# window.refresh()

word=window.find_element_by_xpath('//*[@id="rightModule"]/div[1]/div/div/div/input')
word.send_keys("soho")
time.sleep(2)
window.find_element_by_xpath('//*[@id="rightModule"]/div[1]/div/div/div/div/button/span').click()
time.sleep(5)
window.quit()
# html=window.page_source
# print(html)
#
# window.get_screenshot_as_file("a.png")
# page=window.page_source
# print(page)
# cookies = window.get_cookies()
# print(cookies)

#2.豆瓣session保持登录

  1. 我是把验证码下载到本地然后输入登录
  2. session直接改写requests.session()就行

# coding: utf-8


import  requests
import json

requests=requests.session()
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}
date={'source': 'index_nav',
'form_email': '[email protected]',
'form_password': 'xl111223',
'captcha-solution':input("请输入验证码:"), 
'captcha-id':cc }
j=requests.get('https://www.douban.com/j/misc/captcha')
aa=j.json()
cc=aa['token']
bb=requests.get('http:'+aa['url']).content
with open('F:\pythondoc\yanzheng.jpg','wb') as f:
    f.write(bb)
    
dd=requests.post('https://www.douban.com/accounts/login',data=date,headers=headers)
#print(dd.text)
ee=requests.get('https://www.douban.com/people/181807393/',headers=headers) 
print(ee.text)


猜你喜欢

转载自blog.csdn.net/weixin_42357472/article/details/82846983
今日推荐