首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅在使用for循环时用BeautifulSoup刮取问题

仅在使用for循环时用BeautifulSoup刮取问题
EN

Stack Overflow用户
提问于 2021-03-21 09:17:19
回答 3查看 34关注 0票数 0

我正在使用'BeautifulSoup‘库编写一个python代码,它将从一个新闻网站中提取所有评论文章的标题和作者。虽然for循环是按照预期的方式为标题工作的,但其中的find函数用于为每个标题提取作者的名称,它重复地将第一篇文章的作者作为输出返回。

你知道我哪里出错了吗?

代码

代码语言:javascript
复制
from bs4 import BeautifulSoup
import requests

source = requests.get('https://www.nytimes.com/international/').text
soup = BeautifulSoup(source, 'lxml')

opinion = soup.find('div', class_='css-717c4s')

for story in opinion.find_all('article'):
    title = story.h2.text
    print(title)

    author = opinion.find('div', class_='css-1xdt15l')
    print (author.text)

输出:

犹太抵抗运动中的纳粹女战士

朱迪·巴塔利翁

我的曾祖父知道如何修复美国的食品系统

朱迪·巴塔利翁

旧波尔,新魔法师

朱迪·巴塔利翁

为了遵守法律,我们必须付钱给企业吗?

朱迪·巴塔利翁

我不想让我的榜样被抹去

朱迪·巴塔利翁

进步的基督徒复活了!哈利路亚!

朱迪·巴塔利翁

20世纪20年代的需要:电影中的性与浪漫

朱迪·巴塔利翁

拜登失踪了

朱迪·巴塔利翁

共和党人能从我祖母那里学到什么

朱迪·巴塔利翁

你的家的价值是基于种族主义

朱迪·巴塔利翁

一旦我完全接种疫苗,对我来说什么是安全的?

朱迪·巴塔利翁

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-03-21 09:38:50

你应该这么做,

代码语言:javascript
复制
author = story.find('div', class_='css-1xdt15l')

问题是,你在做什么

代码语言:javascript
复制
author = opinion.find('div', class_='css-1xdt15l')

它在“意见”部分的所有作者中获取第一个作者,因为您在循环的每一次迭代中运行了这个语句,因此不管您做了多少次,都只能得到第一个作者。

author = story.find('div', class_='css-1xdt15l')替换它将使您成为每个故事迭代的第一个作者,而且由于每个故事只有一个作者,所以它工作得很好。

票数 0
EN

Stack Overflow用户

发布于 2021-03-21 09:27:23

这是因为你只针对第一个标签,因此只有一个作者。

下面是使用zip()对代码进行修复的方法

代码语言:javascript
复制
from bs4 import BeautifulSoup
import requests

source = requests.get('https://www.nytimes.com/international/').text
soup = BeautifulSoup(source, 'lxml').find('div', class_='css-717c4s')

authors = soup.find_all('div', class_='css-1xdt15l')
stories = soup.find_all('article')

for author, story in zip(authors, stories):
    print(author.text)
    print(story.h2.text)

输出:

代码语言:javascript
复制
Judy Batalion
The Nazi-Fighting Women of the Jewish Resistance
Gracy Olmstead
My Great-Grandfather Knew How to Fix America’s Food System
Maureen Dowd
Old Pol, New Tricks
Nikolas Bowie
Do We Have to Pay Businesses to Obey the Law?
Elizabeth Becker
I Don’t Want My Role Models Erased
Nicholas Kristof
Progressive Christians Arise! Hallelujah!
Ross Douthat
What the 2020s Need: Sex and Romance at the Movies
Frank Bruni
Biden Has Disappeared
Cecilia Gentili
What Republicans Could Learn From My Grandmother
Dorothy A. Brown
Your Home’s Value Is Based on Racism
Linsey Marr, Juliet Morrison and Caitlin Rivers
Once I Am Fully Vaccinated, What Is Safe for Me to Do?
票数 0
EN

Stack Overflow用户

发布于 2021-03-21 09:33:04

很小的错误。

您应该搜索循环中的每个“/story”对象,而不是初始的“视图”对象,即

代码语言:javascript
复制
author = story.find('div', class_='css-1xdt15l')

这将产生所需的输出:

代码语言:javascript
复制
The Nazi-Fighting Women of the Jewish Resistance
Judy Batalion
My Great-Grandfather Knew How to Fix America’s Food System
Gracy Olmstead
Old Pol, New Tricks
Maureen Dowd
Do We Have to Pay Businesses to Obey the Law?
Nikolas Bowie
I Don’t Want My Role Models Erased
Elizabeth Becker
Progressive Christians Arise! Hallelujah!
Nicholas Kristof
What the 2020s Need: Sex and Romance at the Movies
Ross Douthat
Biden Has Disappeared
Frank Bruni
What Republicans Could Learn From My Grandmother
Cecilia Gentili
Your Home’s Value Is Based on Racism
Dorothy A. Brown
Once I Am Fully Vaccinated, What Is Safe for Me to Do?
Linsey Marr, Juliet Morrison and Caitlin Rivers
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66730612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档