如何解决重新格式化从html提取的line / div
我目前无法重新格式化从网站提取的div。
这是我目前拥有的:
import chardet
with open('csv_of_text.csv','rb') as fraw: # "rb" = bytes mode
file_content = fraw.read()
chardet.detect(file_content)
但是我想要类似的东西:
魁北克省过去24小时内COVID-19演变的最新数据:87例新病例,使感染者总数达到61,004;在过去24小时内未发生死亡,在8月7日至12日之间发生了3次死亡,总共5,718次;与上一日相比,住院人数增加了2倍,累计总数为151。其中,有25例进行了重症监护,在8月12日进行了2项检查;增加了18,596例,总计1,428,286例。
我手动将其删除,但是是否存在一些耗时较少的东西?
解决方法
尝试类似的东西:
soup.select_one('div[class="ce-bodytext"]').text.strip()
那应该可以为您带来预期的输出。
,尝试一下
text = r'<div class=" frame frame-default frame-type-textmedia frame-layout-0" id="c47903"><a id="c47904"/><div class="ce-textpic ce-left ce-above"><div class="ce-bodytext"><p>The latest data of the evolution of COVID-19 over the past 24hours <strong>in Québec</strong> reveal:</p><ul><li>87new cases,bringing the total number of infected persons to61,004;</li><li>no deaths have occurred in the past 24hours,to which are added 3deaths which occurred between August7 and12,for a total of5,718;</li><li>the number of hospitalizations increased by2 compared to the previous day,for a cumulative total of151. Of these,25were in intensive care,an increase of2;</li><li>18,596tests were performed on August12,for a cumulative total of1,428,286.</li></ul></div></div></div>'
import re
print(re.sub(r'<[^<>]*>',' ',text))
,
尝试
str(bs4_obj.select('div')[0].text)
我不知道如何从unicode转换它, 但它摆脱了html标签。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。