我可能做错了什么?尝试从此特定站点中刮取数据时发生TimeoutError?

2024-04-19 23:09:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从“www.money.usnews.com”中获取数据,但我始终得到这样的时间错误,即:

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because connected host has failed to
respond.

这是我的代码:(我包括了一个不同的网站作为测试用例,它是有效的。我是新的网页抓取。我应该怎么做

from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.error import URLError
from bs4 import BeautifulSoup


try:
    html = urlopen('https://money.usnews.com')
    # html = urlopen('http://www.timeanddate.com/weather/usa/los-angeles')
except HTTPError as e:
    print(e)
except URLError as e:
    print("No server")
except ValueError as e:
    print("NULL ")
else:
    print("b")
    bs = BeautifulSoup(html.read(), 'html.parser')
    print(bs.h1)