看板 Python 關於我們 聯絡資訊
原文恕刪~ 我剛使用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
rs6000: 推 05/28 07:31
TakiDog: 結論selenium根本再搞自己!? 05/28 10:30
caty1010: 只能說selenium 在這個case上比較不適合 05/28 12:26
caty1010: 替selenium平反一下, 加註sleep後也可以抓到資料 05/28 12:37
caty1010: code: https://www.codepile.net/pile/p1LvV8lQ 05/28 12:37
haru97724: 有~~我有試出來惹(人′∀`)♪大感謝~~ 05/28 19:49
haru97724: 我一開始用selenium是因為要拿他的cookie。結果好像也 05/28 19:49
haru97724: 不用這麼麻煩XDDD 05/28 19:49