标签:soup col on() window 个数 from set pos password
先查看首页拿到cookie,然后登陆要携带首页拿到的 cookie 才可以通过验证
"""""" # ################################### 示例一:爬取数据(携带请起头) ################################### """ import requests from bs4 import BeautifulSoup r1 = requests.get( url=‘https://dig.chouti.com/‘, headers={ ‘user-agent‘:‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36‘ } ) soup = BeautifulSoup(r1.text,‘html.parser‘) content_list = soup.find(name=‘div‘,attrs={"id":"content-list"}) item_list = content_list.find_all(name=‘div‘,attrs={‘class‘:‘item‘}) for item in item_list: a = item.find(name=‘a‘,attrs={‘class‘:‘show-content color-chag‘}) print(a.text.strip()) """ # ################################### 示例二:登陆点赞 ################################### """ import requests # 1. 查看首页 r1 = requests.get( url=‘https://dig.chouti.com/‘, headers={ ‘user-agent‘:‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36‘ } ) # 2. 提交用户名和密码 r2 = requests.post( url=‘https://dig.chouti.com/login‘, headers={ ‘user-agent‘:‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36‘ }, data={ ‘phone‘:‘8613121758648‘, ‘password‘:‘woshiniba‘, ‘oneMonth‘:1 }, cookies=r1.cookies.get_dict() # 套路 正常用户必然会先访问首页然后再登陆 # 如果你直接登陆必然是爬虫,因此设计在第一次访问首页的时候先创建cookie 并且返回了回去 # 并且要求你第二次访问的时候要带着这个 cookie ) # 3. 点赞 r3 = requests.post( url=‘https://dig.chouti.com/link/vote?linksId=20435396‘, headers={ ‘user-agent‘:‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36‘ }, cookies=r1.cookies.get_dict() ) print(r3.text) """ # ############## 方式二 session 方式 ############## """ # 用 session 自动封装好 cookie 不用在以后自己携带 import requests session = requests.Session() i1 = session.get(url="http://dig.chouti.com/help/service") i2 = session.post( url="http://dig.chouti.com/login", data={ ‘phone‘: "8615131255089", ‘password‘: "xxooxxoo", ‘oneMonth‘: "" } ) i3 = session.post( url="http://dig.chouti.com/link/vote?linksId=8589523" ) print(i3.text) """
请求头中存在自定义的验证字段,要想办法拿到才可以正确爬取,以及 Referer 的使用
import re import requests """ 密码加密了的时候 找js 通过 python 实现加密方式 直接把加密后的密文拿来用 """ r1 = requests.get( url=‘https://passport.lagou.com/login/login.html‘, headers={ ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36‘, } ) """ 有两个奇怪的东西,是网站的防御机制 这两个数据必然是对方发给我们的 要不在响应头里面,要不在响应体里面 响应头看不到。那就去响应体里面找。 """ # 因为不是写在标签里面的。只能用正则来拿了 X_Anti_Forge_Token = re.findall("X_Anti_Forge_Token = ‘(.*?)‘", r1.text, re.S)[0] X_Anti_Forge_Code = re.findall("X_Anti_Forge_Code = ‘(.*?)‘", r1.text, re.S)[0] # print(X_Anti_Forge_Token, X_Anti_Forge_Code) r2 = requests.post( url=‘https://passport.lagou.com/login/login.json‘, headers={ ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36‘, ‘X-Anit-Forge-Code‘:X_Anti_Forge_Code, ‘X-Anit-Forge-Token‘:X_Anti_Forge_Token, ‘Referer‘: ‘https://passport.lagou.com/login/login.html‘, # 上一次请求地址是什么?很多网站会要求带着个才可以继续 }, data={ "isValidate": True, ‘username‘: ‘15131255089‘, ‘password‘: ‘ab18d270d7126ea65915c50288c22c0d‘, # 直接发密文了 ‘request_form_verifyCode‘: ‘‘, ‘submit‘: ‘‘ }, cookies=r1.cookies.get_dict() ) print(r2.text)
scrf_token 的验证
"""""" # ################################### 示例三:自动登录GitHub ################################### # 1. GET,访问登录页面 """ - 去HTML中找隐藏的Input标签获取csrf token - 获取cookie """ # 2. POST,用户名和密码 """ - 发送数据: - csrf - 用户名 - 密码 - 携带cookie """ # 3. GET,访问https://github.com/settings/emails """ - 携带 cookie """ import requests from bs4 import BeautifulSoup # ########################################################## # 访问登陆页面,获取 authenticity_token i1 = requests.get( url=‘https://github.com/login‘ ) soup1 = BeautifulSoup(i1.text, features=‘lxml‘) tag = soup1.find(name=‘input‘, attrs={‘name‘: ‘authenticity_token‘}) authenticity_token = tag.get(‘value‘) # authenticity_token 拿到 c1 = i1.cookies.get_dict() i1.close() # 携带authenticity_token和用户名密码等信息,发送用户验证 form_data = { "authenticity_token": authenticity_token, # 放在请求体中发过去 "utf8": "", "commit": "Sign in", "login": "", ‘password‘: ‘‘ } i2 = requests.post( url=‘https://github.com/session‘, data=form_data, cookies=c1 ) c2 = i2.cookies.get_dict() c1.update(c2) # 将两次的 cookie 整合一起 i3 = requests.get(‘https://github.com/settings/repositories‘, cookies=c1) soup3 = BeautifulSoup(i3.text, features=‘lxml‘) list_group = soup3.find(name=‘div‘, class_=‘listgroup‘) from bs4.element import Tag for child in list_group.children: if isinstance(child, Tag): project_tag = child.find(name=‘a‘, class_=‘mr-1‘) size_tag = child.find(name=‘small‘) temp = "项目:%s(%s); 项目路径:%s" % (project_tag.get(‘href‘), size_tag.string, project_tag.string, ) print(temp)
user-agent
referer
host
cookie
特殊请起头,查看上一次请求获取内容。
‘X-Anit-Forge-Code‘:...
‘X-Anit-Forge-Token‘:...
- 原始数据
- 原始数据 + token
- 密文
- 找算法
- 使用密文
- post登录获取cookie,以后携带cookie
- get获取未授权cookie,post登录携带cookie去授权,以后携带cookie
标签:soup col on() window 个数 from set pos password
原文地址:https://www.cnblogs.com/shijieli/p/10358576.html