ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host - getting this error

馋奶兔 提交于 2021-01-29 16:35:54

问题


ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

I am getting this error while reading a webpage in the following code from urllib.request import urlopen as uReq

from bs4 import BeautifulSoup as soup

myurl = 'https://www.amazon.in/s?k=graphics+card&ref=nb_sb_noss_2'

uClient =uReq(myurl)

回答1:


passing a useragent header seems to solve the issue.

try something like this:

from urllib.request import urlopen as uReq, Request

from bs4 import BeautifulSoup as soup

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3"}

my_url = 'https://www.amazon.in/s?k=graphics+card&ref=nb_sb_noss_2'

uClient = uReq(Request(url=my_url, headers=headers))

But do know that if the data you're trying to scrap is dynamic, bs4 wouldn't be of much help. consider using pyppeteer or selenium, etc.. for that.



来源:https://stackoverflow.com/questions/62462946/connectionreseterror-winerror-10054-an-existing-connection-was-forcibly-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!