Python3爬蟲帶上cookie的實(shí)例代碼
Cookie的英文原意是“點(diǎn)心”,它是在客戶端訪問Web服務(wù)器時(shí),服務(wù)器在客戶端硬盤上存放的信息,好像是服務(wù)器發(fā)送給客戶的“點(diǎn)心”。服務(wù)器可以根據(jù)Cookie來跟蹤客戶狀態(tài),這對(duì)于需要區(qū)別客戶的場合(如電子商務(wù))特別有用。
當(dāng)客戶端首次請(qǐng)求訪問服務(wù)器時(shí),服務(wù)器先在客戶端存放包含該客戶的相關(guān)信息的Cookie,以后客戶端每次請(qǐng)求訪問服務(wù)器時(shí),都會(huì)在HTTP請(qǐng)求數(shù)據(jù)中包含Cookie,服務(wù)器解析HTTP請(qǐng)求中的Cookie,就能由此獲得關(guān)于客戶的相關(guān)信息。
下面我們就來看一下python3爬蟲帶上cookie的方法:
1、直接將Cookie寫在header頭部
# coding:utf-8 import requests from bs4 import BeautifulSoup cookie = '''cisession=19dfd70a27ec0eecf1fe3fc2e48b7f91c7c83c60;CNZZDATA1000201968=181584 6425-1478580135-https%253A%252F%252Fwww.baidu.com%252F%7C1483922031;Hm_lvt_f805f7762a9a2 37a0deac37015e9f6d9=1482722012,1483926313;Hm_lpvt_f805f7762a9a237a0deac37015e9f6d9=14839 26368''' header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Geck o) Chrome/53.0.2785.143 Safari/537.36', 'Connection': 'keep-alive', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cookie': cookie} url = 'http://www.dbjr.com.cn/article/191947.htm' wbdata = requests.get(url,headers=header).text soup = BeautifulSoup(wbdata,'lxml') print(soup)
2、使用requests插入Cookie
# coding:utf-8 import requests from bs4 import BeautifulSoup cookie = { "cisession":"19dfd70a27ec0eecf1fe3fc2e48b7f91c7c83c60", "CNZZDATA100020196":"1815846425-1478580135-https%253A%252F%252Fwww.baidu.com%252F%7C1483 922031", "Hm_lvt_f805f7762a9a237a0deac37015e9f6d9":"1482722012,1483926313", "Hm_lpvt_f805f7762a9a237a0deac37015e9f6d9":"1483926368" } url = 'http://www.dbjr.com.cn/article/191947.htm' wbdata = requests.get(url,cookies=cookie).text soup = BeautifulSoup(wbdata,'lxml') print(soup)
實(shí)例擴(kuò)展:
使用cookie登錄哈工大ACM站點(diǎn)
獲取站點(diǎn)登錄地址
http://acm.hit.edu.cn/hoj/system/login
查看要傳送的post數(shù)據(jù)
user和password
Code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __author__ = 'pi' __email__ = 'pipisorry@126.com' """ import urllib.request, urllib.parse, urllib.error import http.cookiejar LOGIN_URL = 'http://acm.hit.edu.cn/hoj/system/login' values = {'user': '******', 'password': '******'} # , 'submit' : 'Login' postdata = urllib.parse.urlencode(values).encode() user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36' headers = {'User-Agent': user_agent, 'Connection': 'keep-alive'} cookie_filename = 'cookie.txt' cookie = http.cookiejar.MozillaCookieJar(cookie_filename) handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) request = urllib.request.Request(LOGIN_URL, postdata, headers) try: response = opener.open(request) page = response.read().decode() # print(page) except urllib.error.URLError as e: print(e.code, ':', e.reason) cookie.save(ignore_discard=True, ignore_expires=True) # 保存cookie到cookie.txt中 print(cookie) for item in cookie: print('Name = ' + item.name) print('Value = ' + item.value) get_url = 'http://acm.hit.edu.cn/hoj/problem/solution/?problem=1' # 利用cookie請(qǐng)求訪問還有一個(gè)網(wǎng)址 get_request = urllib.request.Request(get_url, headers=headers) get_response = opener.open(get_request) print(get_response.read().decode()) # print('You have not solved this problem' in get_response.read().decode())
到此這篇關(guān)于Python3爬蟲帶上cookie的實(shí)例代碼的文章就介紹到這了,更多相關(guān)Python3爬蟲如何帶上cookie內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python 爬蟲如何正確的使用cookie
- Python爬蟲使用瀏覽器cookies:browsercookie過程解析
- Python3爬蟲之urllib攜帶cookie爬取網(wǎng)頁的方法
- python爬蟲中g(shù)et和post方法介紹以及cookie作用
- python爬蟲使用cookie登錄詳解
- Python爬蟲番外篇之Cookie和Session詳解
- Python爬蟲利用cookie實(shí)現(xiàn)模擬登陸實(shí)例詳解
- 玩轉(zhuǎn)python爬蟲之cookie使用方法
- Python中urllib+urllib2+cookielib模塊編寫爬蟲實(shí)戰(zhàn)
- python爬蟲中PhantomJS加載頁面的實(shí)例方法
- Python爬蟲JSON及JSONPath運(yùn)行原理詳解
- Python爬蟲如何破解JS加密的Cookie
相關(guān)文章
詳解Python中字符串前“b”,“r”,“u”,“f”的作用
這篇文章主要介紹了Python中字符串前“b”,“r”,“u”,“f”的作用,感興趣的朋友跟隨小編一起看看吧2019-12-12Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程
這篇文章主要介紹了Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10Python連接mysql數(shù)據(jù)庫及簡單增刪改查操作示例代碼
這篇文章主要介紹了Python連接mysql數(shù)據(jù)庫及簡單增刪改查操作示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08postman傳遞當(dāng)前時(shí)間戳實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于postman傳遞當(dāng)前時(shí)間戳知識(shí)點(diǎn)相關(guān)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2019-09-09CentOS6.9 Python環(huán)境配置(python2.7、pip、virtualenv)
這篇文章主要介紹了CentOS6.9 Python環(huán)境配置(python2.7、pip、virtualenv)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05200行python代碼實(shí)現(xiàn)貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了200行python代碼實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04