Python Search Car ticket

Source: Internet
Author: User
Tags date1 ticket

I. BACKGROUND

Use the requests module to get the page, BeautifulSoup to get what you need, and process and return the results.

Second, the Code

The getinfo.py code is as follows:

#!/bin/env python#-*-coding:utf-8-*-# _author:kaliarchimport reimport requestsfrom bs4 import BeautifulSoupclass Getin Formation:def __init__ (self): "" "Init url" "" Self.init_url = ' Http://www.xaglkp.com/ClassSearch/IndexPos T ' def getsoup (self): "" "": Return Soup "" "Response = Requests.post (self.init_url) soup = beautifuls OUP (Response.text, ' html.parser ') return soup def getstartaddress (self,soup): "" "Get Start Address Dict" " "staraddress = soup.find_all (' option ', Text=re.compile (" \w+ terminal ")) Num_list = [+] Add_list = [" All " ] for num in staraddress:num_list.append (num[' value ') Add_list.append (Num.text) star taddrdict = dict (Zip (add_list, num_list)) return startaddrdict def getstartdate (self,soup): "" "" Get Time ""        "date = soup.find_all (' option ', Text=re.compile (" 2018.*? ")) Date_list = [] for num in Date:date_list.append (num.tEXT) return date_list def getstarttime (self,soup): "" "Get start Time:return start time Li St "" Times = soup.find_all (' option ', Text=re.compile ("\w.*")) time_list = [' after 05:00 '] for n Um in Times:time_list.append (num.text) return time_listif __name__ = = ' __main__ ': test = Getinformat Ion () soup = test.getsoup () add = test.getstartaddress (soup) print (add)
The

main.py code is as follows

#!/bin/env python#-*-coding:utf-8-*-# _author:kaliarchimport getinfoimport requestsfrom bs4 Import BeautifulSoupoper = GetInfo. GetInformation () soup = oper.getsoup () Startdic = oper.getstartaddress (soup) startdate = oper.getstartdate (soup) StartTime = oper.getstarttime (soup) init_url = ' Http://www.xaglkp.com/ClassSearch/IndexPost ' def getsadd (): "" "Get Input start Address "" "Print (" Starting station as follows: ") Print (" * "*) for V in Startdic.keys (): print (v) print (" * "* 2 0) trynum = 0 start = str (Input ("Please enter the place of departure:")) while Trynum < 5:for v in Startdic.keys (): If            Start = = V:return Startdic[start] Else:print ("Input error,%d retry opportunity"% (5-trynum)) start = str ("Please re-enter the place of departure:")) Trynum + = 1def getdes (): "" "Get Input Destination Address" "" Try:des Tadd = str (Input ("Please enter destination:")) result = 1 except Exception as E:print ("Input error:", e) if Result:retu RN Destadddef getDate (): "" "GetInput start Date "" "Print (" Departure date as follows: ") Print (" * "*) for date in Startdate:print (date) print (" * "* 20)  Trynum = 0 date1 = str (Input ("Please enter departure time:")) while Trynum < 5:for i in startdate:if date1 = = I:return date1 else:print ("Input error,%d retry opportunity"% (5-trynum)) date1 = str (Input ( "Please enter departure time from:")) Trynum + = 1def getTime (): "" "Get Input start Time" "" Print ("Out of the Car" * * (05:00 = ALL) * *: ") print ( "*" *) for the time in Starttime:print ("*") Print ("*" *20) trynum = 0 time1 = str (Input ("Please enter the departure times (05:00 table Trynum < 5:for i in starttime:if time1 = = I:return Time1.split (' rear        ') [0] Else:print ("Input error, also%d retry opportunity"% (5-trynum)) time1 = str (Input ("Please enter departure time (05:00 for All):")) Trynum + = 1def getpaydata (): "" "Get Requests Paydata" "" Sadd = Getsadd () Desadd = Getdes () sdate = getd Ate () stime = GetTime() Paydata = {' selected ': Sadd, ' Arrive ':d esadd, ' selected1 ': sdate, ' selected2 ': stime, ' Page ': ', ' Arrivehidden ': Sadd,} return Paydatadef GetHeader (): "" "Get Request Headers" "" Header = {' Content-type ': ' application/x-www-form-urlencoded ', ' Host ': ' www.xaglkp.com ', ' Referer ': ' http://www. Xaglkp.com/classsearch/indexpost ', ' user-agent ': ' mozilla/5.0 (Windows NT 10.0; Win64;    x64) applewebkit/537.36 (khtml, like Gecko) chrome/61.0.3163.100 safari/537.36 '} return headerdef get_request (): "" "Requests" "" Header = GetHeader () Paydata = Getpaydata () Select_result = Requests.post (init_url,headers=header     , data=paydata) Result_soup = BeautifulSoup (Select_result.text, ' html.parser ') result = result_soup.find_all (' table ') return Resultdef format_list (): result_l = Get_request () L1 = [] for context in Result_L:L1.append (CO NTEXT.TEXT) tmp = ' \ n '. Join (L1) str = TMP.REPLACE ("\ n", "") L2 = [] for i2 in Str.split (): L2.append (I2) return l2def title_context (): "" "Get title Li    St,get Context List "" "result_l = Format_list () print (" Total%d cars found! ") the details are as follows:"% int (len (result_l)/9)) title_l = []    title = 0 for I in Result_l[title:title + 9]: title_l.append (i) print (title_l) totle_l = [] num = 9 For I in range (0, Len (result_l)//9): context_l = [] for n in Result_l[num:num + 8]: context_l . append (n) print (context_l) totle_l.append (context_l) num + = 8 if i > Int (len (result_l)/9 ): Exit () if __name__ = = ' __main__ ': Title_context ()
Third, the effect shows

Run the code, fill in the required information, and eventually return to the search results, which can be saved to excel later

Python Search Car ticket

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.