本帖最后由 flyps 于 2020-3-6 12:49 编辑 import pandas as pd
import numpy as np
import time
import platform,datetime
import urllib.request
import urllib.error
import winsound
import json
url = 'http://www.fishC.com/xxoo'
while True:
print('获取数据中...')
try:
req = urllib.request.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36')
response = urllib.request.urlopen(req)
print('ok')
except Exception as e:
print(e.reason)
time.sleep(10)
continue
html = response.read().decode('utf-8')
data_number = json.loads(html)['data']['newest']['array']
data_number = list(map(int, data_number))
number_old = list(df.loc[0])[2:]
if data_number != number_old:
break
print('未更新,1000秒后重新获取')
time.sleep(1000)
Traceback (most recent call last):
File "C:\Py\G_new04.py", line 36, in <module>
html = response.read().decode('utf-8')
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 467, in read
s = self._safe_read(self.length)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 608, in _safe_read
data = self.fp.read(amt)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\socket.py", line 669, in readinto
return self._sock.recv_into(b)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
html = response.read().decode('utf-8')
这一行报错的,为什么read()会出现TimeoutError的报错,read方法需要连接主机吗?
谢谢 |