flyps 发表于 2020-3-6 11:25:25

为什么read()会出现TimeoutError的报错

本帖最后由 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)
    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: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。


html = response.read().decode('utf-8')
这一行报错的,为什么read()会出现TimeoutError的报错,read方法需要连接主机吗?
谢谢

zltzlt 发表于 2020-3-6 11:48:37

网页连接超时

把你的完整代码发上来

flyps 发表于 2020-3-6 12:30:41

zltzlt 发表于 2020-3-6 11:48
网页连接超时

把你的完整代码发上来

谢谢回复,知道是连接超时,但read()方法,不应该连接超的

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)
    if data_number != number_old:
      break
    print('未更新,1000秒后重新获取')
    time.sleep(1000)
            

陈尚涵 发表于 2020-3-12 12:32:48

网太慢了,到一个网好的地方试试

wp231957 发表于 2020-3-12 18:31:46

http://www.fishC.com/xxoo这个网址存在吗   你用浏览器能打开吗

钙硼钡锗 发表于 2020-3-17 05:16:57

网页内容太多了,读不过来

一个账号 发表于 2020-3-17 17:00:44

flyps 发表于 2020-3-6 12:30
谢谢回复,知道是连接超时,但read()方法,不应该连接超的

根本没有这个 Web 地址:http://www.fishc.com/xxoo

zltzlt 发表于 2020-3-17 18:25:11

http://www.fishC.com/xxoo 这个地址不存在

陈尚涵 发表于 2020-3-24 15:20:53

decode不会放在括号里吗?
页: [1]
查看完整版本: 为什么read()会出现TimeoutError的报错