2017-06-09 7 views
0

以下のサイトでチェックボックスをオフ(または質問の回答)からすべてのラベル(テキスト)を取得しようとしています。bs4を使用してチェックボックスからテキストを取得するには?

しかし、代わりにテキストが表示されないようです。

私がスクラップをやったと思うやり方は、最初にすべてのリンクを集めることでした。右側では、ページを切り替えることができます。また、すべてのリンクの2倍を持っている...ここで

が、私は結果リストの次の出力を得る私の現在のコード(main_urlと呼ばれるだけでなく、そこにリンクを参照してください)

import bs4 as bs 
from splinter import Browser 
import time 



executable_path = {'executable_path' :'C:/users/chromedriver.exe'} 
browser = Browser('chrome', **executable_path) 

main_url = 'https://reporting.unpri.org/surveys/PRI-Reporting-Framework- 
2016/0ad07cdc-cfbc-4c5b-a79f- 
2b07e93d8521/79894dbc337a40828d895f9402aa63de/html/2/?lang=&a=1' 
browser.visit(main_url) 
source = browser.html 
soup = bs.BeautifulSoup(source, 'lxml') 
base_url = main_url[:-51] 
urls = [] 
print(base_url) 

for i in soup.find_all('div', class_ = 'accordion-inner n-accordion-link'): 
    for j in soup.find_all('a', class_ = 'tooltiper'): 
     urls.append(j['href']) 

print(urls) 

result = [] 
for k in urls: 
    ext = k[8:] 
    browser.visit(base_url + ext) 
    source1 = browser.html 
    soup1 = bs.BeautifulSoup(source1, 'lxml') 
    temp_list = [] 
    print(browser.url) 
    for img in soup1.find_all('img', class_ = 'readradio'): 
     for t in img['src']: 
      if t == '/Style/img/checkedradio.png': 
       for x in soup1.find_all('span', class_ = 'title'): 
        txt = str(x.string) 
        temp_list.append(txt) 

result.append(temp_list) 
print(result) 

あるこのリストのように思えます提案と

[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []] 

更新コード:

テキストを含むことになっています
import bs4 as bs 
from splinter import Browser 
import time 



executable_path = {'executable_path' 
:'/users/nichlasrasmussen/documents/webdrivers/phantomjs'} 
browser = Browser('phantomjs', **executable_path) 

main_url = 'https://reporting.unpri.org/surveys/PRI-Reporting-Framework- 
2016/0ad07cdc-cfbc-4c5b-a79f- 
2b07e93d8521/79894dbc337a40828d895f9402aa63de/html/2/?lang=&a=1' 
browser.visit(main_url) 
source = browser.html 
soup = bs.BeautifulSoup(source, 'lxml') 
base_url = main_url[:-51] 
urls = [] 
print(base_url) 

for i in soup.find_all('div', class_ = 'accordion-inner n-accordion-link'): 
    for j in soup.find_all('a', class_ = 'tooltiper'): 
     urls.append(j['href']) 

    print(urls) 

result = [] 
for k in urls: 
    ext = k[8:] 
    browser.visit(base_url + ext) 
    source1 = browser.html 
    soup1 = bs.BeautifulSoup(source1, 'lxml') 
    temp_list = [] 
    print(browser.url) 
    for label in soup1.find_all('label', class_='radio'): 
    t = label.find('img', class_='readradio') 
    if 'checkedradio' in t['src']: 
     content = soup1.find('span', class_='title') 
     temp_list.append(content.text) 

result.append(temp_list) 
print(result) 
+0

出力は何ですか? – codekaizer

+0

申し訳ありませんが、今質問を更新します。 – briyan

+0

'lxml'ではなく' html'にパーサを設定するとどうなりますか? – codekaizer

答えて

0

基本的には、imgspan.titleの両方の要素の親であるlabel.radioを参照できます。

ルートから始まる驚異的なループを実行する必要がありません(soup1

はこれを試してみてください:

for label in soup1.find_all('label', class_='radio'): 
    t = label.find('img', class_='readradio') 
    if t and '/Style/img/checkedradio.png' in t.get('src'): 
     content = label.find('span', class_='title') 
     temp_list.append(content.text) 
+0

"ResultSetオブジェクトには属性 '%s'がありません。おそらく単一項目のような項目のリストを扱っています。find()を呼び出すときにfind_all()を呼び出しましたか? %key AttributeError:ResultSetオブジェクトに 'text'属性がありません。あなたはたぶん単一のアイテムのようなアイテムのリストを扱っているでしょう。あなたはfind()を呼び出すときにfind_all()を呼び出しましたか?......最後の行について – briyan

+0

'[src']にcheckedradioがある場合: TypeError: 'NoneType'オブジェクトはサブスクリプトではありません – briyan

+0

これはsoup1.find_all( 'img'、class_ = 'readradio')のimgのすべてのループを置き換えます: – codekaizer

関連する問題