【发布时间】:2021-06-07 21:54:24
【问题描述】:
新手来了!我正在使用 Python 3.8.3 并试图从附加的文本文件中删除标签listfile.txt
我想提取 3 个列表 - 文章的标题、出版日期和正文并删除标签。在下面的代码中,我已经能够从标题和出版日期中删除标签。但是,我无法从正文中正确删除所有标签。在文件中,正文以标签<div class="story-element story-element-text"> 开始,在下一个
任何帮助提取这部分文本将不胜感激!文章文本是非英文脚本,但所有的html标签都是英文的。
#opening text file which contains newspaper article information scraped off website using beautifulsoup
with open('listfile.txt', 'r', encoding='utf8') as my_file:
text = my_file.read()
print(text)
#removing tags and generating list of newspaper article titles
titles = re.findall('<h1.*?>(.*?)</h1>', text)
print(titles)
#removing tags and generating list of newspaper article publication dates
dates = re.findall('<div class=\"storyPageMetaData-m__publish-time__19bdV\"><span>(.*?)</span>', text)
print(dates)
#removing tags and generating list containing main text of articles. This is where the code is incorrect
bodytext= re.findall('<div class=\"story-element story-element-text\">(.*?)</div>', text)
print(bodytext)
【问题讨论】:
标签: python python-3.x regex tags findall