如何使用 Python 读取 URL 的内容? [英] How can I read the contents of an URL with Python?
问题描述
当我将其粘贴到浏览器上时,以下内容有效:
The following works when I paste it on the browser:
http://www.somesite.com/details.pl?urn=2344
但是当我尝试使用 Python 读取 URL 时,没有任何反应:
But when I try reading the URL with Python nothing happens:
link = 'http://www.somesite.com/details.pl?urn=2344'
f = urllib.urlopen(link)
myfile = f.readline()
print myfile
我是否需要对 URL 进行编码,或者有什么我没有看到的内容?
Do I need to encode the URL, or is there something I'm not seeing?
推荐答案
回答您的问题:
import urllib
link = "http://www.somesite.com/details.pl?urn=2344"
f = urllib.urlopen(link)
myfile = f.read()
print(myfile)
你需要read()
,而不是readline()
EDIT (2018-06-25):从 Python 3 开始,遗留的 urllib.urlopen()
被 urllib.request.urlopen()
取代(见注释来自 https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen 详情).
EDIT (2018-06-25): Since Python 3, the legacy urllib.urlopen()
was replaced by urllib.request.urlopen()
(see notes from https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen for details).
如果您使用的是 Python 3,请参阅 Martin Thoma 或 i.n.n.m 在此问题中的回答:https://stackoverflow.com/a/28040508/158111(Python 2/3 兼容)https://stackoverflow.com/a/45886824/158111(Python 3)
If you're using Python 3, see answers by Martin Thoma or i.n.n.m within this question: https://stackoverflow.com/a/28040508/158111 (Python 2/3 compat) https://stackoverflow.com/a/45886824/158111 (Python 3)
或者,只需在此处获取此库:http://docs.python-requests.org/en/latest/ 并认真使用它:)
Or, just get this library here: http://docs.python-requests.org/en/latest/ and seriously use it :)
import requests
link = "http://www.somesite.com/details.pl?urn=2344"
f = requests.get(link)
print(f.text)
这篇关于如何使用 Python 读取 URL 的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!