import requests, time
from bs4 import BeautifulSoup
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
# ip = requests.get('http://http.tiqu.qingjuhe.cn/getip?num=10&type=1&pack=32260&port=1&lb=1&pb=4®ions=').text
# 抽屉网主页入口(他的反扒策略主页入口并不是https://dig.chouti.com/)
base_url = 'https://dig.chouti.com/all/hot/recent/{}'
# 循环所有的列表页
for page in range(1, 121):
# proxy = {'http': ip.strip()}
page_url = base_url.format(page)
# 使用requests请求页面
index_res = requests.get(page_url, headers=headers)
# 得到cookie
index_cookies = index_res.cookies
soup = BeautifulSoup(index_res.text, 'lxml')
# 获取每条资讯的div标签
div = soup.find_all(name='div', attrs={'class': 'part2'})
for line in div:
# 获得资讯id
new_id = line.attrs['share-linkid']
# 拼接点赞链接
like_url = 'https://dig.chouti.com/link/vote?linksId=%s' % new_id
# 开始登陆
login_url = 'https://dig.chouti.com/login'
# 登陆数据
data = {
'phone': '86你的用户名',
'password': '你的密码',
'oneMonth': '1'
}
# 提交登陆请求
login_res = requests.post(login_url, data=data, headers=headers, cookies=index_cookies)
headers2 = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
'referer': page_url
}
# 提交点赞请求
like_res = requests.post(like_url, headers=headers2, cookies=index_cookies)
# 打印点赞响应信息
print(like_res.text)
# 提价评论url
commit_url = 'https://dig.chouti.com/comments/create'
# 提交评论内容
commit_data = {
'jid': '你的个人站点名',
'linkId': new_id,
'isAssent': ''",
'content': '我觉得他说的很对!我很赞同',
'sortType': 'score'
}
# 提交评论请求
commit_res = requests.post(commit_url, headers=headers2, cookies=index_cookies, data=commit_data)
# 打印评论响应信息
print(commit_res.text)
# 由于该网站存在评论时间限制所以,先睡十秒在进行下一次评论
time.sleep(10)
使用requests和BeautifulSoup,模拟抽屉网的登录、点赞和评论
最新推荐文章于 2021-05-21 23:14:13 发布