Re: [問題] 機票網站爬蟲問題
原文恕刪~
我剛使用python requests 來做實驗
發現可以抓到你想要的資料~
想法是:
感覺呈現資料的頁面
跟ajax的呼叫 可能會存在cookie驗證關係
所以 就先get一下search頁面 (html)
再去post ajax的url (json)
以下爛code 大大們請指教
以2018-06-28日期, TPE to OKA為例 (可惡想去...)
session = requests.Session()
index_url = 'https://www.ctrip.com.hk/flights/taipei-to-okinawa/tickets-tpe-oka/?flighttype=s&dcity=tpe&acity=oka&startdate=2018-06-28&class=y&quantity=1&searchboxarg=t'
index_content = session.post(index_url,verify=False)
header = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',
'x-requested-with': 'XMLHttpRequest',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'origin': 'https://www.ctrip.com.hk',
'referer': index_url
}
flight_api_url = 'https://www.ctrip.com.hk/flights/Ajax/SearchFlight'
param = {'context':'{"SearchNo":"1","FlightWay":"OW","SubChannel":"0","SearchToken":"1","Class":"Y","SegmentParameterList":[{"DCityCode":"TPE","ACityCode":"OKA","DDateString":"2018-06-28"}],"TravelerParameteList":[{"TravelerType":"ADT","TravelerCount":"1"},{"TravelerType":"CHD","TravelerCount":"0"},{"TravelerType":"INF","TravelerCount":"0"}]}'}
SearchResult = session.post(flight_api_url,verify=False,headers=header,data=param).json()
#沒有意外的話 SearchResult 印出來就是你要的json了
我的環境是python 2.7
上面的search條件 請你自己再加以修改
應該就可以開始爬其他天 或是 其他機場 的資料了
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.13.194.3
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1527429062.A.500.html
推
05/28 07:31,
7年前
, 1F
05/28 07:31, 1F
推
05/28 10:30,
7年前
, 2F
05/28 10:30, 2F
→
05/28 12:26,
7年前
, 3F
05/28 12:26, 3F
→
05/28 12:37,
7年前
, 4F
05/28 12:37, 4F
→
05/28 12:37,
7年前
, 5F
05/28 12:37, 5F
推
05/28 19:49,
7年前
, 6F
05/28 19:49, 6F
→
05/28 19:49,
7年前
, 7F
05/28 19:49, 7F
→
05/28 19:49,
7年前
, 8F
05/28 19:49, 8F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
Python 近期熱門文章
PTT數位生活區 即時熱門文章