您好,登錄后才能下訂單哦!
如下所示:
import urllib.request import urllib.parse url = 'https://weibo.cn/5273088553/info' #正常的方式進行訪問 # headers = { # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36' # } # 攜帶cookie進行訪問 headers = { 'GET https':'//weibo.cn/5273088553/info HTTP/1.1', 'Host':' weibo.cn', 'Connection':' keep-alive', 'Upgrade-Insecure-Requests':' 1', 'User-Agent':' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36', 'Accept':' text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', # 'Referer: https':'//weibo.cn/', 'Accept-Language':' zh-CN,zh;q=0.9', 'Cookie':' _T_WM=c1913301844388de10cba9d0bb7bbf1e; SUB=_2A253Wy_dDeRhGeNM7FER-CbJzj-IHXVUp7GVrDV6PUJbkdANLXPdkW1NSesPJZ6v1GA5MyW2HEUb9ytQW3NYy19U; SUHB=0bt8SpepeGz439; SCF=Aua-HpSw5-z78-02NmUv8CTwXZCMN4XJ91qYSHkDXH4W9W0fCBpEI6Hy5E6vObeDqTXtfqobcD2D32r0O_5jSRk.; SSOLoginState=1516199821', } request = urllib.request.Request(url=url,headers=headers) response = urllib.request.urlopen(request) #輸出所有 # print(response.read().decode('gbk')) #將內容寫入文件中 with open('weibo.html','wb') as fp: fp.write(response.read())
以上這篇Python3爬蟲之urllib攜帶cookie爬取網頁的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。