Python用selenium获取cookie以后给rqeuests使用。
生活随笔
收集整理的這篇文章主要介紹了
Python用selenium获取cookie以后给rqeuests使用。
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
先使用selenium的get_cookies()方法獲取cookie,再使用 requests.Session() 使用cookie
獲取
- 首先用selenium自帶的get_cookies()方法獲取cookie
代碼如下:
- 我用的Chrome,掃碼登錄百度貼吧
使用(Session.cookies.set(cookie[‘name’],cookie[‘value’]))
def get_html(self):se = requests.Session()with open("TieBaCookies3.txt", 'r', encoding='UTF-8') as f:listCookies = json.loads(f.read())for cookie in listCookies:se.cookies.set(cookie['name'], cookie['value'])url = 'http://www.tieba.com'html = se.post(url)print(html.text) # 可以看到用戶名變?yōu)槟阕约毫恕<礊槌晒Α?/span> if __name__ == '__main__':tieba = TieBa()if not os.path.exists('TieBaCookies3.txt'):tieba.main_html()tieba.get_cookie()tieba.get_html()else:tieba.get_html()結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的Python用selenium获取cookie以后给rqeuests使用。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决selenium用cookies时候
- 下一篇: python使用requests+xpa