在Python中解析表格标签

时间:2019-12-08 10:09:19

标签: python beautifulsoup

我正在尝试使用python从HTML文件中提取数据。我正在尝试从文件中提取表内容。

以下是表格的HTML内容:

    <table class="radiobutton" id="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay" onclick="return false;">
   <tbody>
      <tr>
         <td>
            <input id="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_0" name="ctl00$bodyPlaceHolder$ctl00$Reg$rblTypeDisplay" type="radio" value="1" />
            <label for="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_0">Fitting</label>
         </td>
      </tr>
      <tr>
         <td>
            <input id="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_1" name="ctl00$bodyPlaceHolder$ctl00$Reg$rblTypeDisplay" type="radio" value="2" />
            <label for="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_1">Material</label>
         </td>
      </tr>
      <tr>
         <td>
            <input id="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_2" name="ctl00$bodyPlaceHolder$ctl00$Reg$rblTypeDisplay" type="radio" value="4" />
            <label for="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_2">Appliance</label>
         </td>
      </tr>
      <tr>
         <td>
            <input checked="checked" id="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_3" name="ctl00$bodyPlaceHolder$ctl00$Reg$rblTypeDisplay" type="radio" value="8" />
            <label for="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_3">Apparatus</label>
         </td>
      </tr>
      <tr>
         <td>
            <input id="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_4" name="ctl00$bodyPlaceHolder$ctl00$Reg$rblTypeDisplay" type="radio" value="16" />
            <label for="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_4">Other procedures</label>
         </td>
      </tr>
      <tr>
         <td>
            <input id="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_5" name="ctl00$bodyPlaceHolder$ctl00$Reg$rblTypeDisplay" type="radio" value="32" />
            <label for="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_5">Alternative fuel oils</label>
         </td>
      </tr>
      <tr>
         <td>
            <input id="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_6" name="ctl00$bodyPlaceHolder$ctl00$Reg$rblTypeDisplay" type="radio" value="64" />
            <label for="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_6">Other compliance method:</label>
         </td>
      </tr>
   </tbody>
</table>

下面是从代码中打印属性的python代码。

from bs4 import BeautifulSoup
from pyparsing import makeHTMLTags


with open('.\ABC.html', 'r') as read_file:
    data = read_file.read()

soup = BeautifulSoup(data, 'html.parser')
table = soup.find("table", attrs={"id":"ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay"})

spotterTag, spotterEndTag = makeHTMLTags("input")

for spotter in spotterTag.searchString(table):
    print(spotter.checked)
    print(spotter.id)

如何打印单选按钮的标签以及选中的属性?

考试:对于以下标签,应打印: 并为下面提到的Input标签“选中”:

<label for="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_0">Fitting</label>

<input checked="checked" id="ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay_3" name="ctl00$bodyPlaceHolder$ctl00$Reg$rblTypeDisplay" type="radio" value="8"/>

下面的代码可以工作,但是需要更好的解决方案:

from bs4 import BeautifulSoup
    from pyparsing import makeHTMLTags
    with open('.\ABC.html', 'r') as read_file:
        data = read_file.read()

    soup = BeautifulSoup(data, 'html.parser')
    table = soup.find("table", attrs={"id":"ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay"})

    spotterTag, spotterEndTag = makeHTMLTags("input")

    for spotter in spotterTag.searchString(table):
        if spotter.checked == 'checked':
            label = soup.find("label", attrs={"for":spotter.id})
            print(str(label)[str(label).find('>')+1:str(label).find('<',2)])
            print(spotter.checked)

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解正确,但是您是否想将输入和标签压缩在一起?如果是,则可以使用zip()函数。例如(data是您的HTML字符串):

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'html.parser')

print('{:^25} {:^15} {:^15}'.format('Text', 'Value', 'Checked'))
for inp, lbl in zip(soup.select('table#ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay input'),
                    soup.select('table#ctl00_bodyPlaceHolder_ctl00_Reg_rblTypeDisplay label')):
    print('{:<25} {:^15} {:^15}'.format(lbl.text, inp['value'], 'checked' if 'checked' in inp.attrs else '-'))

打印:

          Text                 Value          Checked    
Fitting                          1               -       
Material                         2               -       
Appliance                        4               -       
Apparatus                        8            checked    
Other procedures                16               -       
Alternative fuel oils           32               -       
Other compliance method:        64               -