爬虫之模拟登陆
模拟登录:
爬取基于某些用户的用户信息。
需求:对人人网进行模拟登录。
点击登录按钮之后会发起一个post请求
post请求中会携带登录之前录入的相关的登录信息(用户名,密码,验证码......)
验证码:每次请求都会变化
http/https协议特性:无状态。
没有请求到对应页面数据的原因:
发起的第二次基于个人主页页面请求的时候,服务器端并不知道该此请求是基于登录状态下的请求。
cookie:用来让服务器端记录客户端的相关状态。
手动处理:通过抓包工具获取cookie值,将该值封装到headers中。(不建议)
自动处理:
cookie值的来源是哪里?
模拟登录post请求后,由服务器端创建。
session会话对象:
作用:
1.可以进行请求的发送。
2.如果请求过程中产生了cookie,则该cook ie会被自动存储/携带在该session对象中
创建一个sess ion对象: session=requests.Session()
使用session对象进行模拟登录post请求的发送(cookie就会被存储在session中)
session对象对个人主页对应的get请求进行发送(携带了cookie)
1 # 爬取古诗文网的个人首页
2 # 封装一个验证码识别的函数
3 def get_code(filename,type):
4 chaojiying = Chaojiying_Client("2206767909", "ygcqqmmff123", '121212') # 用户中心>>软件ID 生成一个替换 96001
5 im = open(filename, 'rb').read() # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
6 return chaojiying.PostPic(im, type)["pic_str"]
7 if __name__ == '__main__':
8 log_url = "https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx"
9
10 headers = {
11 "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
12 }
13 page_text = requests.get(url=log_url,headers=headers).text
14
15 # 使用etree来解析网页数据
16 tree = etree.HTML(page_text)
17 img_src = 'https://so.gushiwen.cn' + tree.xpath("//img[@id='imgCode']/@src")[0]
18 img_content = requests.get(url=img_src, headers=headers).content
19
20 # 将验证码保存到了本地
21 with open("./古诗文网验证码图片.jpg", "wb") as f:
22 f.write(img_content)
23 print("验证码下载成功!")
24
25 # 使用超级鹰的验证码识别功能来进行验证码的识别
26 # code_text = get_code('古诗文网验证码图片.jpg', 1902)
27 code_text = input('请输入验证码:')
28 print("验证码:",code_text)
29 # 发起登录请求
30 req_url = "https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx"
31 data = {
32 '__VIEWSTATE': 'CtwVprFwEarzmwBuk+FfACxLG/FFbLIXRtfAagmX2/rVeUGgPB3ELcqfGFd2GBJpz/nvnNnb4ErPy6HjSyVOIOYXSD3L0H2/iN6xJLkobZYGYj3TmvCkt6vu9ys=',
33 '__VIEWSTATEGENERATOR': 'C93BE1AE',
34 'from': 'http://so.gushiwen.cn/user/collect.aspx',
35 'email': '2206767909@qq.com',
36 'pwd': 'ygc123456',
37 'code': code_text,
38 'denglu':'登录',
39 }
40 # 自动设置cookie
41 session = requests.Session()
42 response = session.post(url=req_url,headers=headers,data=data) # session对象会保存登录时的cookie,用于对需要登录后才可浏览的网页进行爬取
43 print("状态码:",response.status_code)
44 print(session.cookies)
45 detail_url = "https://so.gushiwen.cn/user/collect.aspx"
46 # 手动设置cookie,在自动保存cookie失效时使用,写在头部。cookie具有时效性,需不定期的更换
47 headers = {
48 "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
49 "cookie": "login=flase; ASP.NET_SessionId=lokudohiqrsiovh4ax4oyjrv; wsEmail=2206767909%40qq.com; idsMingju2017=%2c162%2c; codeyzgswso=d813633832235288; gsw2017user=1228175%7cBA8AD79F5FA394011E2C4F1A917FF36B; login=flase; gswZhanghao=2206767909%40qq.com; gswEmail=2206767909%40qq.com; wxopenid=defoaltid; idsShiwen2017=%2c7722%2c49386%2c71137%2c71138%2c21744%2c1%2c71250%2c47916%2c71139%2c49394%2c",
50 }
51 # 使用保存了cookie的session对象来对网页进行爬取
52 detail_page = session.get(url=detail_url,headers=headers).text
53 # 持久化存储
54 with open("./古诗网个人界面.html","w",encoding="utf-8") as f:
55 f.write(detail_page)
56 tree = etree.HTML(detail_page)
57 div_list = tree.xpath("//div[@class='sons']/kkkdiv")
58 c = 0
59 f = open("./love_poem.txt","w",encoding="utf-8")
60 for div in div_list:
61 # poem = div.xpath("./a//text()")
62 # name = poem[0]
63 # author = poem[1]
64 poem_url = "https://so.gushiwen.cn/" + div.xpath("./a/@href")[0]
65 poem_detail = requests.get(url=poem_url,headers=headers).text
66 tree = etree.HTML(poem_detail)
67 title = tree.xpath("//div[@class='sons'][1]/div[@class='cont']/h1/text()")[0]
68 author = "".join(tree.xpath("//div[@class='sons'][1]/div[@class='cont']/p//text()"))
69 content = tree.xpath('//div[@class="sons"][1]/div[@class="cont"]/div[@class="contson"]//text()')
70 while "\n" in content:
71 content.remove("\n")
72 content = "\n".join(content)
73 f.write(title+'\n'+author+'\n'+content+"\n\n\n")
74 c += 1
75 print("第%d首已下载完成!"%(c))
76 f.close()
77 print("结束")
代理:破解封IP这种反爬机制。
什么是代理:
代理服务器。
反爬机制:封ip
反反爬策略:使用代理进行请求发送
代理的作用:
突破自身IP访问的限制。
隐藏自身真实IP
代理相关的网站:
快代理
西祠代理
www.goubanjia.com
代理ip的类型:
http:应用到http协议对应的urL中
https:应用到ht tps协议对应的ur1中
代理ip的匿名度:
透明:服务器知道该次请求使用了代理,也知道请求对应的真实ip
匿名:知道使用了代理,不知道真实ip
高匿:不知道使用了代理,更不知道真实的ip
1 if __name__ == '__main__': 2 url = "http://www.baidu.com/s?tn=02003390_hao_pg&ie=utf-8&wd=ip" 3 headers = { 4 "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36' 5 } 6 # proxies={"url的类型:http或https":"代理的ip值"} 7 page_data = requests.get(url=url,headers=headers,proxies={"http":"220.249.149.58:9999"}).text 8 with open("./百度代理.html","w",encoding="utf-8") as f: 9 f.write(page_data)

浙公网安备 33010602011771号