我试图从'https://nhandan.vn/'中抓取新闻数据,但我在我的代码中遇到了 WinError10060,我已经在我的代码中添加了 sleep(1) 但这个错误没有改变。请帮助我(我是初学者)。谢谢我的代码:
cte = []
news_links = []
url = newspaper.build('https://nhandan.vn/')
for category in url.category_urls():
cte.append(category)
for i in cte:
url = i
session = HTMLSession()
r = session.get(url)
articles = r.html.find('article')
for item in articles:
try:
newsitem = item.find('a',first = True)
title_no = newsitem.text
link = newsitem.absolute_links
news_links.append(link)
except:
pass
for i in range(len(news_links)):
news_links[i] = str(news_links[i])
news_links[i] = news_links[i][2:-2]
text1 = []
title1 = []
start = []
for i in range(len(news_links)):
if news_links[i] == 't':
start.append(i)
for i in news_links:
url = i
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
cards = soup.find_all('p')
cards1 = soup.find_all('h1')
我的错误:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Do Anh Luyen\Documents\code\python\summary.py", line 18, in <module>
r = session.get(url)
File "C:\Users\Do Anh Luyen\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 555, in get
return self.request('GET', url, **kwargs)
File "C:\Users\Do Anh Luyen\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Do Anh Luyen\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Do Anh Luyen\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='nhandan.vn', port=443): Max retries exceeded with url: /giaoduc (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001C8B4BE58B0>: Failed to establish a new connection: [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'))