如何使用Python读取URL的内容?

问题描述:

下面的作品,当我将其粘贴在浏览器上:如何使用Python读取URL的内容?

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

但是当我尝试读取URL使用Python什么也没有发生:

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 
+0

感谢我改成了阅读(),而该诀窍 – 2013-02-28 17:14:34

+0

@HelenNeely享受你的编程旅程 – woozyking 2013-02-28 17:51:11

+0

感谢链接到'请求'库 - 像抽象那里 – 2015-02-04 13:29:43

URL应该是一个字符串:

import urllib 

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

既有“和”在Python字符串 – Leons 2015-07-25 13:01:32

用与Python 2.X和Python 3.X作品的溶液利用了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) 

我知道有DIF不明确的线程错误:Name Error: urlopen is not defined,但认为这可能会节省时间。

我们可以阅读网站的HTML内容如下:。

from urllib.request import urlopen 
response = urlopen('http://google.com/') 
html = response.read() 
print(html)