从 HTML table 中抓取日期和 link,其中两个项目由不同的标签分隔

Scrape date and link from a HTML table where both items are separated by different tags

我有一个很长的 table,有 link 和日期。请参阅下面的 HTML 代码。 table 中的数据结构良好:所有信息都出现在块中,日期 (isodate) 在 dt 标签之间,link 在 [=16= 之间] 标签。

使用 Python 和 BeautifulSoup,我想获取每个块的日期和 link。

<dt isodate="2022-02-10">
<div class="date">10 February 2021</div>
</dt>
<dd>
<div class="testa"><span class="testy"><a class="row" href="https://www.bla.html" lang="en"> <span class="full">English</span></a></span></div>
</dd>

<dt isodate="2022-02-12">
<div class="date">12 February 2021</div>
</dt>
<dd>
<div class="testa"><span class="testy"><a class="row" href="https://www.bli.html" lang="en"> <span class="full">English</span></a></span></div>
</dd>

如何实现?

您可以使用 beautiful soup find_allfind_next_sibling 方法 select dt 和兄弟 dd 如下:

from bs4 import BeautifulSoup

html_doc = """
    <dt isodate="2022-02-10">
    <div class="date">10 February 2021</div>
    </dt>
    <dd>
    <div class="testa"><span class="testy"><a class="row" href="https://www.bla.html" lang="en"> <span class="full">English</span></a></span></div>
    </dd>

    <dt isodate="2022-02-12">
    <div class="date">12 February 2021</div>
    </dt>
    <dd>
    <div class="testa"><span class="testy"><a class="row" href="https://www.bli.html" lang="en"> <span class="full">English</span></a></span></div>
    </dd>
"""

soup = BeautifulSoup(html_doc, 'html.parser')

items = []

for item in soup.find_all('dt'):
    date = item.get('isodate')
    url = item.find_next_sibling('dd').select_one('div a').get('href')
    # add a dict to the items list
    items.append(dict(date=date, url=url))

print(items)

输出

[
{'date': '2022-02-10', 'url': 'https://www.bla.html'}, 
{'date': '2022-02-12', 'url': 'https://www.bli.html'}
]

此处index.html包括

<html>
<body>

    <dt isodate="2022-02-10"><div class="date">10 February 2021</div></dt>
    <dd>
        <div class="testa"><span class="testy"><a class="row" href="https://www.bla.html" lang="en"> <span
            class="full">English</span></a></span></div></dd>

    <dt isodate="2022-02-12"><div class="date">12 February 2021</div></dt>
    <dd>
        <div class="testa"><span class="testy"><a class="row" href="https://www.bli.html" lang="en"> <span
                        class="full">English</span></a></span></div></dd>
</body>
</html>

Python 使用代码:

from bs4 import BeautifulSoup

with open("index.html") as html_content:
    soup = BeautifulSoup(html_content, 'html.parser')
date = [x.get_text() for x in soup.find_all('div',class_='date')]
link = [x['href'] for x in soup.find_all('a',href=True)]
  • 日期:

find_all 将找到 div class = 'div' 和 return 所有匹配标签的列表。

[<div class="date">10 February 2021</div>, <div class="date">12 February 2021</div>]

从这里您可以通过 x.get_text()

提取日期
  • 对于Link:

find_all 将找到包含 href 值(当 href=True 时)的标签和 return 所有匹配标签的列表。

[<a class="row" href="https://www.bla.html" lang="en"> <span class="full">English</span></a>, <a class="row" href="https://www.bli.html" lang="en"> <span class="full">English</span></a>]

从这里您可以使用 x['href']

提取 href 数据

输出:日期和 url 列表可以单独访问。

['10 February 2021', '12 February 2021'] ['https://www.bla.html', 'https://www.bli.html']