我如何使用Python读取URL的内容?
当我把它粘贴到浏览器上时,下面的工作是正确的
http://www.somesite.com/details.pl?urn=2344 但是,当我尝试用Python读取URL时,什么也没有发生:
  link = 'http://www.somesite.com/details.pl?urn=2344' f = urllib.urlopen(link) myfile = f.readline() print myfile 
我是否需要对url进行编码,或者有什么我没有看到?
回答你的问题:
 import urllib link = "http://www.somesite.com/details.pl?urn=2344" f = urllib.urlopen(link) myfile = f.read() print myfile 
 你需要read() ,而不是readline() 
或者,只需要在这里得到这个库: http : //docs.python-requests.org/en/latest/并认真使用它:)
 import requests link = "http://www.somesite.com/details.pl?urn=2344" f = requests.get(link) print f.text 
 与Python 2.X和Python 3.X一起工作的解决scheme使用Python 2和3兼容性库six : 
 from six.moves.urllib.request import urlopen link = "http://www.somesite.com/details.pl?urn=2344" response = urlopen(link) content = response.read() print(content) 
我使用了下面的代码:
 import urllib def read_text(): quotes = urllib.urlopen("https://s3.amazonaws.com/udacity-hosted-downloads/ud036/movie_quotes.txt") contents_file = quotes.read() print contents_file read_text() 
 对于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 ,但认为这可能会节省时间。 
该url应该是一个string:
 import urllib link = "http://www.somesite.com/details.pl?urn=2344" f = urllib.urlopen(link) myfile = f.readline() print myfile