【发布时间】:2019-08-25 10:38:15
【问题描述】:
我正试图从 wowhead 上刮一张桌子。问题是跨度类对于 2 种不同类型的数据是相同的(Sell for: 和 Buy for:)。
跨度所在的分区没有类,只有我在括号中写的刺。
我试过了
import requests
from bs4 import BeautifulSoup
import urllib.request
import re
import lxml
session = requests.session()
url1 = 'https://classicdb.ch/?item=4291'
response = session.get(url1)
soup = BeautifulSoup(response.text, 'lxml')
x=(soup.find('table', attrs={'class': "infobox"}))
y=x.find('td')
y=y.find('ul')
sell_silver = soup.select_one('div:contains("Sells for:") .moneysilver').text
buy_silver = y.select_one('div:contains("Buy for:") .moneysilver').text
print(sell_silver)
print(buy_silver)
但我只得到第一个跨度。
我拿到表格后的相关HTML是这样的
<div>
Buy for:
<span class="moneysilver">5</span>
</div>
</li>
<li>
<div>
Sells for:
<span class="moneysilver">1</span> <span class="moneycopper">25</span>
</div>
....
最终结果应该允许我将数据排序为
Buy_silver=5
Sell_silver=1
编辑以澄清问题并大声喊叫@QHarr
【问题讨论】:
标签: html python-3.x web-scraping beautifulsoup