gpt4 book ai didi

python - 用 Python 抓取 Edmunds.com 网站时如何处理读取超时错误?

转载 作者:行者123 更新时间:2023-12-03 07:56:40 25 4
gpt4 key购买 nike

我正在尝试网络抓取的初学者,试图从 https://www.edmunds.com/ 抓取客户评论为了我的研究。

然而,即使是基本代码也只给出了读取超时错误。

import requests 
from bs4 import BeautifulSoup
result = requests.get("https://www.edmunds.com/")
print(result.status_code)

你能帮忙吗?

最佳答案

使用requests_html或添加 User-Agent到标题

from requests_html import HTMLSession
session = HTMLSession()
url = session.get('https://www.edmunds.com/')

try:
status = url.status_code
print(status)
except Exception as e:
print(e)
import requests

headers = {
"User-Agent": "Mozilla/5.0"
}
result = requests.get(url="https://www.edmunds.com", headers=headers)

try:
status = result.status_code
print(status)
except Exception as e:
print(e)

在开始抓取之前,请参阅 https://www.edmunds.com/robots.txt

关于python - 用 Python 抓取 Edmunds.com 网站时如何处理读取超时错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61827618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com