【问题标题】:Extract sentence from HTML using python使用python从HTML中提取句子
【发布时间】:2022-12-19 22:07:01
【问题描述】:

我使用 python(BeautifulSoup) 从 HTML 文件中提取了一个感兴趣的组件 我的代码:

import pandas as pd
import numpy as np
from lxml import html
from html.parser import HTMLParser
from bs4 import BeautifulSoup


HTMLFile = open("/home/kospsych/Desktop/projects/dark_web/file", "r")

index = HTMLFile.read()
S = BeautifulSoup(index, 'lxml')

Tag = S.select_one('.inner')


print(Tag)

这将打印以下结果:

<div class="inner" id="msg_550811">Does anyone know if it takes a set length of time to be given verified vendor status by sending a signed PGP message to the admin (in stead of paying the vendor bond)?<br/><br/>I'm regularly on Agora but I want to join the Abraxas club as well.<br/><br/>Mindful-Shaman</div>

和类型:

<class 'bs4.element.Tag'>

我想以某种方式删除 div 标签和 br 标签,只得到一个字符串,这将是上面的句子。 如何有效地做到这一点?

【问题讨论】:

    标签: python python-3.x beautifulsoup html-parsing


    【解决方案1】:

    您可以使用.text.get_text() 方法:

    from bs4 import BeautifulSoup
    
    soup = BeautifulSoup(
        """<div class="inner" id="msg_550811">Does anyone know if it takes a set length of time to be given verified vendor status by sending a signed PGP message to the admin (in stead of paying the vendor bond)?<br/><br/>I'm regularly on Agora but I want to join the Abraxas club as well.<br/><br/>Mindful-Shaman</div>""",
        "html.parser",
    )
    
    Tag = soup.select_one(".inner")
    print(Tag.get_text(strip=True, separator=" "))
    

    印刷:

    Does anyone know if it takes a set length of time to be given verified vendor status by sending a signed PGP message to the admin (in stead of paying the vendor bond)? I'm regularly on Agora but I want to join the Abraxas club as well. Mindful-Shaman
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多