Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用BeautifulSoup提取HTML段落内的文本_Python_Html_Web Scraping_Beautifulsoup - Fatal编程技术网

在Python中使用BeautifulSoup提取HTML段落内的文本

在Python中使用BeautifulSoup提取HTML段落内的文本,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,标题:销售点威胁激增 严重程度:正常严重程度 发布日期:2014年12月4日星期四20:27 最近出现了几个新的销售点恶意软件家族,包括LusyPOS,… 分析:在过去的成功和媒体关注的鼓舞下,威胁行为者。。 这是我想在Python中使用BeautifulSoup从HTML页面中提取的一段。 我能够使用.children和.string方法在标记中获取值。 但我无法得到文本“几个新的销售点恶意软件fa…”,这是没有任何标签的段落内。我尝试使用soup.p.text、.get_text()等

标题:销售点威胁激增 严重程度:正常严重程度
发布日期:2014年12月4日星期四20:27 最近出现了几个新的销售点恶意软件家族,包括LusyPOS,…
分析:在过去的成功和媒体关注的鼓舞下,威胁行为者。。

这是我想在Python中使用BeautifulSoup从HTML页面中提取的一段。 我能够使用.children和.string方法在标记中获取值。 但我无法得到文本“几个新的销售点恶意软件fa…”,这是没有任何标签的段落内。我尝试使用soup.p.text、.get_text()等。。但是没有用。

使用with查找所有文本节点,并仅在父标记的直接子项中搜索:

<p>
    <a name="533660373"></a>
    <strong>Title: Point of Sale Threats Proliferate</strong><br />
    <strong>Severity: Normal Severity</strong><br />
    <strong>Published: Thursday, December 04, 2014 20:27</strong><br />
    Several new Point of Sale malware families have emerged recently, to include LusyPOS,..<br />
    <em>Analysis: Emboldened by past success and media attention, threat actors  ..</em>
    <br />
</p>
from bs4 import BeautifulSoup

data = """
<p>
    <a name="533660373"></a>
    <strong>Title: Point of Sale Threats Proliferate</strong><br />
    <strong>Severity: Normal Severity</strong><br />
    <strong>Published: Thursday, December 04, 2014 20:27</strong><br />
    Several new Point of Sale malware families have emerged recently, to include LusyPOS,..<br />
    <em>Analysis: Emboldened by past success and media attention, threat actors  ..</em>
    <br />
</p>
"""

soup = BeautifulSoup(data)
print ''.join(text.strip() for text in soup.p.find_all(text=True, recursive=False))
Several new Point of Sale malware families have emerged recently, to include LusyPOS,..