多个类,并且IndexError:列表索引超出范围错误

问题描述 投票:0回答:1

当我尝试通过调用下面的代码从“句子”类中打印示例时>

examp3 = soup1.find_all(class_='sentence')
print(examp3[0].get_text())

它会给我类似下面的错误

Traceback (most recent call last):
File "/home/hudacse6/WebScrap/webscrap.py", line 33, in <module>
print(examp3[0].get_text())
IndexError: list index out of range 

这是网页上我要调用和打印的图片

class called pic

如何克服这个错误?

这是我的完整html.parser代码

import requests
from bs4 import BeautifulSoup

page1 = requests.get('https://www.vocabulary.com/dictionary/abet')
page2 = requests.get('https://www.thesaurus.com/browse/cite?s=t')
page3 = requests.get('https://dictionary.cambridge.org/dictionary/english/abet')
page4 = requests.get('https://www.merriam-webster.com/dictionary/abet')

soup1 = BeautifulSoup(page1.content, 'html.parser')
soup2 = BeautifulSoup(page2.content, 'html.parser')
soup3 = BeautifulSoup(page3.content, 'html.parser')
soup4 = BeautifulSoup(page4.content, 'html.parser')

synonyms2 = soup1.find_all(class_='short')
synonyms3 = soup1.find_all(class_='long')
print("Short Description of ABET: ", synonyms2[0].get_text(), "\n", )
print("Brief Description of ABET: ", synonyms3[0].get_text(), "\n", )

syn = soup2.find_all('a', class_='css-18rr30y etbu2a31')
find_syn = [syns.get_text() for syns in syn]
# print(syn[0].get_text())
print("Most relevant Synonyms of ABET: ", find_syn, "\n", )

print("Examples Of ABET: ")

exmp1 = soup3.find_all(class_='eg deg')
print(exmp1[0].get_text())

examp2 = soup4.find_all(class_='mw_t_sp')
print(examp2[0].get_text())

examp3 = soup1.find_all(class_='sentence')
print(examp3[0].get_text())

并且我曾尝试一次调用多个相同名称的类,以使用下面的代码打印类文本,但只有第一个类被调用并打印。第二个不起作用。

 #soup.findAll(True, {'class':['class1', 'class2']}) --Main Code
 examp2 = soup4.findAll(True, {'class': ['mw_t_sp', 'ex-sent first-child t no-aq sents']})
 print(examp2[1].get_text())

仅此打印

ABET示例:

教bet犯罪

这里是我叫这张照片的地方。

enter image description here

上面的代码有什么问题?

[当我尝试通过调用以下代码从“句子”类中打印示例时,例如testp3 = soup1.find_all(class _ ='sentence')print(examp3 [0] .get_text()),它将给我类似下面的错误追溯(...

python beautifulsoup
1个回答
0
投票

页面https://www.vocabulary.com/dictionary/abet使用javascript在“用法示例”下加载内容。

© www.soinside.com 2019 - 2024. All rights reserved.