共計 3813 個字符,預計需要花費 10 分鐘才能閱讀完成。
這篇文章主要講解了“網頁 Cookie 如何獲取”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“網頁 Cookie 如何獲取”吧!
這里采用 python2.7
第一種:mechanize
首先我們要使用 mechanize,第一步:
pip install mechanize
第二步編寫獲取 cookie 代碼:
import osimport mechanizeimport cookielib,re
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.set_debug_http(True)
br.addheaders = [(User-agent , 用戶 ua)]
br.set_proxies({http : 代理})
response = br.open(https://www.amazon.com)
cj = br._ua_handlers[_cookies].cookiejarfor cookie in cj:
print(cookieName: +cookie.name)
print(cookieValue: +cookie.value)
cookie = [item.name + : + item.value for item in cj]
cookiestr={}for item in cookie:
name,value = item.split(:)
cookiestr[name]=value
運行結果:
第二種:urllib
import urllib2import cookielibfrom http import cookiejarfrom bs4 import BeautifulSoup
User_Agent = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36 header = {}
header[User-Agent] = User_Agent
cookie = cookiejar.CookieJar()
cookie_handle=urllib2.HTTPCookieProcessor(cookie)
cookie_opener = urllib2.build_opener(cookie_handle)# proxy_support = urllib2.ProxyHandler({http : 5.62.157.47:8085})# proxy_opener = urllib2.build_opener(proxy_support)urllib2.install_opener(cookie_opener)# urllib2.install_opener(proxy_opener)request = urllib2.Request(https://www.amazon.com ,headers=header)
response = urllib2.urlopen(request)for item in cookie:
print(Name = +item.name)
print(Value = +item.value)
運行結果:
第三種:requests
import requests
headers = {user-agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36 }
r = requests.get(https://www.amazon.com , headers = headers)for cookie in r.cookies:
print(cookie.name)
print(cookie.value)
print(=========)
運行結果:
第四種:selenium(個人感覺這個雖然加載比較慢,但是獲取 cookie 最全)
pip install selenium
代碼:
from selenium import webdriver
driver = webdriver.Chrome(executable_path= d:/seop/chromedriver.exe)
driver.get(https://www.amazon.com)#for c in cookiestr.keys():# driver.add_cookie({ name :c, value :cookiestr[c]})#driver.get(https://www.amazon.com)cookie = [item[ name] + = + item[value] for item in driver.get_cookies()]
cookiestr = .join(item for item in cookie)
運行結果:
第五種: 總覺得 selenium 比較慢,打開還要加載瀏覽器,于是嘗試了 htmlunit 以及 phantomjs
htmlunit
phantomjs
from selenium import webdriver
browser = webdriver.PhantomJS()
browser.get(https://www.amazon.com)
cookie = [item[ name] + = + item[value] for item in browser.get_cookies()]
cookiestr = .join(item for item in cookie)
運行結果:
第六種:scrapy
這邊我們簡單測試一下,首先你電腦已經要安裝了 scrapy,如果沒有安裝,pip install scrapy
然后我們輸入要獲取地址的 cookie
scrapy shell https://www.amazon.com
cookie 結果:
最后一種:chrome headless 使用無頭瀏覽器來獲取
這個目前我是在 centos 上面進行操作:
第一步:肯定你要安裝 chrome 啦
第二步:運行安裝腳本
curl https://intoli.com/install-google-chrome.sh | bash
測試是否成功:運行以下命令,如果成功會在當前目錄下面保存百度的截圖
google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot https://www.baidu.com
這里我們開始獲取 cookie 信息
first:
google-chrome-stable --no-sandbox --headless --disable-gpu --user-data-dir= $HOME/Library/Application Support/Google/Chrome/ --remote-debugging-port=9222 https://www.amazon.com
second: 這里我們主要是獲取 websocket 的 url
curl -s localhost:9222/json
third:這邊要注意哦,要安裝 wsc,安裝 wsc 之前記得要安裝 npm 哦,然后在執行 npm install -g wsc, 然后在執行以下命令
wsc ws://localhost:9222/devtools/page/D42AFC3C9AF9C8A1511ADC60850BD5A8
然后輸入:
{id : 1, method : Network.getAllCookies}
最后 cookie 結果:
目前嘗試了 mechanize、urllib、selenium、headless chrome、requests、htmlunit、phantomjs、scrapy
目前已經嘗試了以上八種,覺得還是 selenium 獲取 cookie 比較全,信息比較完整,獲取 cookie 的字段也是比較穩定的,經過研究 cookie,就是 selenium 獲取 cookie 的速度比較慢,看看還有沒啥辦法優化速度,繼續查閱別的方式來獲取 cookie。
感謝各位的閱讀,以上就是“網頁 Cookie 如何獲取”的內容了,經過本文的學習后,相信大家對網頁 Cookie 如何獲取這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!