我正在尝试使用 python 3 抓取以下页面,但我不断收到HTTP Error 400: Bad Request
. 我查看了之前建议使用的一些答案urllib.quote
,因为它是 python 2,因此对我不起作用。此外,我按照另一篇文章的建议尝试了以下代码,但仍然无效。
url = requote_uri('http://www.txhighereddata.org/Interactive/CIP/CIPGroup.cfm?GroupCode=01')
with urllib.request.urlopen(url) as response:
html = response.read()
服务器拒绝来自非类人User-Agent
HTTP 标头的查询。
只需选择浏览器的 User-Agent 字符串并将其设置为查询的标头:
import urllib.request
url = 'http://www.txhighereddata.org/Interactive/CIP/CIPGroup.cfm?GroupCode=01'
headers={
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0"
}
request = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(request) as response:
html = response.read()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句