我有一个脚本,该脚本每30秒抓取一次网页,但是该脚本会停止运行而不会出现任何错误。
我试图在整个代码中打印它,当它打印出params示例时,它似乎总是停下来:“(('mons','443'),('time','1561432569234'), ('since','1561432504'))“如此看来,它永远都不会去执行request.get,或者恰恰是它停止的地方。不确定是什么原因使脚本停止运行了大约2小时左右,或者它们可以随机停止。
import requests, time, json
from playsound import playsound
from datetime import datetime
last_time = 0
while True:
# get current unix time
ctime = int(time.time()*1000.0) #convert it to milliseconds since the make always submits epoch in ms
now = datetime.now()
headers = {
#'Accept-Encoding': 'gzip, deflate, br', ### removed and now returns unzipped response. Much easier to process
'Accept-Language': 'pt-PT,pt;q=0.9,en-FI;q=0.8,en;q=0.7,en-US;q=0.6',
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Mobile Safari/537.36',
'Accept': '*/*',
'Referer': 'https://nycpokemap.com/',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
}
params = (
('mons', '443'),
('time', str(ctime)),
('since', str(last_time)),
)
#print(params)
response = requests.get('https://nycpokemap.com/query2.php', headers=headers, params=params)
# convert response text to json
json_data = json.loads(response.text)
if 'pokemons' in json_data.keys():
if len(json_data["pokemons"]) != 0: #not empty.. Probably spaghetti code but works
pokemons = json_data["pokemons"]
for i in range(len(pokemons)):
print("[x] Gible Found #" + str(i) + " Found at: " + str(now.hour) + ":" + str(now.minute) + ":" + str(now.second))
print("[x] Coordinates : "+ str(pokemons[i].get("lat")) +","+str(pokemons[i].get("lng")))
print("[x] CP: " + str(pokemons[i].get("cp") + " Attack: " +str(pokemons[i].get("attack"))+ " Defence: " +str(pokemons[i].get("defence"))+ " HP: " +str(pokemons[i].get("stamina"))+ " Level: " +str(pokemons[i].get("level"))))
print("-------------")
playsound('audio.mp3')
last_time = int(time.time()-35) #get the last 35 seconds in seconds
time.sleep(30)
期望脚本可以无限运行,因为代码中没有中断。