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

Python小应用之火车路线查询

2010-04-26 15:12 316 查看
代码def getrain(f,t):
"""获取火车路线,参数是from和to"""
url ="http://wap.huochepiao.com/cx/"

form = urllib.urlencode([('chufa',f),('daoda',t)])

req = urllib2.Request(url)

fd = urllib2.urlopen(req,form)
lines = fd.readlines()
cars = str(lines[16])
cars = cars.replace("<b>","")
cars = cars.replace("</b>","")
cars = cars.replace("<br />","")
print cars.decode("utf-8")
lines = lines[20:-6]
tmp = ""
for l in lines:
tmp = tmp + l
tmp = tmp.replace("<br />","")
tmp = tmp.replace("<li>","")
tmp2 = tmp.split("\r\n\r\n")
tmp3 = []
for i in tmp2:
i = i.strip()
if i == "":
continue
pos1 = i.rfind("=") + 1
pos2 = i.rfind('"')
cc = "车次:%s" % i[pos1:pos2]
i = i[i.find("\n",2)+1:]
i = cc + "\n" + i + "\r\n"
tmp3.append(i)
end = ""
for s in tmp3:
end = end + s + "\n"
end = cars + end + "\n"
return end

拿过去就可以用, 前面需要:

import string

import urllib

import urllib2

import sys

主要思路:

1、就是找个wap查询火车的网站,发现还不错

2、抓包,看到post的数据

3、然后在模拟提交,下载

4、解析,输出

拿过去就可以用,输出已经做过处理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: