python - 如何使用 BeautifulSoup 处理特定标签中的不同格式

标签 python web-scraping beautifulsoup

我希望能够单独处理 HTML 文件中的某些标签。我的代码对于除两个标签之外的所有标签都工作正常(到目前为止)。这两行各有两行,而不是一行。这是我的代码:

from bs4 import BeautifulSoup

with open("F:/gpu.txt") as f:
    soup = BeautifulSoup(f)
    section = soup.find_all("td")
    #print(section[2])
    for section in section:
        if section.parent(text="GPU Name:"):
            print(section.text)
        elif section.parent(text="GPU Variant:"):
            print (section.text)
        elif section.parent(text="Bus Interface:"):
            print (section.text)
        elif section.parent(text="Transistors:"):
            print (section.text)

事情还在继续。然而,当我们说到“进程大小:”时,html 代码是不同的:

        <th>Process Size:</th>
      <td>
        Something 
                <br />
                Something Else
              </td>
    </tr>

对于所有其他情况,就像:

      <th>GPU Name:</th>
      <td>BLABLA</td>
    </tr>
        <tr>
      <th>GPU Variant:</th>
      <td>BLABLA</td>
    </tr>
        <tr>
      <th>Bus Interface:</th>
      <td>BLABLA</td>
    </tr>
    <tr>
      <th>Transistors:</th>
      <td>BLABLA</td>
    </tr>

因此,当我运行脚本时,我得到以下结果:

BLABLA
BLABLA

        Something 

                Something Else

BLABLA
BLABLA

我需要的是能够单独使用“Something”和“Something Else”(并且没有那些白线和空格)和/或使其仅是一件事,将其转换为字符串,例如:“某事/别的事”。

抱歉,如果我的信息不够清楚,英语不是我的母语。谢谢!

最佳答案

您可以找到节内的所有文本节点(使用 text=True )并使用 / 连接它们:

print('/'.join(item.strip() for item in section.find_all(text=True)))

示例:

from bs4 import BeautifulSoup

data = """
<table>
    <tr>
      <th>GPU Name:</th>
      <td>BLABLA</td>
    </tr>
        <tr>
      <th>GPU Variant:</th>
      <td>BLABLA</td>
    </tr>
        <tr>
      <th>Process Size: </th>
      <td>BLABLA</td>
    </tr>
    <tr>
      <th>Transistors:</th>
      <td>BLABLA</td>
    </tr>
    <tr>
      <th>Process Size:</th>
      <td>
        Something
                <br />
                Something Else
              </td>
    </tr>
</table>
"""

soup = BeautifulSoup(data)
section = soup.find_all("td")

for section in section:
    if section.parent(text="GPU Name:"):
        print(section.text)
    elif section.parent(text="GPU Variant:"):
        print (section.text)
    elif section.parent(text="Process Size:"):
        print ('/'.join(item.strip() for item in section.find_all(text=True)))
    elif section.parent(text="Transistors:"):
        print (section.text)

打印:

BLABLA
BLABLA
BLABLA
Something/Something Else

关于python - 如何使用 BeautifulSoup 处理特定标签中的不同格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25417776/

相关文章:

python - 使用 Beautifulsoup Python 从 URL 抓取数据时获取编码文本

python - 带有时间戳的 pandas 数据框插入到 mysql 的问题

python - 为什么我的兽人战斗停止了并且没有转到 move() 命令 :/

python - django注释和过滤

python selenium点击按钮xpath错误

python-3.x - 如何使用带有 urllib 的 urlopen 修复 Python 3 中的 HTTP 错误

python - 剥离 Html 标签 Findall + Beautiful Soup

python - 在Python中选择字典中的数据

python - 如何从网络抓取中创建 Pandas 数据框?

python - Beautiful Soup 根据类型提取内容