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)