我正在尝试打开这个网址:
http://www.leboncoin.fr/montres_bijoux/671762293.htm
import requests
s = requests.Session()
s.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36'
s.headers['Host'] = 'www.leboncoin.fr'
url = 'http://www.leboncoin.fr/montres_bijoux/671762293.htm'
r = s.get(url)
print r.text
当我运行这个脚本时,我的终端显示了这个错误。
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /montres_bijoux/671762293.htm was not found on this server.</p>
</body></html>
虽然我可以在浏览器中打开相同的URL并查看内容,但为什么无法在其他地方打开呢?
可能出现了什么问题?
url
的值吗?你想确保它是完整的URL(例如http://domain.com/asdasd),而不仅仅是路径部分。 - okobokotest.py
,并将相同的代码放入其中,然后执行该文件作为python test.py
。 - user3810188