【发布时间】:2021-06-29 22:56:43
【问题描述】:
所以我正在从这个网站上抓取 MCQ。我最后想要正确的选择。所有选项共享相同的 class='radio-button-click-target'。但正确的选项最后有 radio-button-click-target correctquestions。我尝试了custom function 中的BeautifulSoup webscraping find_all( ): finding exact match solution,但现在没有任何选项出现。
import requests
from bs4 import BeautifulSoup
address = 'https://www.ilmkidunya.com/online-test/5th-class-science-english-meduim-mcqs-with-answers?startfrom=0&last=92'
response = requests.get(address)
soup = BeautifulSoup(response.text, 'lxml')
ques_id = soup.find_all('div', class_='q-title')
ques_det = soup.find_all('div', class_='q-desc')
optn_det = soup.find_all('div', class_='choose-answer-block')
for i in range(0, len(ques_id)):
print((ques_id[i].text))
print(str(ques_det[i].text).strip())
options = optn_det[i].find_all(lambda tag: tag.name == 'div' and tag.get('class') == ['radio-button-click-target correctquestions'])
for opn in options:
print(str(opn.text).strip())
print('<----->')
电流输出
Question # 1
The group which belong to invertebrates is.
amphibians
Worms
Reptiles
Mammals
<----->
Question # 2
The main cause of cholera is:
land polllution
noise pollution
air pollution
water pollution
<----->
预期输出
Question # 1
The group which belong to invertebrates is.
amphibians
Reptiles
Mammals
Worms
<----->
Question # 2
The main cause of cholera is:
land polllution
noise pollution
air pollution
water pollution
<----->
正确的选项应该显示在末尾strong>
【问题讨论】:
-
请提供预期输出和实际输出的示例。
-
您可以立即查看
标签: python beautifulsoup