G'day 伙計們,我正在開發一個從 BOM ( https://bom.gov.au ) 中提取天氣資料的 python 專案。該腳本可以正常作業,但是我希望它能夠在發布請求中使用部分 URL。即,用戶導航到https://example.com/taf/ymml,腳本運行并在 POST 中使用 YMML。
我正在使用的腳本如下。我想將 myobj 中的 'YSSY' 換成從用戶導航到的 url 中提取的東西。
import requests
import re
url = 'http://www.bom.gov.au/aviation/php/process.php'
myobj = {'keyword': 'YSSY', 'type': 'search', 'page': 'TAF'}
headers = {'User-Agent': 'Chrome/102.0.0.0'}
x = requests.post(url, data = myobj, headers=headers)
content = x.text
stripped = re.sub('<[^<] ?>', ' ', content)
split_string = stripped.split("METAR", 1)
substring = split_string[0]
print(substring)
有任何想法嗎?
uj5u.com熱心網友回復:
好的,所以我已經設法使用 fastapi 來完成這項作業。當用戶導航到 example.com/taf/ymml 時,該站點將以純文本形式回傳 ymml 的 taf。它可以替代任何澳大利亞機場。我還沒有弄清楚的一件事是如何洗掉 taf 周圍的方括號,但這是另一個問題。
from fastapi import FastAPI
import requests
from bs4 import BeautifulSoup
app = FastAPI()
@app.get("/taf/{icao}")
async def read_icao(icao):
url = 'http://www.bom.gov.au/aviation/php/process.php'
myobj = {'keyword': icao, 'type': 'search', 'page': 'TAF'}
headers = {'User-Agent': 'Chrome/102.0.0.0'}
x = requests.post(url, data = myobj, headers=headers)
content = x.text
split_string = content.split("METAR", 1)
substring = split_string[0]
soup = BeautifulSoup(substring, 'html.parser')
for br in soup('br'):
br.replace_with(' ')
#Create TAFs array.
tafs = []
for taf in soup.find_all('p', class_="product"):
full_taf = taf.get_text()
tafs.append(full_taf.rstrip())
return {tuple(tafs)}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/488768.html
