我有一个web应用程序,它从Tumblr中读取并重新格式化"reblog链“的格式。
使用Tumblr,post的评论被存储为HTML块引号。当用户响应上面的评论时,另一个级别被添加到区块引用链中,最终导致许多嵌套的reblog链。
下面是一个示例,说明"reblog链“在普通HTML中的外观:
<p><a class="tumblr_blog" href="http://chainsaw-police.tumblr.com/post/96158438802/example-tumblr-post">chainsaw-police</a>:</p><blockquote>
<p><a class="tumblr_blog" href="http://example-blog-domain.tumblr.com/post/96158384215/example-tumblr-post">example-blog-domain</a>:</p><blockquote>
<p>Here is an example of a Tumblr post.</p> <p>It can have multiple <p> elements sometimes. It may only have one, though, at other times.</p>
</blockquote>
<p>This is an example of a user “reblogging” a post. As you can see, the previous comment is stored above as a <blockquote>.</p>
</blockquote>
<p>This is another reblog. As you can see, all of the previous comments are stored as blockquotes, with earlier ones being residing deeper in the nest of blockquotes.</p>这就是渲染时的样子。
我希望能够重新格式化重新博客链,使其看起来更像这样:
example-blog-domain:这里是一个Tumblr帖子的例子。
有时它可以有多个元素。不过,在其他时候,它可能只有一个。
chainsaw-police: --这是一个用户“重新博客”帖子的例子。如您所见,前面的注释存储在上面。
example-blog-domain:,这是另一个博客。正如您所看到的,前面的所有注释都存储为块引号,而前面的注释位于更深的块引号的嵌套中。
我知道,这是一个令人难以置信的混乱的结构,因此,我为什么要写一些东西,使它更易读。
有没有任何方法来解释HTML并将重博客分割成单独的“评论”?例如,拥有一个具有用户名和注释的数组或dict就足够了。然而,在处理lxml和BeautifulSoup几个月后,我已经束手无策了。
如果有一种方法可以在CSS中实现,我对此深表怀疑,这是很好的。
各位,提前谢谢!
发布于 2014-08-31 05:54:54
reddit用户/u/joyeusenoelle在/r/LearnPython上回答了我的问题使用了一吨复杂的正则表达式,最终看起来更像是巫毒魔法,而不是文字处理脚本。
之后,我想我已经解决了这个问题,因为我的评论链很深。 在tf: text += tf.close() tf.close() text =text.replace(“n”,"") text = text.replace(">",">\n") text = text.replace("<","\n<") text =re.sub(“s*”,"",text) text = text.replace("\n","") text = text.replace("\n","\n") text = re.sub("","",text) text = re.sub("+?>",",text) text =text.replace(“,"") text = re.sub("\n+","",text) text =re.sub(”s{2,}","",文本= re.sub("\s*","",text.split( "“)位=”最新:“注释= [] for i in range(len(位)):temp =”j=0- (i+1) if (len(位)-i)> i: temp =“+ bitsi +”+ bitsj comments.append(temp) comments.reverse(),用于注释中的注释: print("%s“(注释)) print()
bits[0] = "Latest:"行可以更改为您希望显示的最新注释,您可能希望更改文本进入脚本的方式。 对于您提供的文本,这给了我: e:一些想法:这是在Python 3中,但是我认为,除了print语句之外,所有东西都应该在Python 2中工作。我尽可能地使用text.split(),因为直接字符串操作通常比正则表达式快,但在这里可能不合适。最后,有可能我为自己做的工作比我在替换部分所需要的要多,但在这一点上,我已经看了很长时间,找不出它是否可以被精简。
发布于 2014-08-30 09:41:41
我想CSS没有这样的功能。您需要通过lxml解析到结构,..。并渲染它。更简单的方法。您还可以使用regexp创建一个筛选器,它不会传递错误的html代码项。
https://stackoverflow.com/questions/25580939
复制相似问题