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.dhdzp.com/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.dhdzp.com/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實(shí)現(xiàn)多個(gè)視頻文件合成畫中畫效果
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)多個(gè)視頻文件合成畫中畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Python機(jī)器學(xué)習(xí)應(yīng)用之決策樹分類實(shí)例詳解
決策樹(Decision?Tree)是在已知各種情況發(fā)生概率的基礎(chǔ)上,通過構(gòu)成決策樹來求取凈現(xiàn)值的期望值大于等于零的概率,評(píng)價(jià)項(xiàng)目風(fēng)險(xiǎn),判斷其可行性的決策分析方法,是直觀運(yùn)用概率分析的一種圖解法2022-01-01
IDEA創(chuàng)建python項(xiàng)目詳細(xì)圖文教程
在開始編寫Python代碼之前,需要在IDEA中配置Python環(huán)境,下面這篇文章主要給大家介紹了關(guān)于IDEA創(chuàng)建python項(xiàng)目的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
Python實(shí)現(xiàn)Matplotlib,Seaborn動(dòng)態(tài)數(shù)據(jù)圖的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何讓Matplotlib、Seaborn的靜態(tài)數(shù)據(jù)圖動(dòng)起來,變得栩栩如生。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2022-05-05
Python 快速實(shí)現(xiàn)CLI 應(yīng)用程序的腳手架
本篇文章主要介紹了Python 快速實(shí)現(xiàn)CLI 應(yīng)用程序的腳手架,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
Selenium執(zhí)行完畢未關(guān)閉chromedriver/geckodriver進(jìn)程的解決辦法(java版+python版
這篇文章主要介紹了Selenium執(zhí)行完畢未關(guān)閉chromedriver/geckodriver進(jìn)程的解決辦法(java版+python版),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

