And*_*rei 7 python python-3.x python-requests
我在 Python 中尝试了以下代码
url="http://www.realtor.com/realestateandhomes-search/Pittsburgh_PA/type-single-family-home/price-na-30000/sby-1/"
r=requests.get(url)
Run Code Online (Sandbox Code Playgroud)
但它抛出了错误
File "C:\Users\Dana\AppData\Local\Programs\Python\Python35-32\lib\site-package
s\requests\sessions.py", line 630, in send
history = [resp for resp in gen] if allow_redirects else []
File "C:\Users\Dana\AppData\Local\Programs\Python\Python35-32\lib\site-package
s\requests\sessions.py", line 630, in <listcomp>
history = [resp for resp in gen] if allow_redirects else []
File "C:\Users\Dana\AppData\Local\Programs\Python\Python35-32\lib\site-package
s\requests\sessions.py", line 111, in resolve_redirects
raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects, respon
se=resp)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激
Mii*_*ind 22
截至 2022 年 6 月 10 日,如果您在此处,请更新:
看来 PyPi 存在问题,这是问题的根本原因
来源: https: //status.python.org/
这只是意味着您的请求得到了一个重定向响应(您试图访问的页面现在位于新位置的信息)。该requests图书馆明白这一点每默认,不返回该结果,但尝试新位置另一个请求。再次返回重定向等。
为避免永远退出requests呼叫,在进程中止之前允许的重定向数量实施了限制。
我假设您尝试从中请求某些内容的站点上存在错误,可能是循环重定向。
您可以调整requests库以不遵循重定向而是返回它们,然后您将不会收到此错误(但当然重定向响应):
response = requests.get(url, allow_redirects=False)
Run Code Online (Sandbox Code Playgroud)
偶尔,不经常,如果您不包含服务器期望的标头,就会发生这种情况。如果您使用requests .get() 中可用的其他选项模拟标头、有效负载、用户代理等,则不太可能出现此错误。
例子:
import requests
headers = {
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US,en;q=0.8',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
}
requests.get('http://www.realtor.com/realestateandhomes-search/Pittsburgh_PA/type-single-family-home/price-na-30000/sby-1', headers=headers)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17327 次 |
| 最近记录: |