如何用Python读取URL内容?

129 投票
11 回答
442739 浏览
提问于 2025-04-17 17:27

下面这段代码在我把它粘贴到浏览器里时能正常工作:

http://www.somesite.com/details.pl?urn=2344

但是当我用Python去读取这个网址时却没有任何反应:

 link = 'http://www.somesite.com/details.pl?urn=2344'
 f = urllib.urlopen(link)           
 myfile = f.readline()  
 print myfile

我是不是需要对这个网址进行编码,还是说我漏掉了什么?

11 个回答

21

这些答案对Python 3来说都不是很好(在我发帖时测试的最新版本)。

这就是你该怎么做……

import urllib.request

try:
   with urllib.request.urlopen('http://www.python.org/') as f:
      print(f.read().decode('utf-8'))
except urllib.error.URLError as e:
   print(e.reason)

上面的代码是针对返回'utf-8'内容的。如果你想让Python自己“猜测合适的编码”,可以去掉.decode('utf-8')这部分。

文档链接: https://docs.python.org/3/library/urllib.request.html#module-urllib.request

46

对于使用 python3 的朋友们,为了节省时间,可以使用下面的代码:

from urllib.request import urlopen

link = "https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html"

f = urlopen(link)
myfile = f.read()
print(myfile)

我知道有很多人提到过这个错误:Name Error: urlopen is not defined,但我觉得这可能能帮大家节省一些时间。

205

来回答你的问题:

import urllib.request

link = "http://www.somesite.com/details.pl?urn=2344"
f = urllib.request.urlopen(link)
myfile = f.read()
print(myfile)

你需要用 read(),而不是 readline()

另外,你可以看看Martin Thoma或i.n.n.m在这个问题中的回答: Python 2/3兼容性Python 3

另外,你也可以使用 requests

import requests

link = "http://www.somesite.com/details.pl?urn=2344"
f = requests.get(link)
print(f.text)

撰写回答