|
|
|
|
 python中的pycurl模块的学习(转) - solaris7 [ 2007-02-26 15:56 | 3,994 byte(s)]
 Re: python中的pycurl模块的学习(转) - chumpklutz [ 2007-03-01 17:07 | 533 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
python中的pycurl模块的学习 (PT007)
1、使用getinfo来获得更多的信息:
#! /usr/bin/env python
# vi:ts=4:et
# $Id: test_getinfo.py,v 1.18 2003/05/01 19:35:01 mfx Exp $
# Author BY MSN:pt007@vip.sina.com
import time
import pycurl
## Callback function invoked when progress information is updated
#下面的函数用来显示下载的进度:
def progress(download_t, download_d, upload_t, upload_d):
print "Total to download %d bytes, have %d bytes so far" %
(download_t, download_d)
url = "http://www.sohu.com/index.html"
print "Starting downloading", url
print
f = open("body.html", "wb") #新建一个文件并返回文件描述字,f用来保存返回的网页内容
h = open("header.txt", "wb")#h用来保存返回的包头header信息
i = open("info.txt","wb") #i用来保存getinfo()函数取回的信息
c = pycurl.Curl()
c.setopt(c.URL, url) #设置要访问的网址
c.setopt(c.WRITEDATA, f) #将返回的网页内容写入f文件描述字
c.setopt(c.NOPROGRESS, 0)
c.setopt(c.PROGRESSFUNCTION, progress)#调用过程函数
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.MAXREDIRS, 5)
c.setopt(c.WRITEHEADER, h)#将返回的包头header内容写入h文件描述字
c.setopt(c.OPT_FILETIME, 1)
c.perform() #执行上述访问网址的操作
print
print "HTTP-code:", c.getinfo(c.HTTP_CODE) #Outputs:200
buf=c.getinfo(c.HTTP_CODE)
i.write("HTTP-code:"+str(buf)) #将输出写入到i文件描述字中
print "Total-time:", c.getinfo(c.TOTAL_TIME) #下载总时间:0.795
buf=c.getinfo(c.TOTAL_TIME)
i.write('
')
i.write("Total-time:"+str(buf))
print "Download speed: %.2f bytes/second" % c.getinfo(c.SPEED_DOWNLOAD) #下载速度:261032.00 bytes/second
print "Document size: %d bytes" % c.getinfo(c.SIZE_DOWNLOAD) #下载文档的大小:207521 bytes
print "Effective URL:", c.getinfo(c.EFFECTIVE_URL) #有效网址:http://www.sohu.com/index.html
print "Content-type:", c.getinfo(c.CONTENT_TYPE) #text/html
print "Namelookup-time:", c.getinfo(c.NAMELOOKUP_TIME) #DNS解析速度:0.065
print "Redirect-time:", c.getinfo(c.REDIRECT_TIME) #0.0
print "Redirect-count:", c.getinfo(c.REDIRECT_COUNT) #0
epoch = c.getinfo(c.INFO_FILETIME)
print "Filetime: %d (%s)" % (epoch, time.ctime(epoch)) #文件下载时间:1172361818 (Sun Feb 25 08:03:38 2007)
print
print "Header is in file 'header.txt', body is in file 'body.html'"
c.close()
f.close()
h.close()
2、简单用法:
#!c:python25python
# vi:ts=4:et
# $Id: test_cb.py,v 1.14 2003/04/21 18:46:10 mfx Exp $
# Author BY MSN:pt007@vip.sina.com
import sys
import pycurl
## Callback function invoked when body data is ready
def body(buf):
# Print body data to stdout
sys.stdout.write(buf) #将buf的内容输出到标准输出
## Callback function invoked when header data is ready
def header(buf):
# Print header data to stderr
sys.stdout.write(buf)
c = pycurl.Curl()
c.setopt(pycurl.URL, 'http://www.sohu.com/') #设置要访问的网址
c.setopt(pycurl.WRITEFUNCTION, body) #调用body()函数来输出返回的信息
c.setopt(pycurl.HEADERFUNCTION, header)#调用header()函数来输出返回的信息
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.MAXREDIRS, 5)
c.perform() #执行上述访问网址的操作
c.close()
|
|
|
----
像你一样,骄傲,不驯,而且敏捷
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
pycurl如何安装
我在官方网站上下载了pycurl
然后执行文件夹里的setup.py文件,提示如下信息:
Using curl directory: c:srcuildpycurlcurl-7.15.0
Traceback (most recent call last):
File "E:MySoftPythonpycurl-7.15.5.1pycurl-7.15.5.1setup.py", line 69, in
<module>
assert os.path.isdir(CURL_DIR), "please check CURL_DIR in setup.py"
AssertionError: please check CURL_DIR in setup.py
这是为何啊
|
|
|
[Original]
[Print]
[Top]
|
|
|