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)
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)
相关文章推荐
- 理解神经网络,从简单的例子开始(2)使用python建立多层神经网络
- [转] 最简单的使用UDP通信的Python Socket例子
- python 使用 talib 和 numpy 的简单例子
- python使用百度进行爬虫简单学习例子
- python中使用状态机的简单例子---截取'/x00'之前的字符串
- 简单使用scipy.weave混合使用Python和C++代码的简单例子
- 如何使用Eclipse最简单地配置其他语言的IDE环境:例子-Python
- vb2005 创建和使用线程的简单例子
- 使用 CXF 做 webservice 简单例子
- Python使用tablib生成excel文件的简单实现方法
- Python 网络爬虫简单例子
- 简单地使用jxl进行操作excel例子
- Python中subprocess的简单使用示例
- 在Python中使用SQLite的简单教程
- Ireport 连接数据库 javabean 使用参数的简单例子
- java爬取网页内容 简单例子(1)——使用正则表达式
- Python使用新浪微博API发送微博的例子
- 使用ANTLR做一个简单的Python SQL语法解析器 - 推酷
- 使用 CXF 做 webservice 简单例子
- Python 安转与简单的使用