【问题标题】:using loop to iterate the html table values python使用循环迭代html表值python
【发布时间】:2022-07-11 21:12:24
【问题描述】:

我正在尝试制作 python 脚本,使用 requests 和 bs4 获取学生的所有成绩。现在我在循环值时遇到问题

    subject = []
    f_quar = []
    s_quar = []
    g = sabaw.find('table', class_="table table-bordered table-striped")
    tbody = g.find('tbody')
    tr = tbody.find_all('tr')
    td = tbody.find_all('td')
    for i in tr:
        subject.append(i)
        print(i)
        print()
        for i in td:
            f_quar.append(i)
            print(i)
# here my goal is there are 4 list and are all connected like all the first value of the subject list, f_quar, s_quar and the average are linked together, like gen math(subject), 90(f_qaur), 90(s_qaur), and 90(average)

我是 pyton 的新手,所以循环是我的弱点。此外,代码似乎很错误,因为我需要主题、1stQ 等级、2ndQ 等级和平均水平。谢谢!。这是表格的html代码:

<table cellspacing="0" class="table table-bordered table-striped" id="tblss1" width="100%">
<thead>
<tr class="success">
<th style="text-align:center">SUBJECT</th>
<th style="text-align:center">1ST</th>
<th style="text-align:center">2ND</th>
<th style="text-align:center">AVE</th>
</tr>
</thead>
<tbody>
<tr>
<td style="color:purple"> GENERAL MATHEMATICS </td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center" style="color:blueviolet"> <strong> ##.00  </strong></td>
</tr>
<tr>
<td style="color:purple"> EARTH SCIENCE </td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center" style="color:blueviolet"> <strong> ##.00  </strong></td>
</tr>
<tr>
<td style="color:purple"> PHYSICAL EDUCATION AND HEALTH </td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center" style="color:blueviolet"> <strong> ##.50  </strong></td>
</tr>
<tr>
<td style="color:purple"> GENERAL CHEMISTRY 1 </td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center" style="color:blueviolet"> <strong> ##.00  </strong></td>
</tr>
<tr>
<td style="color:purple"> 21ST CENTURY LITERATURE </td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center" style="color:blueviolet"> <strong> ##.00  </strong></td>
</tr>
<tr>
<td style="color:purple"> READING AND WRITING </td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center" style="color:blueviolet"> <strong> ##.00  </strong></td>
</tr>
<tr>
<td style="color:purple"> GENERAL BIOLOGY 1 </td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center" style="color:blueviolet"> <strong> ##.00  </strong></td>
</tr>
<tr>
<td style="color:purple"> ENTREPRENEURSHIP </td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center"> <strong> ##.00 </strong></td>
<td align="center" style="color:blueviolet"> <strong> ##.50  </strong></td>
</tr>
</tbody>
</table>

【问题讨论】:

  • 你试过什么?我没看懂你的评论,链接是什么意思?

标签: python loops web-scraping python-requests lxml


【解决方案1】:

你使用 i 作为索引两次(外循环和内循环)。

我不确定解释器是否可以如此轻松地处理变量的“覆盖”,因为它可能会这样做,但在返回外部循环后,i 中的对象/迭代器光标可能会消失。

尝试更改内部循环索引变量名称,以不覆盖外部循环中的 i。

如果这不能解决您的问题,请更详细地描述您尝试实现的目标或看到的行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 2023-03-05
    • 1970-01-01
    • 2018-09-08
    • 2021-04-11
    • 2017-11-15
    相关资源
    最近更新 更多