我想抓取韓國航班網站,該網站給出了隱藏的contuniue航班的api。
但當我在postman中檢查時,它可以作業并顯示結果,而當我用python在request中嘗試時,它顯示了空白記錄。
這是一個網站https://suvarnabhumi.airportthai.co.th/flight,這是一個api
'https://apis.airportthai.co.th/',其請求的有效載荷在以下代碼中給出:
import requests
from requests import session
import json
from pprint import pprint
headers = {
#"Content-Type": "application/text;",
"User-Agent"。"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"/span>,
"接受" : "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive"。
"Accept-Language": "en-US,en;q=0.9,ur;q=0.8" ,
"sec-ch-ua": 'Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"' ,
"Sec-Fetch-Mode": "cors",
"Referer": "https://suvarnabhumi.airportthai.co.th/".
}
data = {"query"/span>:"
query ($site: 字串, $type: FlightType, $search: 字串, $schedule_start: String, $schedule_end: 字串) {
航班(site: $site, type: $type, search: $search, schedule_start: $schedule_start, schedule_end: $schedule_end) {
航班_id
號碼
航空公司_ID
飛機號
出發時間
抵達時間
航班_離境 {
id
site_id
評論
終端
閘機
Check_in_counter
狀態顏色
估計時間
實際時間
預定日期
更新時間
飛行份額
種類
}
飛行_到達 {
id
site_id
帶子
終端
評論
狀態_顏色
估計時間
第一個袋子的位置
最后一包
飛行份額
型別
}
Origin_airport {
id
名稱
城市
iata_code
國際民航組織代碼
型別名稱
}
目的地機場 {
id
名稱
城市
iata_code
國際民航組織代碼
型別名稱
}
航空公司 {
id
iata
ICAO
名稱
標志
型別名稱
}
飛機 {
id
名稱
iata
身份證
型別名稱
}
更新的_at
帳號:_typename
}
","變數":{"站點": "bkk","型別":"A","搜尋"。 "","schedule_start":"2021-08-24 11: 49:00","schedule_end":"2021-08-24 23:59:59"}}
url = " https://apis.airportthai.co.th/"。
r = requests.post(url, data = data, headers = headers)
print(r.json()
當它運行時,它給我的結果是空白的:
{'data': {'fills': []}}
在postman中,它顯示了所有的資料,但在這里它不作業。
這里是網站中的api
uj5u.com熱心網友回復:
嘗試一下:-
data = {"query": "query($site: 字串, $type: FlightType, $search: 字串, $schedule_start: String, $schedule_end: 字串) {flights(site: $site, type: $type, search: $search, schedule_start: $schedule_start, schedule_end: $schedule_end) {flight_id number airline_id aircraft_id departure_scheduled_at arrival_scheduled_at flight_departure {id site_id remark terminal gate check_in_counter status_color estimated_at actual_at scheduled_at updated_at flight_shares __typename}flight_arrival {id site_id belt terminal remark status_color estimate_at first_bag_at last_bag_at flight_shares __typename}origin_airport {id name city iata_code icao_code __typename}destination_airport {id name city iata_code icao_code __typename}airline {id iata icao name logo __typename}aircraft {id name iata icao __typename}updated_at __typename}"。 "variables"。 {"site":"bkk","type":"A","search": "","schedule_start":"2021-09-10 00。 00:00","schedule_end":"2021-09-10 23:59:59"}}
url = " https://apis.airportthai.co.th/"。
from urllib import request
import json
req = request.Request(url, method='POST')
req.add_header('Content-Type', 'application/json')
r = request.urlopen(req, data=json.dumps(data).encode() )
print(r.read()
uj5u.com熱心網友回復:
這對我來說很有效。
這對我來說很有效。
import requests
url = "https://apis.airportthai.co.th/"/span>
data = {"query"/span>:"
query ($site: 字串, $type: FlightType, $search: 字串,"
" $schedule_start: 字串, $schedule_end: 字串) {
"
" flights(site: $site, type: $type, search: $search, schedule_start: "
"$schedule_start, schedule_end: $schedule_end) {
航班_id
"/span>
" number
航空公司_ID
飛機號
"
" departure_scheduled_at
抵達時間
"/span>
" flight_departure {
id
site_id
"
" remark
終端
門
Check_in_counter
"
" status_color
估計時間
實際時間
"
" scheduled_at
更新日期
航班數
"
" __typename
}
flight_arrival {
id
"/span>
" site_id
帶子
終端
備注
"
"status_color
估計時間
第一袋
"
"last_bag_at
飛行股份
種類
}
"
" origin_airport {
id
名稱
城市
"/span>
"iata_code
編碼
型別
}
"
" destination_airport {
id
名稱
城市
"/span>
"iata_code
編碼
型別
}
航線 {
"
" id
iata
ICAO
名稱
徽標
"
" __typename
}
飛機 {
id
名稱
"/span>
" iata
身份證
型別
}
update_at
"
"__typename
}
}
"。
"variables":{"site": "bkk","型別":"A","搜索"。 "","schedule_start":"2021-09-10 17。 10:00","schedule_end":"2021-09-10 23:59:59"}}
回應 = requests.post(url, json=data)
print(response.json())
可能是資料行的長度過大。
你所提供的頭檔案是不必要的。
另外,在提交有效載荷時,請使用json格式,而不是行格式資料
uj5u.com熱心網友回復:
以下是我的作業方案:
代碼:
代碼:import requests
import pandas as pd
import json
body = {"query": "
query ($site: String, $type: FlightType, $search: 字串, $schedule_start: String, $schedule_end: 字串) {
航班(site: $site, type: $type, search: $search, schedule_start: $schedule_start, schedule_end: $schedule_end) {
航班_id
號碼
航空公司_ID
飛機號
出發時間
抵達時間
航班_離境 {
id
site_id
評論
終端
閘機
Check_in_counter
狀態顏色
估計時間
實際時間
預定日期
更新時間
飛行份額
種類
}
飛行_到達 {
id
site_id
帶子
終端
評論
狀態_顏色
估計時間
第一個袋子的位置
最后一包
飛行份額
型別
}
Origin_airport {
id
名稱
城市
iata_code
國際民航組織代碼
型別名稱
}
目的地機場 {
id
名稱
城市
iata_code
國際民航組織代碼
型別名稱
}
航空公司 {
id
iata
ICAO
名稱
標志
型別名稱
}
飛機 {
id
名稱
iata
身份證
型別名稱
}
更新的_at
帳號:_typename
}
", "變數": {"站點": "bkk"/span>, "type"/span>: "A", "搜索": "", "schedule_start": "2021-09-10 21:38:00"/span>, "schedule_end"/span>: "2021-09-10 23:59:59"}}頭檔案 = { {fnFangSong_GB2312bord1shad1pos(200,288)}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36"/span>,
"接受"。"*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9,bn;q=0.8,es;q=0.7,ar;q=0.6"。
"連接"。"keep-alive"。
"Content-Length": "1843",
"Content-Type": "application/json",
"Host": "apis.airportthai.co.th"。
"Origin": "https://suvarnabhumi.airportthai.co.th",
"referer": "https://suvarnabhumi.airportthai.co.th/",
"sec-ch-ua-mobile": "?0".
}
url = "https://apis.airportthai.co.th/"/span>
r = requests.post(url, data=json.dumps(body), headers=headers)
回應 = r.json()['data']['fours']
for resp in response:
print(respon['number'])
OUTPUT。例子 航班號
SG 089
VZ 317466
WE 137 137
ZE761
LJ 011
TG 677850 TR
吉隆坡 820
TG222
TG 625418410121
吉隆坡 804
FD4127
TG482
TG 585345
TG 607651[/span
TG436
WE 591[/span
VZ2107
WE288
TG635
WE268
JL 707
TG 679741[/span
VZ311
FD4303
NH 805181
VZ 3293971[/span
FD 4109[/span
LJ 001
EK385
7C 2215617[/span
KE 659
VZ121
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/320271.html
標籤:
