Question

I started to get into beautifulsoup but I came to a problem that I can't seem to solve.

I have this

website and want to parse the value of the item. The value can be found between the

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

tags and

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

Getting those values wasn't a problem, the problem is checking whether the value is inside the <i class="gw2money-silver"> or <i class="gw2money-copper"> tags.

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)

This is how I retrieve the copper and silver values currently.

Was it helpful?

Solution

I'd search for the gw2money-fragment class and then test to see what the class is on the contained i element:

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_)

Demo:

>>> 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top