簡體   English   中英

在美麗的湯中用文字打印 Class 名稱 Python

[英]Printing Class Name with Text in Beautiful Soup Python

我想用所有選項打印 class 名稱。 對於錯誤的答案,它只是單選按鈕點擊目標,但使用正確選項,它的單選按鈕點擊目標更正問題

import requests
from bs4 import BeautifulSoup
addresses = ['https://www.ilmkidunya.com/online-test/5th-class-science-english-meduim-mcqs-with-answers?startfrom=0&last=92']
subjects = ['05th-Science-EM-']
for runscript in range(0, len(addresses)):
    response = requests.get(addresses[runscript])
    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)):
        unformated_ques_id = (ques_id[i].text)
        formated_ques_id = unformated_ques_id.replace("Question #  ", subjects[runscript])
        print(formated_ques_id)
        #print('\n')
        print(str(ques_det[i].text).strip())
        #print('\n')
        options = optn_det[i].find_all('label', class_='radio-button-click-target')
        for opn in options:
            print(str(opn.text).strip())
            #print('\n')
        print('<----->')


當前結果
05th-科學-EM-1
雌性登革熱蚊子產卵:
在牆上
在土壤中
在木頭上
在水里
<----->
05th-科學-EM-2
蛇是一個例子:
脊椎動物
蠕蟲
昆蟲
蝸牛
<----->
預期結果
05th-科學-EM-1
雌性登革熱蚊子產卵:
單選按鈕點擊目標:在牆上
單選按鈕點擊目標:在土壤中
單選按鈕點擊目標:在木頭上
單選按鈕點擊目標更正問題:在水中
<----->
05th-科學-EM-2
蛇是一個例子:
單選按鈕點擊目標更正問題:脊椎動物
單選按鈕點擊目標:蠕蟲
單選按鈕點擊目標:昆蟲
單選按鈕點擊目標:蝸牛
<----->

按如下方式更改您的選擇器,並刪除已有字符串上的 str 調用。 此外,在適當的情況下,使用 ['class'] 提取 class,並使用join組合多值類:

import requests
from bs4 import BeautifulSoup

addresses = ['https://www.ilmkidunya.com/online-test/5th-class-science-english-meduim-mcqs-with-answers?startfrom=0&last=92']
subjects = ['05th-Science-EM-']

with requests.Session() as s:
    for runscript in range(0, len(addresses)):
        response = s.get(addresses[runscript])
        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)):
            unformated_ques_id = (ques_id[i].text)
            formated_ques_id = unformated_ques_id.replace("Question #  ", subjects[runscript])
            print(formated_ques_id)
            print(ques_det[i].text.strip())
            options = optn_det[i].select('label.radio-button-click-target')

            for opn in options:
                print(' '.join(opn['class']), opn.text.strip())
            print('<----->')

你應該多嘗試一點。

for opn in options:
            print(' '.join(opn['class']), end=' : ')
            print(str(opn.text).strip())

這將獲得您想要的結果。

實際上您的代碼包含多個錯誤:

  1. 您無需多次調用soup object! 因為你可以拿起你的goal然后解析它。
  2. 如果可以使用一個 function,則不需要使用兩個功能。 .text.strip()類的東西實際上可以通過.get_text(strip=True)一次性處理!
  3. 有很多錯誤,但只需轉到我的代碼,以便您了解完整的想法。
from bs4 import BeautifulSoup
import requests


def main(url):
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'lxml')
    goal = soup.select_one('#divQuestion1')

    title = goal.select_one('.q-desc').get_text(strip=True)
    can = goal.select_one(
        'label[class$=correctquestions]').get_text(strip=True)
    wan = [x.get_text(strip=True)
           for x in goal.select('label[class$=click-target]')]
    print("Question: {}\nCorrect-Answer: {}\nWrong-Answers: {}".format(title, can, wan))


main('https://www.ilmkidunya.com/online-test/5th-class-science-english-meduim-mcqs-with-answers?startfrom=0&last=92')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM