您的位置:首页 > 编程语言 > Python开发

libcurl的使用简单例子(python)

2010-01-09 18:18 741 查看
libcurl的使用简单例子(python)
3     关于libcurl
4     libcurl is a free and easy-to-use client-side URL transfer library, supporting FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS and FILE. libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos4), file transfer resume, http proxy tunneling and more!
5     homepape:           http://curl.haxx.se/libcurl
6     document:           http://curl.haxx.se/libcurl/c/
7     Python Binding:     http://curl.haxx.se/libcurl/python/
8
9
10     一个无聊的例子:-)  
11     查询开心网上超级大亨物品当前物品价格(PS:一个超级无聊的游戏).  
12
13 #!/bin/env python
14 #coding=utf8
15
16 import pycurl
17 import urllib
18 import cStringIO
19 import re
20 import time
21
22     kaixin_host = 'http://www.kaixin001.com'
23     user_agent=r'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1) Gecko/20090628 Firefox/3.5rc3'
24
25 def login(user_name, psw):
26 # 新Curl对象.  
27         c = pycurl.Curl()
28 # 存放结果用
29         f = cStringIO.StringIO()
30 # 设置保存数据使用的函数
31         c.setopt(c.WRITEFUNCTION, f.write)
32 # 设定URL
33         c.setopt(c.URL, 'http://www.kaixin001.com/login/login.php')
34 # 处理Post数据
35         login_info = {'email': user_name, 'password' : psw}
36         c.setopt(c.POSTFIELDS,urllib.urlencode(login_info))
37 # 处理cookie
38         c.setopt(c.COOKIEJAR, "_login.txt")
39 # 设定user-agent. 有些服务器会检查这个值.
40         c.setopt(c.USERAGENT, user_agent)
41 ## 设置代理
42 #c.setopt(c.PROXY, "http://...@..:1234")
43 # do it!~~
44         c.perform()
45 return f.getvalue()
46
47 ###
48 ## 通过物品ID 查询当前价格. 比如说红薯的ID是1
49 def get_current_value(id):
50         c = pycurl.Curl()
51         f = cStringIO.StringIO()
52         c.setopt(c.WRITEFUNCTION, f.write)
53         c.setopt(c.URL, kaixin_host + '/!rich/!api_item_price.php?rt=xml&iid=%d'%id)
54 ## 在向服务器请求数据时, 要包含Cookie信息.
55         c.setopt(c.COOKIEFILE, "_login.txt")
56         c.setopt(c.USERAGENT, user_agent)
57         c.perform()
58         res = f.getvalue()
59
60
61 ## 结果是xml数据. 解析便可得大约12个小时内所有价格.
62 if len(res) != 0:
63 ## 简便起见. 只取第一个值,即当前价格.
64             p = re.compile('<price>(\d+)</price>')
65             x = p.search(res)
66 if x:
67 ## 打印debug info.
68 print "%15s ==> %s" % (id, x.group(1))
69 return x.group(1)
70 else:
71 raise "data error!"
72 else:
73 raise "get failed!"
74
75 ## Test
76 if __name__=="__main__":
77 ##
78         login('test@xxx.com', 'xxxx')
79 for p in range(1, 10):
80             get_current_value(p)
81             time.sleep(0.5)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: