retrieving essential data from a webpage using pyt

2019-09-05 02:03发布

Following is a part of a webpage i downloaded with urlretrieve (urllib). I want to write only this data from the webpage given below in to another text file as:

ENGINEERING MATHEMATICS-IV, 4 ,36 ,40 , F
ENVIRONMENTAL STUDIES, 47, 36, 83 , p
...
..
.

likeways.. which module should i use?, which all commands??

thanx in advance..:)

part of the webpage

<td>ENGINEERING MATHEMATICS-IV</td>
        <td align=center>4</td>
        <td align=center>36</td>
        <td align=center>40</td>
        <td align=center>F</td>
    </tr>
<tr align=left bgcolor='#FFFFFF'>       <td>EIT402    </td>
        <td>ENVIRONMENTAL STUDIES</td>
        <td align=center>47</td>
        <td align=center>36</td>
        <td align=center>83</td>
        <td align=center>P</td>
    </tr>
<tr align=left bgcolor='#DA9292'>       <td>EIT403    </td>
        <td>SYSTEM PROGRAMMING</td>
        <td align=center>40</td>
        <td align=center>36</td>
        <td align=center>76</td>
        <td align=center>P</td>
    </tr>
<tr align=left bgcolor='#FFFFFF'>       <td>EIT404    </td>
        <td>MICROPROCESSOR BASED DESIGN</td>
        <td align=center>3</td>
        <td align=center>35</td>
        <td align=center>38</td>
        <td align=center>F</td>
    </tr>
<tr align=left bgcolor='#DA9292'>       <td>EIT405    </td>
        <td>PROGRAMMING PARADIGMS</td>
        <td align=center>42</td>
        <td align=center>36</td>
        <td align=center>78</td>
        <td align=center>P</td>
    </tr>
<tr align=left bgcolor='#FFFFFF'>       <td>EIT406    </td>
        <td>COMMUNICATION SYSTEMS</td>
        <td align=center>9</td>
        <td align=center>35</td>
        <td align=center>44</td>
        <td align=center>F</td>
    </tr>
<tr align=left bgcolor='#DA9292'>       <td>EIT407    </td>
        <td>DATA STRUCTURE LAB</td>
        <td align=center>10</td>
        <td align=center>35</td>
        <td align=center>45</td>
        <td align=center>F</td>
    </tr>
<tr align=left bgcolor='#FFFFFF'>       <td>EIT408    </td>
        <td>PROGRAMMING  ENVIRONMENTS  LAB</td>
        <td align=center>20</td>
        <td align=center>25</td>
        <td align=center>45</td>
        <td align=center>F</td>
    </tr>

1条回答
虎瘦雄心在
2楼-- · 2019-09-05 02:27
import urllib2
import BeautifulSoup

def main():
    infname  = 'htmltable.html'
    outfname = 'courses.txt'

    with open(infname) as inf:
        html = inf.read()

    doc   = BeautifulSoup.BeautifulSoup(html)
    table = doc.find('table',{'id':'content'})

    with open(outfname, 'w') as outf:
        for row in table.findAll('tr'):
            id,name,a,b,c,d = [cell.getText().strip() for cell in row.findAll('td')]
            outf.write("{name}, {a}, {b}, {c}, {d}\n".format(id=id, name=name, a=a, b=b, c=c, d=d))

if __name__=="__main__":
    main()            

works quite nicely if you assume the saved page starts like

<html><head><title>Data Table</title></head><body>
<table id='content'>
<tr align=left bgcolor='#FFFFFF'>       <td>EIT402    </td>
    <td>ENGINEERING MATHEMATICS-IV</td>
        <td align=center>4</td>
        <td align=center>36</td>
        <td align=center>40</td>
        <td align=center>F</td>
    </tr>

resulting in

ENGINEERING MATHEMATICS-IV, 4, 36, 40, F
ENVIRONMENTAL STUDIES, 47, 36, 83, P
SYSTEM PROGRAMMING, 40, 36, 76, P
MICROPROCESSOR BASED DESIGN, 3, 35, 38, F
PROGRAMMING PARADIGMS, 42, 36, 78, P
COMMUNICATION SYSTEMS, 9, 35, 44, F
DATA STRUCTURE LAB, 10, 35, 45, F
PROGRAMMING  ENVIRONMENTS  LAB, 20, 25, 45, F
查看更多
登录 后发表回答