Python小应用之火车路线查询

代码
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、解析,输出

 

拿过去就可以用,输出已经做过处理 

 

posted @ 2010-04-26 15:12  飘啊飘  阅读(531)  评论(0编辑  收藏  举报