我开始进入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)
这就是我目前检索铜和银的价值。
答案 0 :(得分: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