2016-07-23 8 views
0

Ich habe ein Web-Dokument, das wie folgt aussieht: -Wie Wert von einer Klasse in schöner Suppe extrahieren

<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.

aber ich habe das ganze Dokument, wenn ich das versuchen unter dem Skript.

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) 

Wohin gehe ich falsch? Ich möchte diese Werte speziell erhalten.

ODER wenn die Tabelle in ein JSON-Array konvertiert werden kann, ist das auch hilfreich.

Antwort

1

Sie müssen tiefer graben, um die spezifischen Daten zu erhalten, nach denen Sie suchen. Um beispielsweise die Preise zu erhalten, suchen Sie nach den Tabellenzellen mit der Klasse "pricecell". Dann können Sie den enthaltenen Text erhalten und einfach analysieren. Einige Beispielcode (nicht getestet):

price_cells = soup.findAll('td', {'class': 'pricecell'}) 
for price_cell in price_cells: 
    print(price_cell.text) 
+0

findall nicht existiert, es ist findAll, wenn findall verwendet wird, wird das Skript nicht funktioniert, dieser Code funktioniert, bitte geben Sie mir etwas mehr Zeit, um die Antwort zu akzeptieren – penta