从电子邮件中检索航班信息

问题描述 投票:0回答:1

我如何解析电子邮件并提取航班预订详细信息(如果存在)?

我现在只想解析 Gmail 电子邮件,因此如果它专门使用 Gmail API,那也没关系。

虽然我查看了 Gmail API (https://developers.google.com/gmail/api/v1/reference/users/messages/get),并在收件箱中的特定邮件上尝试了该 API,但包含网络应用程序上的航班预订弹出窗口,并且在 API 中找不到任何与航班相关的内容。

那么在一般情况下我该怎么做呢?通过直接解析电子邮件正文?有图书馆吗?

谢谢。

email parsing
1个回答
0
投票

我实际上正在开发一个可以执行此操作的 Python 包,但它可能非常依赖于航空公司。某些公司电子邮件可能包含包含该信息的 HTML,以下是来自 Ryanair 的示例。

<div itemscope="itemscope" itemtype="http://schema.org/FlightReservation"
    xmlns="">
    <meta itemprop="reservationNumber" content="KLLXXX" />
    <link itemprop="reservationStatus" href="https://schema.org/Confirmed" />
    <div itemprop="underName" itemscope="itemscope" itemtype="http://schema.org/Person">
        <meta itemprop="name" content="John Smith" />
    </div>
    <div itemprop="reservationFor" itemscope="itemscope" itemtype="http://schema.org/Flight">
        <meta itemprop="flightNumber" content="1197" />
        <div itemprop="airline" itemscope="itemscope" itemtype="http://schema.org/Airline">
            <meta itemprop="name" content="Ryanair" />
            <meta itemprop="iataCode" content="FR" />
        </div>
        <div itemprop="departureAirport" itemscope="itemscope" itemtype="http://schema.org/Airport">
            <meta itemprop="name" content="Brussels (Charleroi) " />
            <meta itemprop="iataCode" content="CRL" />
        </div>
        <meta itemprop="departureTime" content="2024-05-31T07:00:00" />
        <div itemprop="arrivalAirport" itemscope="itemscope" itemtype="http://schema.org/Airport">
            <meta itemprop="name" content="Athens " />
            <meta itemprop="iataCode" content="" />
        </div>
        <meta itemprop="arrivalTime" content="2024-05-31T11:05:00" />
    </div>
    <link itemprop="checkinUrl" href="https://www.ryanair.com/gb/en/check-in" />
</div> 

对于其他公司,我正在编写一些复杂的 REGEX 表达式来提取信息。

请注意,HTML 中的信息不可见,因此,如果您转发电子邮件,它可能会丢失(除非您转发原始邮件)。此外,电子邮件可能会采用 Base64 编码,您可能需要在提取信息之前对其进行预处理。

© www.soinside.com 2019 - 2024. All rights reserved.