本帖最后由 正己 于 2020-9-25 12:19 编辑
0x1前言
其实之前就发过帖子,不过在后来运行的过程中遇到了问题,这段时间一直没解决,菜鸡实力不够,眼界太狭窄了,所以来请教一下论坛的大佬们
0x2问题
这是在github上面运行的情况,最新的,我一个学长跟我说过跟吾爱的缓存机制有关,但是本菜鸡也不知道到怎么处理,如下图,返回为空,这个时候刚好就是更新了一篇帖子,但是他为什么会返回为空,我代码中做了判断,如果返回为空的时候,就再次请求一次,但是连续请求十次都是返回为空
0x3源码
代码写得很烂,大佬们见谅
import requests
import time
import re
headers = {
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36",
'Connection': 'keep-alive',
'host': 'www.52pojie.cn',
'Upgrade-Insecure-Requests': '1',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Enocding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
}
skey = 'SCU112948T8*****' # 你的server酱skey
url = 'https://www.52pojie.cn/forum.php?mod=forumdisplay&fid=24&filter=author&orderby=dateline&typeid=29'
def push():
server_url = "https://sc.ftqq.com/%s.send" % skey
res = getHTMLText2(url)
des = re.findall('class="s xst">(.*?)</a>', res)[0]
print(des)
params = {
"text": '爷,您关注的帖子更新啦!',
"desp": des
}
response = requests.get(server_url, params=params)
json_data = response.json()
if json_data['errno'] == 0:
print(" 推送成功。")
else:
print("推送失败")
def getHTMLText(url):
try:
r = requests.get(url, headers=headers)
r.raise_for_status() # 如果状态不是200,引发HTTPError异常
r.encoding = r.apparent_encoding
return r.text
except:
return "网站访问异常"
def getHTMLText2(url):
try:
r = requests.get(url)
r.raise_for_status() # 如果状态不是200,引发HTTPError异常
r.encoding = r.apparent_encoding
return r.text
except:
return "网站访问异常"
def get_id():
res = getHTMLText(url)
time.sleep(1)
id = re.findall("CONTENT_TID='(.*?)'", res)
i = 0
while id == []:
print("id为空")
time.sleep(5)
res = getHTMLText(url)
id = re.findall("CONTENT_TID='(.*?)'", res)
i +=1
print(i,id)
if i >=10:
break
return id
def get_id2():
res = getHTMLText2(url)
time.sleep(1)
id2 = re.findall("CONTENT_TID='(.*?)'", res)
i = 0
while id2 == []:
print("id2为空")
time.sleep(5)
res = getHTMLText2(url)
id2 = re.findall("CONTENT_TID='(.*?)'", res)
i += 1
print(i, id2)
if i >= 10:
break
return id2
def main():
id = get_id()
print(id)
time.sleep(1700)
id2 = get_id2()
print(id2)
if id == id2:
print("暂无帖子更新")
else:
push()
print("爷,您关注的帖子更新啦!")
if __name__ == "__main__":
main()
|