使用requests和BeautifulSoup,模拟抽屉网的登录、点赞和评论

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&regions=').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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值