查找Python regex并从lis中删除regex

2024-09-28 17:03:57 发布

您现在位置:Python中文网/ 问答频道 /正文

不久前我为自己构建了这个小RSS阅读器,我很高兴能更新它,将垃圾从描述标签中排除。我现在正忙着测试它,以便从描述标签中删除&;lt;(所有内容)&;gt;,我很难得到这个文件。你知道吗

到目前为止,我的代码是这样的

from re import findall
from Tkinter import *
from urllib import urlopen

disc = []
URL = 'http://feeds.sciencedaily.com/sciencedaily/matter_energy/engineering?format=xml'
O_W = urlopen(URL).read()

disc_ex = findall('<description>(.*)</description>',O_W)
for i in disc_ex:
    new_disc = i.replace(findall('&lt;(.*)&gt;',i),'')
    disc.extend([new_disc])

所以在我尝试删除一些垃圾文本的新代码行之前,我通常会让我的文本像这样通过

"Tailored DNA structures could find targeted cells and release their molecular payload selectively into the cells.&lt;img src="http://feeds.feedburner.com/~r/sciencedaily/matter_energy/engineering/~4/J1bTggGxFOY" height="1" width="1" alt=""/&gt;"

我想要的只是没有垃圾的文本,所以本质上只是:

"Tailored DNA structures could find targeted cells and release their molecular payload selectively into the cells."

有什么建议吗?你知道吗


Tags: 代码from文本importltgturl标签
1条回答
网友
1楼 · 发布于 2024-09-28 17:03:57

有几种解决方案,例如BeautifulSoup。要遵循您的想法,请避免使用“<;”…“>;”括号中的字符串,只需更改最后一行:

...
for i in disc_ex:
    new_disc = i.replace(findall('&lt;(.*)&gt;',i),'')
    disc.extend([re.sub(r'<(.*)/>','',new_disc)])

相关问题 更多 >