在python中的N个单词之后拆分HTML

时间:2021-03-09 21:37:03

Is there any way to split a long string of HTML after N words? Obviously I could use:

有没有办法在N个单词之后拆分一长串HTML?显然我可以使用:

' '.join(foo.split(' ')[:n])

to get the first n words of a plain text string, but that might split in the middle of an html tag, and won't produce valid html because it won't close the tags that have been opened.

获取纯文本字符串的前n个单词,但可能会在html标记的中间分割,并且不会生成有效的html,因为它不会关闭已打开的标记。

I need to do this in a zope / plone site - if there is something as standard in those products that can do it, that would be ideal.

我需要在zope / plone站点中执行此操作 - 如果在那些可以执行此操作的产品中有某些标准,那将是理想的。

For example, say I have the text:

例如,说我有文字:

<p>This is some text with a 
  <a href="http://www.example.com/" title="Example link">
     bit of linked text in it
  </a>.
</p>

And I ask it to split after 5 words, it should return:

我要求它在5个单词之后拆分,它应该返回:

<p>This is some text with</p>

7 words:

<p>This is some text with a 
  <a href="http://www.example.com/" title="Example link">
     bit
  </a>
</p>

4 个解决方案

#1


6  

Take a look at the truncate_html_words function in django.utils.text. Even if you aren't using Django, the code there does exactly what you want.

看一下django.utils.text中的truncate_html_words函数。即使你没有使用Django,那里的代码也完全符合你的要求。

#2


3  

I've heard that Beautiful Soup is very good at parsing html. It will probably be able to help you get correct html out.

我听说Beautiful Soup非常善于解析HTML。它可能会帮助您获得正确的HTML。

#3


0  

I was going to mention the base HTMLParser that's built in Python, since I'm not sure what the end-result your trying to get to is, it may or may not get you there, you'll work with the handlers primarily

我要提到用Python构建的基础HTMLParser,因为我不确定你想要达到的最终结果是什么,它可能会或者可能不会让你到那里,你将主要使用处理程序

#4


0  

You can use a mix of regex, BeautifulSoup or Tidy (I prefer BeautifulSoup). The idea is simple - strip all the HTML tags first. Find the nth word (n=7 here), find the number of times the nth word appears in the string till n words - coz u are looking only for the last occurrence to be used for truncation.

你可以混合使用正则表达式,BeautifulSoup或Tidy(我更喜欢BeautifulSoup)。这个想法很简单 - 首先删除所有HTML标记。找到第n个单词(这里n = 7),找到第n个单词在字符串中出现的次数,直到n个单词 - coz u只查找最后一个用于截断的单词。

Here is a piece of code, though a bit messy but works

这是一段代码,虽然有点乱,但有效

import re
from BeautifulSoup import BeautifulSoup
import tidy

def remove_html_tags(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

input_string='<p>This is some text with a <a href="http://www.example.com/" '\
    'title="Example link">bit of linked text in it</a></p>'

s=remove_html_tags(input_string).split(' ')[:7]

###required to ensure that only the last occurrence of the nth word is                                                                                      
#  taken into account for truncating.                                                                                                                       
#  coz if the nth word could be 'a'/'and'/'is'....etc                                                                                                       
#  which may occur multiple times within n words                                                                                                            
temp=input_string
k=s.count(s[-1])
i=1
j=0
while i<=k:
    j+=temp.find(s[-1])
    temp=temp[j+len(s[-1]):]
    i+=1
####                                                                                                                                                        
output_string=input_string[:j+len(s[-1])]

print "\nBeautifulSoup\n", BeautifulSoup(output_string)
print "\nTidy\n", tidy.parseString(output_string)

The output is what u want

输出就是你想要的

BeautifulSoup
<p>This is some text with a <a href="http://www.example.com/" title="Example link">bit</a></p>

Tidy
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 6 November 2007), see www.w3.org">
<title></title>
</head>
<body>
<p>This is some text with a <a href="http://www.example.com/"
title="Example link">bit</a></p>
</body>
</html>

Hope this helps

希望这可以帮助

Edit: A better regex

编辑:一个更好的正则表达式

`p = re.compile(r'<[^<]*?>')`

#1


6  

Take a look at the truncate_html_words function in django.utils.text. Even if you aren't using Django, the code there does exactly what you want.

看一下django.utils.text中的truncate_html_words函数。即使你没有使用Django,那里的代码也完全符合你的要求。

#2


3  

I've heard that Beautiful Soup is very good at parsing html. It will probably be able to help you get correct html out.

我听说Beautiful Soup非常善于解析HTML。它可能会帮助您获得正确的HTML。

#3


0  

I was going to mention the base HTMLParser that's built in Python, since I'm not sure what the end-result your trying to get to is, it may or may not get you there, you'll work with the handlers primarily

我要提到用Python构建的基础HTMLParser,因为我不确定你想要达到的最终结果是什么,它可能会或者可能不会让你到那里,你将主要使用处理程序

#4


0  

You can use a mix of regex, BeautifulSoup or Tidy (I prefer BeautifulSoup). The idea is simple - strip all the HTML tags first. Find the nth word (n=7 here), find the number of times the nth word appears in the string till n words - coz u are looking only for the last occurrence to be used for truncation.

你可以混合使用正则表达式,BeautifulSoup或Tidy(我更喜欢BeautifulSoup)。这个想法很简单 - 首先删除所有HTML标记。找到第n个单词(这里n = 7),找到第n个单词在字符串中出现的次数,直到n个单词 - coz u只查找最后一个用于截断的单词。

Here is a piece of code, though a bit messy but works

这是一段代码,虽然有点乱,但有效

import re
from BeautifulSoup import BeautifulSoup
import tidy

def remove_html_tags(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

input_string='<p>This is some text with a <a href="http://www.example.com/" '\
    'title="Example link">bit of linked text in it</a></p>'

s=remove_html_tags(input_string).split(' ')[:7]

###required to ensure that only the last occurrence of the nth word is                                                                                      
#  taken into account for truncating.                                                                                                                       
#  coz if the nth word could be 'a'/'and'/'is'....etc                                                                                                       
#  which may occur multiple times within n words                                                                                                            
temp=input_string
k=s.count(s[-1])
i=1
j=0
while i<=k:
    j+=temp.find(s[-1])
    temp=temp[j+len(s[-1]):]
    i+=1
####                                                                                                                                                        
output_string=input_string[:j+len(s[-1])]

print "\nBeautifulSoup\n", BeautifulSoup(output_string)
print "\nTidy\n", tidy.parseString(output_string)

The output is what u want

输出就是你想要的

BeautifulSoup
<p>This is some text with a <a href="http://www.example.com/" title="Example link">bit</a></p>

Tidy
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 6 November 2007), see www.w3.org">
<title></title>
</head>
<body>
<p>This is some text with a <a href="http://www.example.com/"
title="Example link">bit</a></p>
</body>
</html>

Hope this helps

希望这可以帮助

Edit: A better regex

编辑:一个更好的正则表达式

`p = re.compile(r'<[^<]*?>')`