Import Requests,re,time
Header ={
"Cookie": "Cookie must be filled in after login",
"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 = "Baidu homepage-top right corner bar--Right top corner user name (my bar) and then fill in the URL here"
html = requests.get (Url,headers=header)
#print (Html.text)
Information such as #提取贴吧相关的ID name
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要控制好, otherwise easy to appear verification code, resulting in failure to sign in
Print (i[1])
Print (I[1].encode ("latin-1"))
#获取可以签到的全部贴吧名字
#print (I[1].encode ("latin-1"). Decode ("Unicode_escape"))
#获取tbs send a check-in request to get the data named TBS he is in the page information
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 ': \" (. *?) \ "" #单双引号都有 note the escape character
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这个数据意义不明 can search the relevant page code in the vicinity to see if the association can be found
#实现签到 whether the success can be seen return information
Try
Qiandao = Requests.post (Qiandao_url,data=qiandao_data,headers=header)
#print (Qiandao.text)
Print (Tieba_name, "sign in")
Except
Print (Tieba_name, "exception")
Continue
Python crawler Baidu Bar Registration widget