How to use python and beautifulsoup to divide one data intoto several data within one tag -
how use python , beautfulsoup divided 1 data several data within 1 tag, , year 14 year 2014, lot !
html:
<tbody id="tbody"> <tr id="tr0" class="tablehdrb1" align="center"> <td align="centre">c aug-14 - 15000</td> </tr> </tbody> code:
import urllib2 bs4 import beautifulsoup contenturl = "html:" soup = beautifulsoup(urllib2.urlopen(contenturl).read()) table = soup.find('tbody', attrs={'id': 'tbody'}) rows = table.findall("tr") tr in rows: cols = tr.find_all('td') data = [t.strip() tag in cols t in tag.find_all(text=true) if t.strip()] if len(data) == 1: print ';'.join(data) output above code
c aug-14 - 15000 ; expected output:
c ; aug ; 2014 ;15000 ;
since getting data . use split break , join
data=["c aug-14 - 15000"] alldata=[] in data: roidata=i.replace('-',' ') alldata.append(';'.join(roidata.split()))
Comments
Post a Comment