我正在尝试废弃(span class =' Number')。代码在我正在废弃的页面上看起来像这样:
<div id="DetailMainBox">
<table>
<tr>
<td rowspan="2" class="styleA">
<span class="UP">99 </span><span class="Change">10.00 (-0.1%)</span>
<span class="Portfolio"><a href="../../members/index.php" class="ThemeColor" target="_blank">Menu<img src="../images/more.gif" width="11" height="11" border="0" align="absmiddle" /></a></span>
</td>
<td class="styleB">Max Quantity<span class="RT"></span><br>
<span class="Number">100.000</span></span> </td>
<td class="styleB">Average Quantity<span class="RT"></span><br />
<span class="Number">822</span></td>
<td class="styleB">Previous Order<br />
<span class="Number">96</span></td>
<td class="styleB">Max Price<br />
<span class="Number">104</span></td>
<td class="styleB">Number of Trades<br />
<span class="Number">383</span></td>
</tr>
<tr>
<td class="styleB">Min Price<span class="RT"></span><br>
<span class="Number">59</span></td>
<td class="styleB">Total Amount<span class="RT"></span><br />
<span class="Number">800</span></td>
<td class="styleB">Start<br />
<span class="Number">10</span></td>
<td class="styleB">Low<br />
<span class="Number">98 </span></td>
我曾尝试使用Beautifulsoup来抓取数据。但是,它在屏幕上返回Nothing
from bs4 import BeautifulSoup
html = response.content
soup = BeautifulSoup(html,"html.parser")
title = soup.select('td.styleB')[0].next_sibling
title1 = soup.find_all('span', attrs={'class': 'Number'}).next_sibling
print(title1)
我希望我可以按如下方式检索号码: 最大数量:100 平均数量:822 先前的订单:96 最高价格:104 交易次数:383 最低价格:59 总金额:800 开始:10 低:98
请告知处理查询时我的代码有什么问题。谢谢
答案 0 :(得分:1)
可能的解决方案如下 - 深入了解bs4,您可以获得更高效,更优雅的解决方案:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("bs4_test.html"), "html.parser")
tds = soup.select('td.styleB')
for td in tds:
num = td.select('span.Number')
for i in num:
print ("{0} : {1}".format(td.next_element, i.string))
bs4_test.html
是你分享的html - 上面的行输出:
Max Quantity : 100.000
Average Quantity : 822
Previous Order : 96
Max Price : 104
Number of Trades : 383
Min Price : 59
Total Amount : 800
Start : 10
Low : 98
bs4_test.html供参考:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="DetailMainBox">
<table>
<tr>
<td class="styleA" rowspan="2"><span class=
"UP">99 </span><span class="Change">10.00 (-0.1%)</span>
<span class="Portfolio"><a class="ThemeColor" href=
"../../members/index.php" target="_blank">Menu<img align=
"absmiddle" border="0" height="11" src="../images/more.gif"
width="11"></a></span></td>
<td class="styleB">Max Quantity<span class="RT"></span><br>
<span class="Number">100.000</span></td>
<td class="styleB">Average Quantity<span class="RT"></span><br>
<span class="Number">822</span></td>
<td class="styleB">Previous Order<br>
<span class="Number">96</span></td>
<td class="styleB">Max Price<br>
<span class="Number">104</span></td>
<td class="styleB">Number of Trades<br>
<span class="Number">383</span></td>
</tr>
<tr>
<td class="styleB">Min Price<span class="RT"></span><br>
<span class="Number">59</span></td>
<td class="styleB">Total Amount<span class="RT"></span><br>
<span class="Number">800</span></td>
<td class="styleB">Start<br>
<span class="Number">10</span></td>
<td class="styleB">Low<br>
<span class="Number">98</span></td>
</tr>
</table>
</div>
</body>