python - 使用 BS4 提取最常阅读的标题

标签 python beautifulsoup

我想提取新闻页面阅读次数最多的部分中的标题。这是我到目前为止所拥有的,但我正在获得所有标题。我只想要阅读次数最多的部分中的内容。

`

import requests
from bs4 import BeautifulSoup

base_url = 'https://www.michigandaily.com/section/opinion'
r = requests.get(base_url)
soup = BeautifulSoup(r.text, "html5lib")

for story_heading in soup.find_all(class_= "views-field views-field-title"):
    if story_heading.a:
        print(story_heading.a.text.replace("\n", " ").strip())
    else:
        print(story_heading.contents[0].strip())`

最佳答案

您需要将范围限制为仅包含阅读次数最多的文章的 div 容器。

import requests
from bs4 import BeautifulSoup

base_url = 'https://www.michigandaily.com/section/opinion'
r = requests.get(base_url)
soup = BeautifulSoup(r.text, "html5lib")

most_read_soup = soup.find_all('div', {'class': 'view-id-most_read'})[0]

for story_heading in most_read_soup.find_all(class_= "views-field views-field-title"):
    if story_heading.a:
        print(story_heading.a.text.replace("\n", " ").strip())
    else:
        print(story_heading.contents[0].strip())

关于python - 使用 BS4 提取最常阅读的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36630285/

相关文章:

python - dateutilrelativedelta(weekday=FR) 是否有等效的月份?

python - 围绕中心单元旋转 3x3 阵列的每个单元的模块(不是矩阵旋转)

python - 将代码的基本部分从 C++ 转换为 Python

python - BeautifulSoup 按数字指定表格列?

Python - BeautifulSoup - 通过列表中的特定元素遍历 findall

python - 通过 tcp python 发送字节

python - Asyncio 两个循环用于不同的 I/O 任务?

python-3.x - 如何从 target.com 产品页面抓取产品价格?

python - 使用 BeautifulSoup 查找具有特定子元素的元素

python - 如何将 beautifulsoup 的输出附加到 pandas 数据框