无特殊设计,全靠手动收集cookie。
代码案例在:https://github.com/suchocolate/test/blob/master/spider/tieba/
点击方法,滤出post,找到onekey,点击该post后右侧出现请求头。
点击原始码按钮,复制请求头的原始码,将其制作成python字典。
再点击右侧参数,复制出表单数据,将其制作成python字典。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# requests模拟发起http,time用于打印时间
import requests
import time
# url固定的
url = 'https://tieba.baidu.com/tbmall/onekeySignin1'
# 把请求头制作成字典
headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "tieba.baidu.com",
"Refer": "https://tieba.baidu.com/index.html",
"Cookie": "TIEBA_USERTYPE=18658700de1e8f842ed81648; BAIDUID=FC561B40E910ADEF94887D62CF306E65:FG=1; TIEBAUID=240918a99f647b3abf3f8383; Hm_lvt_98b9d8c2fd6608d564bf2ac2ae642948=1580004689,1580029385,1580030020,1580030118; Hm_lpvt_98b9d8c2fd6608d564bf2ac2ae642948=1580030314; BDUSS=X5KT1o0eUFXQVJ0RGFPT35QNG82Vi1SVVVXVUljZUJOd0k1bzJKdlhEcFQ1bFJlSUFBQUFBJCQAAAAAAAAAAAEAAAAQTRwgQ3JpdGljMjAxMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFNZLV5TWS1eM; STOKEN=0098802104ccad1432d0fbe4d06d1d9a8634dad014f8f4609ce1c8c445863599",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0",
"X-Requested-With": "XMLHttpRequest"
}
# 把表单数据制作成字典
data={"ie": "utf-8", "tbs": "95b8e93ebde16bbf1580030293"}
# 发起自动签到
r = requests.post(url,headers=headers,data=data)
# 查看结果
print(time.asctime(time.localtime(time.time())),r.status_code)
crontab -e
1 2 * * * python -u /usr/my/tieba.py >> /var/log/my/tieba.log 2>&1
运行了2天的结果
tail /var/log/my/tieba.log
('Sat Jan 25 02:01:02 2020', 200)
('Sun Jan 26 02:01:02 2020', 200)
开始自动签到后,浏览器中的登陆不要注销,否则cookie将会失效。
另外也可以用curl登陆,右键点击post,复制为curl命令:
复制的命令制作成sh脚本:
#!/bin/bash
curl 'https://tieba.baidu.com/tbmall/onekeySignin1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0' -H 'Accept: */*' -H 'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2' --compressed -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Origin: https://tieba.baidu.com' -H 'Connection: keep-alive' -H 'Referer: https://tieba.baidu.com/index.html' -H 'Cookie: BAIDUID=6E2D330C269323081C4805FAB335E411:FG=1; BIDUPSID=9C286F75F7E6CCBDCD44F29C129D1B61; PSTM=1580452093; BDUSS=VJuMXFZajQ4ZFEtTnRFU3JIM28zfkZsUm1VQU9TaVF0SkpFTWZiUkJYOEZWbHRlSVFBQUFBJCQAAAAAAAAAAAEAAAAQTRwgQ3JpdGljMjAxMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAXJM14FyTNea; TIEBA_USERTYPE=5f4d5be1da1fe58af0bb90d9; STOKEN=da9b2136cfc5b6e5e847c59441aaa7d0c19778f83e9f9edc92aa193011a847ba; TIEBAUID=240918a99f647b3abf3f8383; Hm_lvt_98b9d8c2fd6608d564bf2ac2ae642948=1580455345,1580461298,1580481580,1580532851; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; cflag=13%3A3; rpln_guide=1; bdshare_firstime=1580455362468; H_PS_PSSID=1429_21111; Hm_lpvt_98b9d8c2fd6608d564bf2ac2ae642948=1580532854; showCardBeforeSign=1' --data 'ie=utf-8&tbs=8633fdc81ea490d61580532849'
每天02:01点签到
crontab -e
1 2 * * * bash /usr/my/tieba.sh > /var/log/my/tieba_sh.log 2>&1