【Python】百度贴吧自动签到从编写-部署到服务器_哔哩哔哩_bilibili
跟着这个大佬写的,稍微加了自己的一点想法,其他基本差不多。
签到失败是因为已经签到过不能重复签
萌新刚入门,欢迎来交流
import requests
from bs4 import BeautifulSoup
# cookie直接上去格式会乱掉,所以用了这么个奇奇怪怪的方式
cookie = r"""cookie替换这里"" + "\""
signUrl = 'https://tieba.baidu.com/sign/add'
# 请求头
headers = {
'User-Agent': "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Mobile Safari/537.36",
'cookie': cookie
}
# 查找页数
def search_all_page():
url = "https://tieba.baidu.com/f/like/mylike?v=1720786625680&pn=1"
get_html = requests.get(url, headers=headers).text
soup = BeautifulSoup(get_html, 'html.parser')
return len(soup.select('.pagination>a'))
page_num = search_all_page() - 1
# 查找关注的吧
def search_like(page):
like_list = []
url = "https://tieba.baidu.com/f/like/mylike?v=1720787781194&pn="
for i in range(page):
html = requests.get(url + str(i + 1), headers=headers).text
soup = BeautifulSoup(html, 'html.parser')
for item in soup.select('.forum_main>.forum_table>table>tr>td>a'):
title = item.attrs.get('title')
if title and title.find("级会员")==-1:
like_list.append(title)
return like_list
like_list = search_like(page_num)
print("开始签到")
# 具体签到方法
def sign(title):
data = {
'ie': 'utf-8',
'kw': title, # 吧名
}
response = requests.post(signUrl, data=data, headers=headers)
if response.text == r"""{"no":1101,"error":"\u4eb2\uff0c\u4f60\u4e4b\u524d\u5df2\u7ecf\u7b7e\u8fc7\u4e86","data":""}""":
print(title + "签到失败")
return 0
else:
print(title + "签到成功")
return 1
success=0
fail=0
for item in like_list:
if sign(item):
success=success+1
else:
fail=fail+1
print("===================================")
print("签到完成,共有{}吧,签到成功{}个,失败{}个".format(len(like_list),success,fail))