首页 > 解决方案 > 我得到 ConnectionResetError: [Errno 54] Connection reset by peer 在尝试抓取时

问题描述

任何人都可以帮助我吗?我在尝试使用 BeautifulSoup 抓取时遇到这些错误,

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq

myUrl = "https://www.tokopedia.com/discovery/produk-terlaris?source=homepage.top_carousel.0.38454"
#open the connection
uClient = uReq(myUrl)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html,"html.parser")

product = page_soup.findAll("div", {"class": "css-6bc98m e1uv83qc1"})
print(len(product))

这就是错误

Traceback (most recent call last):
 ....
 ....
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 911, in read
    return self._sslobj.read(len, buffer)
ConnectionResetError: [Errno 54] Connection reset by peer

标签: python-3.xbeautifulsoup

解决方案


首先,您需要User-Agent标头,否则服务器(正确)认为您是机器人。

第二件事是你不会从那个网站得到任何东西,因为几乎所有的内容都在后面JS(JavaScript),这基本上意味着BeautifulSoup看不到它。

我已经修复了您的代码,因此不再有错误,但是,正如我所说,您在HTML返回时没有任何 div。

import requests
from bs4 import BeautifulSoup

my_url = "https://www.tokopedia.com/discovery/produk-terlaris?source=homepage.top_carousel.0.38454"
headers = {
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36",
}
page_soup = BeautifulSoup(requests.get(my_url, headers=headers).text, "html.parser")

product = page_soup.findAll("div", {"class": "css-6bc98m e1uv83qc1"})
print(len(product))

这打印0

您可以做的是探索selenium或检查流量,看看是否有 API 端点暴露。


推荐阅读