从URL获取路径并在Python脚本中使用

发布于 2025-02-05 18:50:38 字数 868 浏览 2 评论 0原文

g'day的家伙,我正在研究一个Python项目,该项目从BOM中提取天气数据( https://bom.gov。 au )。 该脚本正常工作,但是我希望它能够在POST请求中使用部分URL。即,用户将导航到 https://example.com/taf/ymml ,脚本运行和运行和运行和在帖子中使用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)

有什么想法吗?

G'day guys, I'm working on a python project that pulls weather data from BOM (https://bom.gov.au).
The script works correctly, however I would like for it to be able to use part of the URL within the post request. i.e., the user navigates to https://example.com/taf/ymml, the script runs and uses YMML within the POST.

the script I am using is below. I would like to swap out 'YSSY' in myobj for something that pulls it from the url that the user navigates to.

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)

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖风昔人 2025-02-12 18:50:38

好的,所以我设法使用fastapi进行了此工作。当用户导航到example.com/taf/ymml时,该站点将以纯文本为ymml返回。它可以代替任何澳大利亚机场。我尚未确定的一件事是如何删除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)}

Ok so I've managed to get this working using fastapi. When a user navigates to example.com/taf/ymml, the site will return in plain text the taf for ymml. it can be substituted for any Australian Aerodrome. One thing I haven't figured out is how to remove the the square brackets around the taf, but that is a problem for another time.

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)}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文