kaixin001.com有一个摆地摊的小应用,我时常在玩。摆地摊的钱太少,老实摆摊的摊主没有倒卖的黄牛赚钱。今晚闲的发慌,写了一段用以淘黑老大收购物品的Python程序,可以把满足条件的货物的卖家url搜出来,并以卖价从低到高把结果排列出来。我试了一下,还不错,如果搜出来结果的url提示用户还没有加入本程序,那是该摆地摊的问题,不是程序的问题。
贴一下源代码,使用说明见注释。
#! /usr/bin/env python
#coding=utf-8
#author = yyobin@gmail.com
import re,urllib, urllib2, cookielib
def Kaixin(user,psw,qurl,wupin,price):
result = {}
signed_in = False
#下面这一段是登录kaixin001.com的代码
cj = cookielib.LWPCookieJar()
try:
cj.revert('kaixin.coockie')
except:
print 'except'
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
r = opener.open('http://www.kaixin001.com/home/')
if r.geturl() == 'http://www.kaixin001.com/?flag=1&url=%2Fhome%2F':
print 'Need logging on!'
params = {'email':user, 'password':psw, 'remember':1}
req = urllib2.Request(
'http://www.kaixin001.com/login/login.php',
urllib.urlencode(params)
)
r = opener.open(req)
newurl = r.geturl()
print newurl
if 'http://www.kaixin001.com/home/' in newurl:
print 'Logged on successfully!'
cj.save('kaixin.coockie')
signed_in = True
else:
print 'Logged on failed'
else:
print 'still logged on!'
signed_in = True
#下面的代码是查找自己想倒卖的物品在哪里的
if signed_in:
restr = r'''<p class="txt">%s<br />售价:(\d+)元</p></dd>\n.*?<a class="gothis" href="/!stall/index[.]php[?]fuid=(\d+)">去逛逛</a>''' % (wupin)
reobj = re.compile(restr, re.DOTALL)
#默认搜50个页面就好了
for loop in xrange(50):
desturl = '%s&start=%d' % (qurl,loop*8)
req2 = urllib2.Request(desturl)
content = urllib2.urlopen(req2).read()
matched_objs = reobj.findall(content)
for obj in matched_objs:
if int(obj[0]) < price:
#print int(obj[0])
result[obj[0]] = obj
if result:
#排序一下搜索结果
for pr in sorted(result.items()):
print 'price = %s, url= http://www.kaixin001.com/!stall/index.php?fuid=%s' % (pr[1][0],pr[1][1])
else:
print 'no proper goods'
def main():
print 'gogogo!'
#填写自己的开心网用户名和密码
user = 'your account'
psw = 'your password'
#需要查询的url,点击倒卖,鼠标移往想倒卖物品后会提示前往下列地区淘货,
#鼠标点击一个地区,然后浏览器会出来一个url,填入下方
qurl = 'http://www.kaixin001.com/!stall/stalllist.php?districtid=19&classid=12'
#需要倒卖的物品名称
wupin = '竹蜻蜓'
#需要找的最高价格,由于可以砍价的,所以自己设定的价格要高一点
price = 92 #* 1.3
Kaixin(user,psw,qurl,wupin,price)
print 'The end.'
if __name__ == "__main__":
main()
...