Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中N个单词后拆分HTML_Python_Html_Plone_Zope - Fatal编程技术网

python中N个单词后拆分HTML

python中N个单词后拆分HTML,python,html,plone,zope,Python,Html,Plone,Zope,有没有办法在N个单词后拆分一个长字符串的HTML?显然,我可以使用: ' '.join(foo.split(' ')[:n]) 获取纯文本字符串的第一个n个字,但是它可能在HTML标记中间分裂,并且不会生成有效的HTML,因为它不会关闭已打开的标签。 我需要在zope/plone网站上做这件事——如果这些产品中有标准的东西可以做到这一点,那将是理想的 例如,假设我有文本: <p>This is some text with a <a href="http://www.e

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

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

获取纯文本字符串的第一个n个字,但是它可能在HTML标记中间分裂,并且不会生成有效的HTML,因为它不会关闭已打开的标签。 我需要在zope/plone网站上做这件事——如果这些产品中有标准的东西可以做到这一点,那将是理想的

例如,假设我有文本:

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

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

<p>This is some text with</p>
这是一些带有

7个字:

<p>This is some text with a 
  <a href="http://www.example.com/" title="Example link">
     bit
  </a>
</p>
这是一些带有


我听说它非常擅长解析html。它可能会帮助您获得正确的html。

我要提到的是Python中构建的基础,因为我不确定您试图达到的最终结果是什么,它可能会或可能不会让您达到目标,您将主要使用处理程序。

看看django.utils.text中的函数。即使您没有使用Django,那里的代码也会完全满足您的需要。

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

下面是一段代码,虽然有点凌乱,但可以正常工作

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)
重新导入
从BeautifulSoup导入BeautifulSoup
进口整洁
def删除html标签(数据):
p=重新编译(r“”)
返回p.sub(“”,数据)
input_string='这是一些带有

的文本 s=删除html标签(输入字符串)。拆分(“”)[:7] ###需要确保只保留第n个单词的最后一次出现 #考虑到截断。 #因为如果第n个单词可以是“a”和“/”是“…”等等 #在n个单词内可能出现多次 temp=输入字符串 k=s.计数(s[-1]) i=1 j=0
而我想你想忽略标签,这样他们就不会被分割?换句话说,只获取和拆分标记中不包含的文本。您是否希望拆分封装在标记之间的文档文本(例如,在和标记之间)?您为什么编写自己的函数来删除HTML标记而不使用Beautiful Soup的方法?
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>
`p = re.compile(r'<[^<]*?>')`