libcurl的使用简单例子(python)

本文介绍了一个使用libcurl的Python示例程序,展示了如何利用libcurl进行网页登录并获取指定物品的价格信息。示例中详细解释了如何设置Curl对象、处理POST数据、管理Cookies等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

转载于:https://www.cnblogs.com/changyou/archive/2010/01/09/1643066.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值