2016-05-25 6 views
1

私は、BeautifulSoupを使ってこのツリーを解析して、私が探しているテキストを取得するのに苦労しています。私は、私が興味を持ってテーブルに終わる。BeautifulSoup Parsing

<td> 
     <a href="/inventoryCheck/16783169/?zip=93817"> 
     <h3> 
     Product A 
     </h3> 
     </a> 
     <a class="show_hide" href="/inventoryCheck/16783169/?zip=93817" style="color:red"> 
     Not Available 
     </a> 
     <br/> 
     Available at roughly 
     <a style="color:red"> 
     0% 
     </a> 
     of Stores Nationwide 
     </td> 
    </tr> 
    <tr> 
     <td style="padding:10px"> 
     <a href="/inventoryCheck/32201303/?zip=93817"> 
     <img src="/prod_image/32201303.jpg"/> 
     </a> 
     </td> 
     <td> 
     <a href="/inventoryCheck/32201303/?zip=93817"> 
     <h3> 
     Product B 
     </h3> 
     </a> 
     <a class="show_hide" href="/inventoryCheck/32201303/?zip=93817" style="color:red"> 
     Not Available 
     </a> 
     <br/> 
     Available at roughly 
     <a style="color:red"> 
     0% 
     </a> 
     of Stores Nationwide 
     </td> 
    </tr> 
    <tr> 
     <td style="padding:10px"> 
     <a href="/inventoryCheck/29236000/?zip=93817"> 
     <img src="/prod_image/29236000.jpg"/> 
     </a> 
     </td> 
     <td> 
     <a href="/inventoryCheck/29236000/?zip=93817"> 
     <h3> 
     Product C 
     </h3> 
     </a> 
     <a class="show_hide" href="/inventoryCheck/29236000/?zip=93817" style="color:red"> 
     Not Available 
     </a> 
     <br/> 
     Available at roughly 
     <a style="color:red"> 
     0% 
     </a> 
     of Stores Nationwide 
     </td> 
    </tr> 
    <tr> 
     <td style="padding:10px"> 
     <a href="/inventoryCheck/35536199/?zip=93817"> 
     <img src="/prod_image/35536199.jpg"/> 
     </a> 
     </td> 
     <td> 
     <a href="/inventoryCheck/35536199/?zip=93817"> 
     <h3> 
     Product D 
     </h3> 
     </a> 
     <a class="show_hide" href="/inventoryCheck/35536199/?zip=93817" style="color:red"> 
     Not Available 
     </a> 
     <br/> 
     Available at roughly 
     <a style="color:red"> 
     0% 
     </a> 
     of Stores Nationwide 
     </td> 

HTMLをprettifying後アン「H3」タグは、私はそのタグ内のテキストを取得したいので、製品を示し、H3は、私も、その後がある場合次の 'a'タグを調べて、その製品が利用可能かどうかを確認します。

最終的にはPythonでは製品の名前とその可用性のある行が必要です。

私は、.children、.descendantsなどを使用しようとしましたが、実際にはどこにも行きません。

誰でも手掛かりを提供できますか。

答えて

1

をお探しのものが.parent.nextSibling属性です。彼らは、あなたのh3タグに関連してツリーをナビゲートするのを助けます.SalesSoup(およびHTML/XML/etc。)について覚えておくべき重要なことは、それがツリーベースだということです。

td 
├─ a 
│  └─ h3 
├─ a 
├─ a 
└─ br 

だからあなたh3が最初aの子であり、そしてあなたが欲しいaの「姪/甥」:あなたのHTMLの大まかな構造は次のようです。だから、h3の親の次の兄弟を取得する必要があります。 BeautifulSoupのドキュメントにはnavigating the treeに関する良いセクションがあります。

この試してみてください:あなたはちょうどあなたが引く、CSSセレクタを使用することができ、製品と可用性が必要な場合

[{'name': u'Product A', 'availability': u'Not Available'}, 
    {'name': u'Product B', 'availability': u'Not Available'}, 
    {'name': u'Product C', 'availability': u'Not Available'}, 
    {'name': u'Product D', 'availability': u'Not Available'}] 
1

少なくとも、すべての要素がh3要素の中にあることがわかります。これらはあなたの製品です。次にshow_hideクラスのaエレメントとinventoryCheckエレメントのhrefの空きを得ることができます。コードの作業:提供されているサンプルHTMLの場合

from bs4 import BeautifulSoup, Tag 

data = """ 
your HTML 
""" 

soup = BeautifulSoup(data, "html.parser") 
for product in soup.find_all(lambda tag: tag and tag.name == "td" and tag.h3): 
    name = product.h3 
    availability = product.find("a", class_="show_hide", href=lambda href: href and "inventoryCheck" in href) 
    availability_stats = " ".join([item.get_text(strip=True) if isinstance(item, Tag) else item.strip() 
            for item in availability.next_siblings]) 

    print(name.get_text(strip=True), availability.get_text(strip=True), availability_stats.strip()) 

を、それが印刷になります。

(u'Product A', u'Not Available', u'Available at roughly 0% of Stores Nationwide') 
(u'Product B', u'Not Available', u'Available at roughly 0% of Stores Nationwide') 
(u'Product C', u'Not Available', u'Available at roughly 0% of Stores Nationwide') 
(u'Product D', u'Not Available', u'Available at roughly 0% of Stores Nationwide') 
1

:あなたは、各製品の辞書とitems配列を取得します

from bs4 import BeautifulSoup 

testdata = """ 
Your data here 
""" 

soup = BeautifulSoup(testdata) 

items = [] 

for item in soup.find_all('h3'): 
    name = item.text 
    availability = item.parent.nextSibling.text 

    items.append({'name': name, 'availability': availability}) 

soup = BeautifulSoup(h,"html.parser") 
h3s = soup.select("td h3") 
print([(h3.text.strip(), h3.find_next("a").text.strip()) for h3 in h3s]) 
:TDタグ内のH3タグは、アンカーを取得するために find_nextを使用します10

出力:

[(u'Product A', u'Not Available'), (u'Product B', u'Not Available'), (u'Product C', u'Not Available'), (u'Product D', u'Not Available')]