[英]How to extract text inside a <p> with a <small> tag
目前我正在抓取一个新闻网站进行研究工作,我使用 python+BeautifulSoup 如下
newsPageSoup = BeautifulSoup(newsPage.content, 'html.parser', from_encoding="iso 639-3")
newsText = newsPageSoup.find(class_='post-content').get_text()
从以下html代码中获取文本部分。 它工作得很好。
<p class="post-content">The completion of the sixth review, upon the granting of a waiver of non‑observance for the end‑June 2019, performance criterion on the primary balance</p>
但情况是我想从以下内容中提取文本部分安德鲁
<p class="text-primary" style="color : #2793ed; font:Arial, Helvetica, sans-serif; font-size:14px; font-weight:normal">Andrew <small style="color:#999999; font-size:11px">Friday, 13 December 2019 07:58 PM </small> </p>
所以我使用了与上面相同的python代码
readerNames = newsPageSoup.find(class_='text-primary').get_text()
但它给出了以下错误
AttributeError: 'NoneType' object has no attribute 'get_text'
我认为这是因为<p>
标签内的<small>
标签。所以他们有办法做到这一点吗? 请帮忙
您可以这样访问文本值:
import bs4
l = '<p class="text-primary" style="color : #2793ed; font:Arial, Helvetica, sans-serif; font-size:14px; font-weight:normal">Andrew <small style="color:#999999;font-size:11px">Friday, 13 December 2019 07:58 PM </small> </p>'
newsPageSoup = bs4.BeautifulSoup(l)
readerNames = newsPageSoup.find(class_='text-primary').text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.