Python3 爬虫带上 cookie
Cookie的英文原意是“点心”,它是在客户端访问Web服务器时,服务器在客户端硬盘上存放的信息,好像是服务器发送给客户的“点心”。服务器可以根据Cookie来跟踪客户状态,这对于需要区别客户的场合(如电子商务)特别有用。
当客户端首次请求访问服务器时,服务器先在客户端存放包含该客户的相关信息的Cookie,以后客户端每次请求访问服务器时,都会在HTTP请求数据中包含Cookie,服务器解析HTTP请求中的Cookie,就能由此获得关于客户的相关信息。
下面我们就来看一下python3爬虫带上cookie的方法:
1、直接将Cookie写在header头部
立即学习“Python免费学习笔记(深入)”;
# coding:utf-8import requestsfrom bs4 import BeautifulSoupcookie = '''cisession=19dfd70a27ec0eecf1fe3fc2e48b7f91c7c83c60;CNZZDATA1000201968=1815846425-1478580135-https%253A%252F%252Fwww.baidu.com%252F%7C1483922031;Hm_lvt_f805f7762a9a237a0deac37015e9f6d9=1482722012,1483926313;Hm_lpvt_f805f7762a9a237a0deac37015e9f6d9=1483926368'''header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36','Connection': 'keep-alive','accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Cookie': cookie}url = 'https://www.jb51.net/article/191947.htm'wbdata = requests.get(url,headers=header).textsoup = BeautifulSoup(wbdata,'lxml')print(soup)
2、使用requests插入Cookie
# coding:utf-8import requestsfrom bs4 import BeautifulSoupcookie = {"cisession":"19dfd70a27ec0eecf1fe3fc2e48b7f91c7c83c60","CNZZDATA100020196":"1815846425-1478580135-https%253A%252F%252Fwww.baidu.com%252F%7C1483922031","Hm_lvt_f805f7762a9a237a0deac37015e9f6d9":"1482722012,1483926313","Hm_lpvt_f805f7762a9a237a0deac37015e9f6d9":"1483926368"}url = 'https://www.jb51.net/article/191947.htm'wbdata = requests.get(url,cookies=cookie).textsoup = BeautifulSoup(wbdata,'lxml')print(soup)
实例扩展:
使用cookie登录哈工大ACM站点
获取站点登录地址
http://acm.hit.edu.cn/hoj/system/login
查看要传送的post数据
user和password
Code:
#!/usr/bin/env python# -*- coding: utf-8 -*-"""__author__ = 'pi'__email__ = 'pipisorry@126.com'"""import urllib.request, urllib.parse, urllib.errorimport http.cookiejarLOGIN_URL = 'http://acm.hit.edu.cn/hoj/system/login'values = {'user': '******', 'password': '******'} # , 'submit' : 'Login'postdata = urllib.parse.urlencode(values).encode()user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'headers = {'User-Agent': user_agent, 'Connection': 'keep-alive'}cookie_filename = 'cookie.txt'cookie = http.cookiejar.MozillaCookieJar(cookie_filename)handler = urllib.request.HTTPCookieProcessor(cookie)opener = urllib.request.build_opener(handler)request = urllib.request.Request(LOGIN_URL, postdata, headers)try: response = opener.open(request) page = response.read().decode() # print(page)except urllib.error.URLError as e: print(e.code, ':', e.reason)cookie.save(ignore_discard=True, ignore_expires=True) # 保存cookie到cookie.txt中print(cookie)for item in cookie: print('Name = ' + item.name) print('Value = ' + item.value)get_url = 'http://acm.hit.edu.cn/hoj/problem/solution/?problem=1' # 利用cookie请求訪问还有一个网址get_request = urllib.request.Request(get_url, headers=headers)get_response = opener.open(get_request)print(get_response.read().decode())# print('You have not solved this problem' in get_response.read().decode())
推荐教程:《Python教程》