【问题标题】:Using BeautifulSoup to Match string in a html document and highlight it where ever it appears使用 BeautifulSoup 匹配 html 文档中的字符串并突出显示它出现的位置
【发布时间】:2018-07-16 05:44:25
【问题描述】:

我正在尝试匹配 HTML 文档中的字符串并特别突出显示它。 我使用 BeautifulSoup 和 html.parser。

到目前为止,我尝试的是使用 find_all() 并传递要匹配的字符串,但它没有帮助,因为它返回元素中存在的整个文本。

我希望您指导我如何定位文档中的特定字符串并突出显示它。

例如:标记:

 <p>Lorem  is simply dummy text of the printing and typesetting industry.</p> 
 <p>Lorem has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it

高亮后:标记:

 <p><mark>Lorem</mark> is simply dummy text of the printing and typesetting industry.</p> 
 <p><mark>Lorem</mark> has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it

预期输出:

Lorem 只是印刷和排版行业的虚拟文本。

Lorem 自 1500 年代以来一直是行业的标准虚拟文本,当时一位不知名的打印机采用了一种类型的厨房并将其打乱

如果我能得到一个字符串数组,我可以用标记标签替换它。

beautifulsoup 做到了这么远

 import urllib.request
 import re
 from bs4 import BeautifulSoup
 sauce = urllib.request.urlopen('http://courseweb.stthomas.edu/mjodonnell/cojo258/resume/simple_code.html').read()
 soup = BeautifulSoup(sauce, 'html.parser')


 body = soup.find('body')

 results = body.find_all(text=re.compile(r'bastyr', re.I))

 print(results)

【问题讨论】:

  • 阐述你的“突出”
  • @RomanPerekhrest 我编辑了问题。

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


【解决方案1】:

也许你可以试试这样的东西

soup = bs("<p>Lorem  is simply dummy text of the printing and typesetting industry.</p> ",'lxml')

# This is the word we want to put a tag around
special_word = 'Lorem'
content_orig = soup.p.text
split_content_orig = content_orig.split(special_word)

soup.p.string = ''  
soup.p.insert(len(soup.p), split_content_orig[0])

for i_word in split_content_orig[1:]:
# We need to create a new tag in every loop, otherwise it moves the tag around. Probably has something to do with each tag having a unique id()
    new_tag = soup.new_tag('mark')
    new_tag.string = special_word
    soup.p.insert(len(soup.p), new_tag)
    soup.p.insert(len(soup.p), i_word)

我遇到了类似的问题,在这里提出了我的问题:

Replace text with bold version in Beautiful Soup

也许其他人会回复它并找到更好的解决方案。但与此同时,我猜你可以使用它

【讨论】:

    【解决方案2】:

    如果您正在使用更复杂的 html,您可能不想替换 html 中任何位置的文本。这可能会破坏链接、图像、样式等。

    您只能替换文本的实例:

    
    def highlight_html(html, re_highlighter):
        soup = BeautifulSoup(html, 'html.parser')
        for tag in soup.strings:
            highlighted = re_highlighter.sub(r"<mark>\1</mark>", tag)
            if highlighted != tag:
                highligted_soup = BeautifulSoup(highlighted, 'html.parser')
                tag.replace_with(highligted_soup)
        return str(soup)
    
    # create your re rule as needed...
    re_highlighter = re.compile(r"Lorem...", flags=re.IGNORECASE)
    highlighted_html = highlight_html(html, re_highlight)
    
    

    【讨论】:

      猜你喜欢
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 2018-08-29
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多