这篇文章主要讲解了“网页Cookie如何获取”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“网页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。
最新评论