请问这个urllib2 python代码下载文件的页面吗?

时间:2009-12-13 08:54:20

标签: python http urllib2

urllib2.urlopen(theurl).read() ...这会下载文件。

urllib2.urlopen(theurl).geturl() ...这会下载文件吗? (需要多长时间)

5 个答案:

答案 0 :(得分:5)

来自documentation

  

geturl()方法返回实数   页面的URL。在某些情况下,   HTTP服务器将客户端重定向到   另一个网址。 urlopen()函数   处理这个透明,但在   有些情况下,来电者需要知道   客户端被重定向到哪个URL   至。 geturl()方法可用于   获取此重定向的网址。

答案 1 :(得分:4)

使用Wireshark和Python 2.5进行测试:urllib2.urlopen(theurl).geturl()下载正文的部分。它发出GET,读取标题和身体的几个K,然后停止。

答案 2 :(得分:3)

没有。 对我来说,在google.com上进行测试:

x= time.time(); urllib2.urlopen("http://www.google.com").read(); print time.time()-x
0.166881084442

x= time.time(); urllib2.urlopen("http://www.google.com").geturl(); print time.time()-x
0.0772399902344

答案 3 :(得分:2)

urllib2.urlopen()返回一个像object这样的文件,这样当你使用urlopen()实际上是下载文件,并将它加载到你机器的内存中时,你可以使用文件函数来读取你的文件,就像这样。 ..

#将python.org存储到本地文件d:\ python.org.html

from urllib2 import urlopen
doc = urlopen("http://www.python.org")
html=doc.read( )
f=open("d:/python.org.html","w+")
f.write(html)
f.close()

或仅使用urllib

import urllib
urllib.urlretrieve("http://www.python.org","d:/python.org.html")

希望有所帮助;)

答案 4 :(得分:1)

没有。 geturl()返回url。

例如; urllib2.urlopen("http://www.python.org").geturl()会返回字符串“http://www.python.org”。

你可以在python交互式shell中轻松找到这类东西,例如

$ python
Python 2.4.3 (#1, Jul 27 2009, 17:57:39)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> u = urllib2.urlopen("http://www.python.org")
>>> u.geturl()
'http://www.python.org'
>>>
相关问题