2017-06-15 26 views
0

嗨,我們希望在此表中獲得價格值,我嘗試了許多代碼,但我無法獲得。使用BeautifulSoup Python獲得更深的值表

<div class="row"> 
     <div class="col-xs-60 dephTableBuy"> 
      <table class="dephTableBuyTable"> 
       <thead> 
        <tr> 
         <td>M</td> 
         <td>F</td> 
        </tr> 
       </thead> 
       <tbody id="bidOrders-temp-inj"> 
          <tr class="orderRow" amount="1.3" price="9000" total="12000"> 
           <td class="amount forBuyFirstTH">1.34742743</td> 
           <td class="price forBuyLastTH"><span class='part1'>9290</span>.<span class='part2'>00</span> <span class="buyCircleAdv"></span></td> 
          </tr> 
          <tr class="orderRow" amount="0.2" price="9252.02" total="2466.10"> 
           <td class="amount forBuyFirstTH">0.2</td> 
           <td class="price forBuyLastTH"><span class='part1'>9,252</span>.<span class='part2'>02</span> <span class="buyCircleAdv"></span></td> 
          </tr> 

這裏是我的無用代碼:

table = soup.find_all("table",{"class": "dephTableBuyTable"}) 

for item in table: 
    print(item.contents[0].find_all("tr",{"class": "price"})[0].text) 

TY

+0

任何錯誤信息? – Ding

+0

AttributeError:'NavigableString'對象沒有屬性'find_all' –

+0

嘗試'item.find_all(「tr」,{「class」:「price」})[0] .text' – Ding

回答

2

存在包含價格在每TR標籤兩個地方:價格屬性第二TD手機

找到行:

tr = soup.find('table', {'class': 'dephTableBuyTable'}).find_all('tr', {'class': 'orderRow'}) 

爲了讓價格標籤屬性,只需使用row['price']

for row in tr: 
    print(row['price']) 

# 9000 
# 9252.02 

若要獲取td標籤的價格,你可以使用find得到td細胞,然後使用text屬性:

for row in tr: 
    print(row.find('td', {'class': 'price'}).text) 

# 9290.00 
# 9,252.02 
+1

非常有效。我越來越瘋狂:) –

+1

很酷。很高興它的作品。 – Psidom