我有一个如下所示的网络文件: -
<table class="table "><col width="75px"></col><col width="1px"></col><tbody><tr class="tablerow style2" prodid="143012"><td class="pricecell"><span class="WebRupee">Rs.</span>
29
<br/><font style="font-size:smaller;font-weight:normal">
3 days
</font></td><td class="spacer"></td><td class="detailcell"><span><span class="label label-default" style="background-color:#3cb521;color:#fff;border:1px solid #3cb521">FULL TT</span>
</span><span><span class="label label-default" style="background-color:#fff;color:#0c7abc;border:1px solid #0c7abc">SMS</span>
</span><div style="padding-top:5px">
29
Full Talktime
</div><div class="detailtext"> 5 Local A2A SMS valid for 1 day </div></td></tr><tr class="tablerow style2" prodid="127535"><td class="pricecell"><span class="WebRupee">Rs.</span>
59
<br/><font style="font-size:smaller;font-weight:normal">
7 days
</font></td><td class="spacer"></td><td class="detailcell"><span><span class="label label-default" style="background-color:#3cb521;color:#fff;border:1px solid #3cb521">FULL TT</span>
</span><span><span class="label label-default" style="background-color:#fff;color:#0c7abc;border:1px solid #0c7abc">SMS</span>
</span><div style="padding-top:5px">
59
Full Talktime
</div><div class="detailtext"> 10 A2A SMS valid for 2 days </div></td></tr><tr class="tablerow style2" prodid="143025"><td class="pricecell"><span class="WebRupee">Rs.</span>
99
<br/><font style="font-size:smaller;font-weight:normal">
12 days
</font></td><td class="spacer"></td><td class="detailcell"><span><span class="label label-default" style="background-color:#3cb521;color:#fff;border:1px solid #3cb521">FULL TT</span>
</span><div style="padding-top:5px">
99
Full Talktime
</div><div class="detailtext"> 10 Local A2A SMS for 2 days only </div>
I want the values 29, 3 days,29 full talktime, 59, 7 days,59 full talktime etc.
但如果我尝试以下脚本,我会得到整个文档。
from bs4 import BeautifulSoup
import requests
r = requests.get("http://www.ireff.in/plans/airtel/karnataka")
data = r.text
soup = BeautifulSoup(data,"html.parser")
table = soup.find('table',{'class':'table'})
print(table)
我哪里错了? 我想特别得到这些价值。
如果表可以转换为json数组,那么这也会有所帮助。
答案 0 :(得分:1)
您需要深入挖掘以获取您所追求的具体数据。例如,要获取价格,请使用“pricecell”类搜索表格单元格。然后你可以得到包含的文本,然后解析它。一些示例代码(未测试):
price_cells = soup.findAll('td', {'class': 'pricecell'})
for price_cell in price_cells:
print(price_cell.text)