【问题标题】:How do I extract all the links of a certain section of a web page with beautifulsoup? [duplicate]如何用beautifulsoup提取网页某个部分的所有链接? [复制]
【发布时间】:2026-01-03 05:05:03
【问题描述】:

我只需要提取网页中某个部分的链接,但我在 Beautifulsoup 上找到的所有教程总是抓取整个页面。

如何只抓取某个<div class="xyz">内的链接???

编辑: 我目前有这个代码:

soup1.find_all('h3', class_="entry-title td-module-title")

这会找到网页的所有链接,包含在class_="entry-title td-module-title"

我想找到类中仍然包含的所有链接

"entry-title td-module-title"`

但只有那些包含在由以下部分表示的部分:

<div class="wpb_wrapper">

(对不起,如果我的问题有点缺乏信息,我试图添加更多细节)

【问题讨论】:

  • soup.findAll("div", {"class": "xyz"}) 这应该可以。稍后您应该通过将其存储在变量中来抓取该部分内的各个链接。
  • 这能回答你的问题吗? How to find elements by class

标签: python web-scraping beautifulsoup


【解决方案1】:

试试这个:

soup2 =  soup1.find_all('div',class_='wpb_wrapper')
results = []
for div in soup2:
    required = div.find_all('h3', class_="entry-title td-module-title")
    results.append(required)

【讨论】:

  • 我用不同的方式解决了,但你的回答还是很有用的
【解决方案2】:

您可以为此任务使用 CSS 选择器:

for link in soup.select('div.wpb_wrapper h3.entry-title.td-module-title a'):
    print(link['href'])

这将打印&lt;h3 class="entry-title td-module-title"&gt; 下的所有链接,&lt;div class="wpb_wrapper"&gt; 下。

【讨论】:

    最近更新 更多