Python - multiple HTML page parsing to a single TXT file
I'm trying to parse specific content from an X number of HTML files to a single TXT file.
I have dirtily coded the following:
#!/usr/bin/python
import sys, mechanize, BeautifulSoup
def parsedata():
    ##do stuff
    prvitekst = soup.find(text='Random Number')
    prvikesh = prvitekst.findNextSiblings('td')
    drugitekst = soup.find(text='Random Month/Yeare')
    drugikesh = drugitekst.findNextSiblings('td')
    trechitekst = soup.find(text='Small Random Number')
    trechikesh = trechitekst.findNextSiblings('td')
    content = prvikesh + ";" + drugikesh + ";" + trechikesh + ";"
    writeFile(content);
def readFile(id):
    fi = open('result/page-%s.html' % id, 'r');
def writeFile(content):
    f = open('parsed.txt', 'a')
    f.write(content,"\n")
    f.close();
def main(start): 
    ##initialize vars
    id = int(start)
    page = readFile(id)
    soup = BeautifulSoup(page)
    print soup.prettify()
    readFile(id)
    for id in range(1000000000):
        parsedata()
        id = id + 1
        continue
    main(sys.argv[1]);
While the HTML part im trying to scrape looks like this
<tr style="height:40px; background-color:#f0f0f0;"><td colspan="4" class="textLargeBold" style="border-bottom: solid 1px #c4c4c4;">Random Details</td></tr>
<tr class="text">
<td align="left" valign="top"><b>Type</b></td>
<td align="left" valign="top">Color</td>
<td align="left" valign="top"><b>Random Number</b></td>
<td align="left" valign="top">213523123123123</td>
</tr>
<tr class="text"
<td align="left" valign="top"><b>Random Month/Year</b></td>
<td align="left" valign="top">12/13</td>
<td align="left" valign="top"><b>Small Random Number</b></td>
<td align="left" valign="top">13233</td>
</tr>
I want the de开发者_StackOverflow社区tails that come after the first one. thus if I'm searching for Typem I want it to show me Color.
and ofcourse in the end I'd like the gotten contents to be parsed in a format similar to CSV.
Type;Random Number;Random Month/Year
it should parse
Color;213523123123123;12/13
ofcourse in the code i made already im not searching for type, but that can easily be changed.
EDIT : Fixed intendation
html='''
<tr style="height:40px; background-color:#f0f0f0;"><td colspan="4" class="textLargeBold"  style="border-bottom: solid 1px #c4c4c4;">Random Details</td></tr> <tr class="text"> <td align="left" valign="top"><b>Type</b></td> <td align="left" valign="top">Color</td> <td align="left" valign="top"><b>Random Number</b></td> <td align="left" valign="top">213523123123123</td> </tr> <tr class="text" <td align="left" valign="top"><b>Random Month/Year</b></td> <td align="left" valign="top">12/13</td> <td align="left" valign="top"><b>Small Random Number</b></td> <td align="left" valign="top">13233</td> </tr> 
'''
import htql; 
a=[x for x in htql.HTQL(html, "<b sep>2-0 {name=<b>:tx; value=<td>1:tx } ")];
a
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论