Py学习  »  Python

Python:如何通过BeatifullSoup在表行中通过字符串获取表行?

Niwla23 • 6 年前 • 2465 次点击  
<tr class="list even">
    <td class="list">5</td>
    <td class="list"><s>BI</s>→MU</td>
    <td class="list"><s>TEACHER</s>→TEACHER</td>
    <td class="list">Hello I am a Text</td>
    <td class="list">5b</td>
    <td class="list">BI3</td></tr>

这是表中的一行。有一些使用一行作为内联头,但idc用于它们。

所以,我只想得到包含字符串“8f”的行,而不仅仅是 td ,整个 tr 如果有多行包含字符串,它应该给我一个列表

for row in soup.find_all('tr', class_='list even'):
    if '5b' in row.text:
        print(row)
        for cell in row.find_all('td'):
            if "5b" not in cell.text:
                print(cell.text)

for row in soup.find_all('tr', class_='list odd'):
    if '5b' in row.text:
        for cell in row.find_all('td'):
            if "5b" not in cell.text:
                print(cell.text)

我现在有了这个,但是它在最后一个表字段之前添加了一个换行符: https://haste.thevillage.chat/foguvakixa.py

if "5b" not in cell.text:

这是因为如果我请求5d的数据,我不需要再知道它的5d,所以这只会过滤掉它自己的类

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51959
文章 [ 3 ]  |  最新文章 6 年前