2017-06-25 11 views
1

次のWebサイトでさまざまな値(質問に対する回答)を削り取ろうとしています。 'https://www.unpri.org/organisation/schroders-144205'、具体的にはウェブサイトに添付されたレポート。 https://reporting.unpri.org/surveys/PRI-Reporting-Framework-2016/6a23ed84-6bbf-4416-9d0b-6c49f63bc9ac/79894dbc337a40828d895f9402aa63de/html/2/?lang=&a=1bs4とpythonを使用して質問にチェックボックスとテキスト値を入力する

質問に答えることができない場合は、リストに空白を追加し、回答があれば回答を追加したいと思います。私は今、いろいろな方法で試してきました。

**質問は次のとおりです。 - レポートリンクの質問に対する回答を取り消し、質問に答えられない場合は、空白の要素を追加しますか?すべての回答または空白の要素をリストに追加する必要があります。

urls = ['https://www.unpri.org/organisation/schroders-144205'] 

for i in urls: 
    browser.visit(i) 
    window = browser.windows[0] 
    window.is_current = True 
    temp_list = [] 
    sourcenew = browser.html 
    soupnew = bs.BeautifulSoup(sourcenew, 'lxml') 
    temp_list.append(browser.url) 


for info in soupnew.find_all('span', class_ = 'org-type'): 
     string_com = str(info.text) 
     if len(string_com) == 16: 
      string_com = string_com.replace(' ', ' ')[1:-1] 
     elif len(string_com) == 11: 
      string_com = string_com.replace(' ', ' ')[1:-1] 
     elif len(string_com) == 10: 
      string_com = string_com.replace(' ', ' ')[1:-1] 
     elif len(string_com) == 12: 
      string_com = string_com.replace(' ', ' ')[1:-1] 
     elif len(string_com) == 13: 
      string_com = string_com.replace(' ', ' ')[1:-1] 
     else: 
      string_com = string_com.replace(' ', ' ')[40:-37] 
      temp_list.append(string_com) 
     if len(browser.find_by_xpath('//*[@id="main-    
content"]/div[2]/div/div/div[2]/p/a')) > 0: 
     browser.find_by_xpath('//*[@id="main- 
content"]/div[2]/div/div/div[2]/p/a').click() 
     time.sleep(2) 
     if len(browser.windows) > 1: 
      window = browser.windows[1] 
      window.is_current = True 

      sourcenew2 = browser.html 
      soupnew2 = bs.BeautifulSoup(sourcenew2, 'lxml') 

      parent = soupnew2.select('div[class="indent type_^ parent_S"]') 
      header_values = [] 

      for r in parent: 
       headers = r.find_all("h3") 
       for header in headers: 
        if header is not None: 
         fake_radio_button = r.find("img", src="/Style/img/checkedradio.png") 
         real_radio_button = r.select("input[checked='checked']") 

         if fake_radio_button == None: 
          if real_radio_button == None: 
           header_values.append('') 
          else: 
           if len(real_radio_button) > 0: 
            header_values.append(
            real_radio_button[0].attrs["data-original"]) 
           else: 
            header_values.append("") 
         else: 
          header_values.append(fake_radio_button.parent.find(
          "span").get_text(strip=True)) 





      text_values1 = [] 
      text_values2 = [] 



      for r in parent: 
       headers = r.find_all("h3") 
       for header in headers: 
        if header is not None: 
         fake_radio_button = r.find_all("img", src="/Style/img/checkedcheckbox.png") 
         real_radio_button = r.select("input[checked='checked']") 

         for b in fake_radio_button: 
          if b == None: 

           if real_radio_button == None: 
            text_values1.append('') 
          else: 
            if len(real_radio_button) > 0: 
             text_values1.append(
             real_radio_button[0].attrs["data-original"]) 
            else: 
             text_values1.append("") 
         else: 
          text_values1.append(b.parent.find(
            "span").get_text(strip=True)) 

      for r in parent: 
       headers = r.find_all("h3") 
       for header in headers: 
        if header is not None: 
         fake_radio_button1 = r.find("img", src="/Style/img/checkedcheckbox.png") 
         real_radio_button1 = r.select("input[checked='checked']") 

         if fake_radio_button1 == None: 

          if real_radio_button1 == None: 
           text_values2.append('') 
          else: 
           if len(real_radio_button1) > 0: 
           text_values2.append(
           real_radio_button1[0].attrs["data-original"]) 
           else: 
            text_values2.append("") 
         else: 

text_values2.append(fake_radio_button1.parent.find(
           "span").get_text(strip=True)) 

      text_values3 = [] 

      for r in parent: 
       headersss = r.find_all("span", class_ = 'n-text-p response') 
       for headerss in headersss: 
        if headerss is not None: 

         text_values3.append(headerss.get_text(strip=True)) 

      for r in parent: 
       headersss = r.find_all("span", class_ = 'response number') 
       for headerss in headersss: 
        if headerss is not None: 

         text_values3.append(headerss.get_text(strip=True)) 
        else: 
         text_values3.append('') 

      for r in parent: 
       headersss = r.find_all("span", class_ = 'response date') 
       for headerss in headersss: 
        if headerss is not None: 

         text_values3.append(headerss.get_text(strip=True)) 
        else: 
         text_values.append('') 

      list_final = [] 


      def f7(seq): 
       seen = set() 
       seen_add = seen.add 
       return [x for x in seq if not (x in seen or seen_add(x))] 


      list_final.append(f7(temp_list)) 
      list_final.append(f7(header_values)) 
      list_final.append(f7(text_values1)) 
      list_final.append(f7(text_values2)) 
      list_final.append(f7(text_values3)) 

      print(list_final) 
+1

どういうのですか? –

+0

質問が更新されました。私の悪い@MrGrj – Briyan

答えて

0

私はあなたのリンクを見ましたが、チェックボックスは実際のチェックボックスの要素ではなく、イメージですが、あなたが望むものは引き続き実行できます。あなたが見れば、

は、これが確認され、ラジオボタン

<img src="/Style/img/checkedradio.png" class="readradio"> 

の画像であり、これはそのため未チェックのもの

<img src="/Style/img/uncheckedradio.png" class="readradio"> 

用の画像タグである、あなたがオンまたはオフの答えを選ぶことができます

all_question_blocks = soup_obj.findAll("div",{"class":"question-block"}) 
    for question_block in all_question_blocks: 
     checked = question_block.findAll("a",{"src=":"/Style/img/checkedradio.png"} 
     #all your checked attributes, if empty then not answered 
     unchecked = question_block.findAll("a",{"src=":"/Style/img/uncheckedradio.png"} 

次に、HTML形式で上に移動することができます他の属性を抽出する場合は、親要素を1つずつ取得します。

希望すると便利です。

関連する問題