Python 浏览新闻网站时出现索引错误

Python 浏览新闻网站时出现索引错误,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我一直在尝试从web上搜索新闻文章的标题,但在执行以下代码时遇到了“索引错误”。我只在最后一行代码中遇到了一个问题 import requests from bs4 import BeautifulSoup URL= 'https://www.ndtv.com/coronavirus?pfrom=home-mainnavgation' r1 = requests.get(URL) coverpage = r1.content soup1 = BeautifulSoup(coverpage, 'h

我一直在尝试从web上搜索新闻文章的标题,但在执行以下代码时遇到了“索引错误”。我只在最后一行代码中遇到了一个问题

import requests
from bs4 import BeautifulSoup
URL= 'https://www.ndtv.com/coronavirus?pfrom=home-mainnavgation'
r1 = requests.get(URL)
coverpage = r1.content
soup1 = BeautifulSoup(coverpage, 'html5lib')
coverpage_news = soup1.find_all('h3', class_='item-title')
coverpage_news[4].get_text()

这就是错误:

IndexError                                Traceback (most recent call last)
<ipython-input-10-f7f1f6fab81c> in <module>
      6 soup1 = BeautifulSoup(coverpage, 'html5lib')
      7 coverpage_news = soup1.find_all('h3', class_='item-title')
----> 8 coverpage_news[4].get_text()

IndexError: list index out of range

索引器错误回溯(最近一次调用)
在里面
6 soup1=BeautifulSoup(封面“html5lib”)
7封面新闻=soup1.find_all('h3',class='item-title')
---->8封面新闻[4]。获取文本()
索引器:列表索引超出范围
尝试更改:

coverpage_news = soup1.find_all('h3', class_='item-title')

使用
soup1.select()
搜索与CSS选择器匹配的嵌套元素:

coverpage_news = soup1.select("h3 a.item-title")

这将找到一个
h3
元素后代的
a
元素,该元素带有
class=“item title”

稍微更改@Barmar的有用答案以显示所有标题:

coverpage_news = soup1.select("h3 a.item-title")
for link in coverpage_news:
  print(link.text)
输出:

US Covid Infections Cross 9 Million, Record 1-Day Spike Of 94,000 Cases
Johnson & Johnson Plans To Test COVID-19 Vaccine On Youngsters Soon
Global Stock Markets Decline As Coronavirus Infection Rate Weighs
Cristiano Ronaldo Recovers From Coronavirus
Reliance's July-September Profit Falls 15% As Covid Slams Oil Business
"Likely To Know By December If We'll Have Covid Vaccine": Top US Expert
With No Local Case In A Record 200 Days, This Country Is World's Envy
Delhi Blames Pollution For Covid Spike At High-Level Health Ministry Meet
Delhi Covid Cases Above 5,000 For 3rd Straight Day, Spike In ICU Patients
2 Million Indians Returned From Abroad Under Vande Bharat Mission: Centre
Existing Lockdown Restrictions Extended In Maharashtra Till November 30
Can TB Vaccine Protect Elderly From Covid?
Is The Covid-19 Situation Worsening In Delhi?
What's The Truth Behind India's Falling Covid Numbers?
"Slight Laxity Can Lead To Spike": AIIMS Director As India Sees Drop In Covid Cases

这意味着页面上没有5个元素满足您搜索的条件。我在该页面上没有看到任何
。我看到
,这就是您要查找的内容吗?我也看到了很多
,还有
,它们包含
,所以你到底想提取什么?这段代码只给出了一个结果,但我想抓取网页的整个标题。它应该找到所有匹配项
select_one()
只返回第一个匹配项。
US Covid Infections Cross 9 Million, Record 1-Day Spike Of 94,000 Cases
Johnson & Johnson Plans To Test COVID-19 Vaccine On Youngsters Soon
Global Stock Markets Decline As Coronavirus Infection Rate Weighs
Cristiano Ronaldo Recovers From Coronavirus
Reliance's July-September Profit Falls 15% As Covid Slams Oil Business
"Likely To Know By December If We'll Have Covid Vaccine": Top US Expert
With No Local Case In A Record 200 Days, This Country Is World's Envy
Delhi Blames Pollution For Covid Spike At High-Level Health Ministry Meet
Delhi Covid Cases Above 5,000 For 3rd Straight Day, Spike In ICU Patients
2 Million Indians Returned From Abroad Under Vande Bharat Mission: Centre
Existing Lockdown Restrictions Extended In Maharashtra Till November 30
Can TB Vaccine Protect Elderly From Covid?
Is The Covid-19 Situation Worsening In Delhi?
What's The Truth Behind India's Falling Covid Numbers?
"Slight Laxity Can Lead To Spike": AIIMS Director As India Sees Drop In Covid Cases