python3爬虫之如何使用浏览器cookie

小编给大家分享一下python3爬虫之如何使用浏览器cookie,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!


以网页提取标题为例

>>> import re

>>> get_title = lambda html: re.findall('<title>(.*?)</title>', html, flags=re.DOTALL)[0].strip()

未登录情况下下载得到的标题:

>>> import urllib2

>>> url = 'https://bitbucket.org/'

>>> public_html = urllib2.urlopen(url).read()

>>> get_title(public_html)

'Git and Mercurial code management for teams'

使用第三方库browsercookie,获取cookie再下载:

>>> import urllib.request

>>> public_html = urllib.request.urlopen(url).read()

>>> opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

看完了这篇文章,相信你对python3爬虫之如何使用浏览器cookie有了一定的了解,想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!