2017-11-29 2 views

답변

0

여기에 ::before:: 부분이 전혀 필요하지 않습니다. 선택된 요소와 선택되지 않은 요소는 서로 다른 클래스를가집니다. 선택된 요소는 selected_question이고 선택되지 않은 요소는 advanced_question입니다.

from bs4 import BeautifulSoup 
import requests 


url = "https://www.unglobalcompact.org/participation/report/cop/create-and-submit/active/395091" 
response = requests.get(url) 

soup = BeautifulSoup(response.content, "lxml") 

questions = soup.select("ul.questionnaire > li.question_group") 
for question in questions: 
    question_text = question.get_text(strip=True) 
    print(question_text) 

    answers = question.find_next_siblings("li") 
    for answer in answers: 
     answer_text = answer.get_text(strip=True) 
     is_selected = "selected_question" in answer.get("class", []) 

     print(answer_text, is_selected) 
    print("-----") 

인쇄겠습니까 :

당신은 같은 것을 사용하여 분석 할 수

Which of the following Sustainable Development Goals (SDGs) do the activities described in your COP address? [Select all that apply] 
SDG 1: End poverty in all its forms everywhere False 
SDG 2: End hunger, achieve food security and improved nutrition and promote sustainable agriculture False 
SDG 3: Ensure healthy lives and promote well-being for all at all ages True 
SDG 4: Ensure inclusive and equitable quality education and promote lifelong learning opportunities for all False 
... 

주 선정 된 답을 인쇄 True.

html.parser을 파서로 선택하면이 코드가 제대로 작동하지 않는 것으로 나타났습니다.