Last active
September 30, 2018 07:00
-
-
Save tudoanh/e1d6db85652a2f53c2d30ca6a9341085 to your computer and use it in GitHub Desktop.
Get VietJet flight data from reservation code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from lxml import html | |
import requests | |
class VJCrawl(): | |
def __init__(self): | |
self.s = requests.Session() | |
self.url = 'https://flightstatus.vietjetair.com/?lang=vi' | |
self.user_agent = ("Mozilla/5.0 (X11; Linux x86_64) " | |
"AppleWebKit/537.36 (KHTML, like Gecko) " | |
"Chrome/59.0.3071.115 Safari/537.36") | |
def get_tokens(self): | |
self.s.headers.update({'User-Agent': self.user_agent}) | |
response = self.s.get(self.url) | |
tree = html.fromstring(response.content) | |
di = {} | |
di['__VIEWSTATE'] = tree.xpath('//*[@id="__VIEWSTATE"]/@value')[0] | |
di['__VIEWSTATEGENERATOR'] = tree.xpath('//*[@id="__VIEWSTATEGENERATOR"]/@value')[0] | |
di['__EVENTVALIDATION'] = tree.xpath('//*[@id="__EVENTVALIDATION"]/@value')[0] | |
return di | |
def get_flight_info(self, flight_code): | |
token = self.get_tokens() | |
token['ctl00$MainContent$ScriptManager'] = 'ctl00$MainContent$ScriptManager|ctl00$MainContent$buttonSearch' | |
token['ctl00$ddlLanguges'] = 'vi' | |
token['__EVENTTARGET'] = 'ctl00$MainContent$buttonSearch' | |
token['__EVENTARGUMENT'] = '' | |
token['__ASYNCPOST'] = 'true' | |
token['ctl00$MainContent$txtReservationNumber'] = flight_code | |
r = self.s.post(self.url, data=token, params={'lang': 'vi'}) | |
tree = html.fromstring(r.content) | |
result = [] | |
for flight in tree.xpath('//*[@class="trRow"]'): | |
result.append(flight.xpath('./td/text()')) | |
return result | |
def get_multiple_flight_info(self, flight_code_list): | |
for code in flight_code_list: | |
yield self.get_flight_info(code) | |
if __name__ == '__main__': | |
v = VJCrawl() | |
print(v.get_flight_info(55767987)) | |
for fl in v.get_multiple_flight_info(range(55767987, 55767997)): | |
print(fl) |
get_multiple_flight_info(range(55767987, 55767997))
cho mình hỏi flight_code này là gì nhỉ? lấy từ đâu?
Thanks bạn nhiều
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: