import requests from bs4 import BeautifulSoup #先向主页发送请求,获取点赞目标用户id headers = { "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" } base_url = "https://dig.chouti.com/all/hot/recent/{}" for line in range(1,121): index_url = base_url.format(line) #反爬 需要通过all/hot/rencent/作为主页的入口 index_res = requests.get(index_url,headers = headers) #访问主页返回的cookies index_cookies= index_res.cookies #解析提取用户的id soup = BeautifulSoup(index_res.text,"lxml") div = soup.find_all(name="div",attrs={"class":"part2"}) for line in div: new_id = line.attrs["share-linkid"] nice_url = "https://dig.chouti.com/link/vote?linksId=%s"%new_id #登录 login_url = "https://dig.chouti.com/login" data = { 'phone': '8615016868647', 'password': 'DKC19960413', 'oneMonth': '1' } login_res = requests.post(login_url,data=data,headers=headers,cookies=index_cookies) #登录成功返回9999 登录失败访问主页 ''' {"result":{"code":"9999", "message":"", "data":{"complateReg":"0","destJid":"cdu_55906084308"}}} ''' print(login_res.text) header2 = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36', # referer必须填上一次发送请求的https://dig.chouti.com/all/hot/recent/1 'referer': 'https://dig.chouti.com/all/hot/recent/1' } #3点赞 cookies携带的是第一次发送请求的https://dig.chouti.com/all/hot/recent/1页面 nice_res = requests.post(nice_url,headers = header2,cookies=index_cookies) print(nice_res.text) #4评论 #请求url commit_url = "https://dig.chouti.com/comments/create" commit_data = { 'jid': 'cdu_55906084308', 'linkId': new_id, 'isAssent': "", 'content': 'tank 到此一游', 'sortType': 'score' } commit_res = requests.post( commit_url,headers=header2,cookies=index_cookies,data=commit_data ) print(commit_res.text) import time time.sleep(8)
- 自动登录抽屉新热榜并自动点赞与评论
- 先清除浏览器缓存信息
- 打开开发者模式进行通信流程的分析:
鼠标右击 ---> 点击检查 ---> network ---> preserver log、disable cache
- all: 所有的请求
- xhr: ajax异步请求
一 主页:
请求url:
https://dig.chouti.com/
请求方式:
GET
请求头:
cookie
user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
二 登录页面
- 错误登录
请求url:
https://dig.chouti.com/login
请求方式:
POST
请求头:
referer: https://dig.chouti.com/
user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
请求体:
phone: 8615016868641
password: DKC19960413
oneMonth: 1
- 正确登录
请求url:
https://dig.chouti.com/login
请求方式:
POST
响应头:
set-cookie
请求头:
referer: https://dig.chouti.com/
user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
请求体:
phone: 8615016868641
password: DKC19960413
oneMonth: 1
- 登录成功后直接访问抽屉 主页
https://dig.chouti.com/
referer: https://dig.chouti.com/
三 分析点赞
请求url:
https://dig.chouti.com/link/vote?linksId=26331621
https://dig.chouti.com/link/vote?linksId=26331306
26331621
请求方式:
POST
请求头:
cookie
referer: https://dig.chouti.com/
user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
1.获取点赞目标用户ID
<div class="part2" share-linkid="26332241" >
2.拼接url并发送POST请求
评论:
请求url:
https://dig.chouti.com/comments/create
请求方式:
POST
请求头:
cookie
referer: https://dig.chouti.com/all/hot/recent/1
user-agent
请求体:
jid: cdu_55906084308
linkId: new_id
isAssent: ""
content: tank
sortType: score