我在python中创建了一个刮板来从网页获取不同的批号。但是,当我运行我的刮板时,我在控制台中看到“请求的URL无效”。我试图获得响应网址,并发现它是有效的。在处理请求时是否有任何错误? 我,试图脚本:刮板抛出无效的网址错误
import requests
from lxml import html
payload = {"keyword":"degas"}
headers={
"Content-Type":"text/html; charset=UTF-8",
"User-Agent":"Mozilla/5.0"
}
response = requests.get("http://www.sothebys.com/en/search-results.html?", params=payload, headers=headers, allow_redirects=False)
# tree = html.fromstring(response.text)
# for item in tree.cssselect("div.search-results-lot-number"):
# print(item.text)
print(response.url)
print(response.text)
print(response.status_code)
这是我在印刷时 “response.url”, “response.text” 控制台获得,而 “response.status_code”:
http://www.sothebys.com/en/search-results.html?keyword=degas
<HTML><HEAD>
<TITLE>Invalid URL</TITLE>
</HEAD><BODY>
<H1>Invalid URL</H1>
The requested URL "[no URL]", is invalid.<p>
Reference #9.541d2017.1503578560.40be2bd
</BODY></HTML>
400
顺便说一句,如果我手动检查URL,然后我发现它确实将我带到了所需的页面。
您拥有的网址将重定向到“未找到”页面。 http://www.sothebys.com/en/notfound.html – Mekicha
由于您的请求中包含'allow_redirects = False',它只会引发错误。 – Mekicha
@ Mekicha,“allow_redirects = True”给了我与使用False参数相同的结果。 – SIM