2017-11-29 4 views
0

私の質問は、HTMLをスクレープするためにbs4を使用すると、スクラップできない場合は::beforeのようになります。スクラップを含むウェブページ::前に

私は会社がページ内のどのSDGに貢献しているか知りたいと思います。 https://www.unglobalcompact.org/participation/report/cop/create-and-submit/active/395091 ただし、チェックマークはソースコードでは表示されません。

私は何をすればよいですか、それともウェブサイトからそれを掻くために何ができますか?

答えて

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がパーサーとして選択されている場合、このコードが正しく機能しないことに気付きました。

関連する問題