1. 程式人生 > >python 爬蟲 百度貼吧簽到小工具

python 爬蟲 百度貼吧簽到小工具

sca window user con lee post use wow64 搜索

import requests,re,time
header ={
"Cookie":"登陸過賬號後的cookie 必須填寫",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
}
#訪問個人帳號下的貼吧主頁
url = "百度首頁--右上角貼吧--右上角用戶名(我的貼吧) 然後把url填到這裏"
html = requests.get(url,headers=header)
#print(html.text)

#提取貼吧相關的ID 名稱等信息
s1 = r‘"forum_id":(.*?),"forum_name":"(.*?)"‘
tieba_info = re.compile(s1,re.S).findall(str(html.text))
#print(tieba_info)

for i in tieba_info:
time.sleep(3)#訪問CD要控制好,否則容易出現驗證碼,導致簽到失敗
print(i[1])
print(i[1].encode("latin-1"))
#獲取可以簽到的全部貼吧名字
#print(i[1].encode("latin-1").decode("unicode_escape"))

#獲取tbs 發送簽到請求需要獲得名為tbs的數據 他在頁面信息裏面
tieba_name = (i[1].encode("latin-1").decode("unicode_escape"))
tieba_link = "https://tieba.baidu.com/f?kw=" + tieba_name
info = requests.get(tieba_link,headers=header)
#print(info.text)
s2 =r"tbs‘: \"(.*?)\"" #單雙引號都有 註意轉義字符
tieba_tbs = re.compile(s2,re.S).findall(str(info.text))[0]
#print(tieba_tbs)

#簽到的postdata
qiandao_url = "https://tieba.baidu.com/sign/add"
qiandao_data = {"ie":"utf-8",
"kw":tieba_name,
"tbs":tieba_tbs} #tbs這個數據意義不明 可以在附近相關網頁代碼中搜索看看 是否能發現關聯

#實現簽到 是否成功 可以看返回信息
try:
qiandao = requests.post(qiandao_url,data=qiandao_data,headers=header)
#print(qiandao.text)
print(tieba_name,"簽到")

except:
print(tieba_name,"異常")
continue

python 爬蟲 百度貼吧簽到小工具