2024-04-24 04:38:16 发布
网友
我正试图通过以下方法获取页面的源代码:
import urllib2 url="http://france.meteofrance.com/france/meteo?PREVISIONS_PORTLET.path=previsionsville/750560" page =urllib2.urlopen(url) data=page.read() print data
也可以使用user_agent(headers) 我没有成功获取该页的源代码!
user_agent(headers)
你们有什么办法吗? 提前谢谢
如Martin Maillard所示,requests库为我工作。
requests
另外,在另一个线程中,我注意到这个注释是leolukhere:
Edit: It's 2014 now, and most of the important libraries have been ported and you should definitely use Python 3 if you can. python-requests is a very nice high-level library which is easier to use than urllib2.
所以我写了一个获取页面的过程:
import requests def get_page (website_url): response = requests.get(website_url) return response.content print get_page('http://example.com')
干杯!
我尝试了很多东西,“urllib”“urllib2”和其他很多东西,但有一件事对我来说对我所需要的一切都有用,解决了我所面临的任何问题。它是Mechanize。这个库模拟使用一个真正的浏览器,所以它处理了这个领域的许多问题。
我试过了,请求也有效,但是你收到的内容说你的浏览器必须接受cookies(法语)。你也许可以用urllib2来解决这个问题,但我认为最简单的方法是使用requests库(如果你不介意有额外的依赖关系的话)。
urllib2
要安装requests:
pip install requests
然后在你的剧本里:
import requests url = 'http://france.meteofrance.com/france/meteo?PREVISIONS_PORTLET.path=previsionsville/750560' response = requests.get(url) print(response.content)
我很确定这个页面的源代码将是你所期望的。
如Martin Maillard所示,
requests
库为我工作。另外,在另一个线程中,我注意到这个注释是leolukhere:
所以我写了一个获取页面的过程:
干杯!
我尝试了很多东西,“urllib”“urllib2”和其他很多东西,但有一件事对我来说对我所需要的一切都有用,解决了我所面临的任何问题。它是Mechanize。这个库模拟使用一个真正的浏览器,所以它处理了这个领域的许多问题。
我试过了,请求也有效,但是你收到的内容说你的浏览器必须接受cookies(法语)。你也许可以用
urllib2
来解决这个问题,但我认为最简单的方法是使用requests
库(如果你不介意有额外的依赖关系的话)。要安装
requests
:然后在你的剧本里:
我很确定这个页面的源代码将是你所期望的。
相关问题 更多 >
编程相关推荐