python爬虫获取以及使用cookie
首先获取cookie:
@logger.catch()
def get_cookie():
cookie_headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome / 53.0.2785.143Safari / 537.36',
'Connection': 'keep-alive',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'referer': 'https://*****.com/'
}
# 设置保存cookie的文件,同级目录下的cookie.txt
filename = 'cookie.txt'
# 声明一个MozillaCookieJar对象实例来保存cookie,之后写入文件
cookie_aff = cookiejar.MozillaCookieJar(filename)
# 利用urllib.request库的HTTPCookieProcessor对象来创建cookie处理器,也就CookieHandler
handler = urllib.request.HTTPCookieProcessor(cookie_aff)
# 通过CookieHandler创建opener
opener = urllib.request.build_opener(handler)
# 此处的open方法打开网页
cooki