2014-03-07 192 views
1

我開始進入美麗的世界,但我遇到了一個我似乎無法解決的問題。beautifulsoup找到特定標籤

this

網站,想解析項的值。此值可以在

<span class="gw2money-fragment">%value <i class="gw2money-silver">s</i></span> 

標籤和

<span class="gw2money-fragment">%value <i class="gw2money-copper">c</i></span>. 

獲取這些值不是一個問題之間找到,問題是檢查值是否是<i class="gw2money-silver"><i class="gw2money-copper">標籤內。

r = requests.get("http://www.gw2spidy.com/item/24467", proxies=proxyDict) 

soup = BeautifulSoup(r.text) 
checksoup = soup.find_all("span") 

numliste = [] 
for links in checksoup: 
    #print(links) 
    price = links.contents[0] 
    print(price)  

del numliste[0] 
print(numliste) 

這是我如何檢索當前的銅和銀值。

回答

3

我搜索gw2money-fragment類,然後進行測試,看看這個類是包含i元素是什麼:

for row in soup.find_all('tr'): 
    fragments = row.find_all('span', class_='gw2money-fragment') 
    if not fragments: 
     continue 
    label = row.th or row.td 
    print(label.text) 
    for fragment in fragments: 
     value = fragment.text.split()[0] 
     type_ = fragment.i['class'][0].rsplit('-', 1)[-1] 
     print('-', value, type_) 

演示:

>>> for row in soup.find_all('tr'): 
...  fragments = row.find_all('span', class_='gw2money-fragment') 
...  if not fragments: 
...   continue 
...  label = row.th or row.td 
...  print(label.text) 
...  for fragment in fragments: 
...   value = fragment.text.split()[0] 
...   type_ = fragment.i['class'][0].rsplit('-', 1)[-1] 
...   print('-', value, type_) 
... 
Sell Price: 
- 1 silver 
- 50 copper 
Buy Price: 
- 1 silver 
- 32 copper 

Topaz Nugget 

- 2 silver 
- 98 copper 
- 1 silver 
- 77 copper 

Sunstone Nugget 

- 3 silver 
- 17 copper 
- 2 silver 
- 15 copper 

Carnelian Nugget 

- 3 silver 
- 48 copper 
- 2 silver 
- 15 copper 

Peridot Nugget 

- 3 silver 
- 21 copper 
- 2 silver 
- 19 copper 

Adorned Tiger's Eye Jewel 

- 4 silver 
- 26 copper 
- 3 silver 
- 85 copper 

Tiger's Eye Copper Amulet of Precision 

- 6 silver 
- 26 copper 
- 4 silver 
- 51 copper 

Tiger's Eye Copper Ring of Precision 

- 6 silver 
- 46 copper 
- 4 silver 
- 76 copper 

Tiger's Eye Copper Stud of Precision 

- 7 silver 
- 2 copper 
- 5 silver 
- 43 copper 
+0

謝謝,這做到了。我仍然需要圍繞一些東西,但無論如何感謝:) – koki