2017-06-23 16 views
2

htmlページの特定のセクションにあるすべてのタグを繰り返し処理したいと思います。私はBeautifulSoupを適用しましたが、私はそれだけでなく、セレンのライブラリだけで生きることができました。 のは、私は次のHTMLコードを持っているとしましょう:Python BeautifulSoup、タグと属性を反復する

<table id="myBSTable"> 
    <tr> 
     <th>Column A1</th> 
     <th>Column B1</th> 
     <th>Column C1</th> 
     <th>Column D1</th> 
     <th>Column E1</th> 
    </tr> 
    <tr> 
     <td data="First Column Data"></td> 
     <td data="Second Column Data"></td> 
     <td title="Title of the First Row">Value of Row 1</td> 
     <td>Beautiful 1</td> 
     <td>Soup 1</td> 
    </tr> 
    <tr> 
     <td></td> 
     <td data-g="Second Column Data"></td> 
     <td title="Title of the Second Row">Value of Row 2</td> 
     <td>Selenium 1</td> 
     <td>Rocks 1</td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td title="Title of the Third Row">Value of Row 3</td> 
     <td>Pyhon 1</td> 
     <td>Boulder 1</td> 
    </tr> 
    <tr> 
     <th>Column A2</th> 
     <th>Column B2</th> 
     <th>Column C2</th> 
     <th>Column D2</th> 
     <th>Column E2</th> 
    </tr> 
    <tr> 
     <td data="First Column Data"></td> 
     <td data="Second Column Data"></td> 
     <td title="Title of the First Row">Value of Row 1</td> 
     <td>Beautiful 2</td> 
     <td>Soup 2</td> 
    </tr> 
    <tr> 
     <td></td> 
     <td data-g="Second Column Data"></td> 
     <td title="Title of the Second Row">Value of Row 2</td> 
     <td>Selenium 2</td> 
     <td>Rocks 2</td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td title="Title of the Third Row">Value of Row 3 2</td> 
     <td>Pyhon 2</td> 
     <td>Boulder 2</td> 
    </tr> 
</table> 

私はこの部分が完璧に働いてい:

#Selenium libraries 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.common.exceptions import NoSuchElementException 

#BeautifulSoup 
from bs4 import BeautifulSoup 

browser = webdriver.Firefox() 
browser.get('http://urltoget.com') 

table = browser.find_element_by_id('myBSTable') 
bs_table = BeautifulSoup(table.get_attribute('innerHTML'), 'lxml') 
#So far so good 
rows = bs_table.findAll('tr') 
for tr in rows: 
    #Here is where I need help 
    #I want to iterate through all tags 
    #but I don't know if is going to be a th or a td 
    #At the same time I need to do something 
    #if is a td or a th 

そして、これは私が達成したいものです。

#The following is a pseudo code 
    for col in tr.tags: 
     print col.name, col.value 
     for attribute in col.attrs: 
      print " ", attribute.name, attribute.value 
    #End pseudo code 

おかげで、 Arty

答えて

1

tdまたはthに検索するタグのリストを指定します。属性のすべての要素を取得するためには、.attrs attributeを使用します。

rows = bs_table.find_all('tr') 
for row in rows: 
    cells = row.find_all(['td', 'th']) 
    for cell in cells: 
     print(cell.name, cell.attrs) 
+0

おかげで、あなたのソリューションはほとんど働いていました。そして、私はcell.attrsがうまく動作しなかったのでほとんど言う。少しの研究の後で、私は属性を繰り返し処理する以下のメソッドを見つけました:** 'for attr、cell.attrs.iteritems()の値: "属性 "、attr、value' ** 私は私がcells.attrsしか持っていないと動作しないので、iteritems()をattrsから取得します。 – Arty

3

代替ルーピング(アクションは、一番下にある):

html='''<table id="myBSTable"> 
    <tr> 
     <th>Column A1</th> 
     <th>Column B1</th> 
     <th>Column C1</th> 
     <th>Column D1</th> 
     <th>Column E1</th> 
    </tr> 
    <tr> 
     <td data="First Column Data"></td> 
     <td data="Second Column Data"></td> 
     <td title="Title of the First Row">Value of Row 1</td> 
     <td>Beautiful 1</td> 
     <td>Soup 1</td> 
    </tr> 
    <tr> 
     <td></td> 
     <td data-g="Second Column Data"></td> 
     <td title="Title of the Second Row">Value of Row 2</td> 
     <td>Selenium 1</td> 
     <td>Rocks 1</td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td title="Title of the Third Row">Value of Row 3</td> 
     <td>Pyhon 1</td> 
     <td>Boulder 1</td> 
    </tr> 
    <tr> 
     <th>Column A2</th> 
     <th>Column B2</th> 
     <th>Column C2</th> 
     <th>Column D2</th> 
     <th>Column E2</th> 
    </tr> 
    <tr> 
     <td data="First Column Data"></td> 
     <td data="Second Column Data"></td> 
     <td title="Title of the First Row">Value of Row 1</td> 
     <td>Beautiful 2</td> 
     <td>Soup 2</td> 
    </tr> 
    <tr> 
     <td></td> 
     <td data-g="Second Column Data"></td> 
     <td title="Title of the Second Row">Value of Row 2</td> 
     <td>Selenium 2</td> 
     <td>Rocks 2</td> 
    </tr> 
    <tr> 
     <td></td> 
     <td></td> 
     <td title="Title of the Third Row">Value of Row 3 2</td> 
     <td>Pyhon 2</td> 
     <td>Boulder 2</td> 
    </tr> 
</table>''' 

Soup = BeautifulSoup(html) 

rows = Soup.findAll('tr') 
for tr in rows: 
    for z in tr.children: 
     if z.name =='td': 
      do stuff1 
     if z.name == 'th': 
      do stuff2 
関連する問題