Python 如何从没有标题的span标记中提取文本?
我试图从这个页面和其他一些页面中提取cve。这里是链接。 然而,cve似乎没有一个标题或任何东西来让我抓住它的文本。有办法做到这一点吗?以下是cve的html格式Python 如何从没有标题的span标记中提取文本?,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,我试图从这个页面和其他一些页面中提取cve。这里是链接。 然而,cve似乎没有一个标题或任何东西来让我抓住它的文本。有办法做到这一点吗?以下是cve的html格式 <section> <h4 class="u-m-t-2">Reference Information</h4> <section> <p><strong>CVE <!--
<section>
<h4 class="u-m-t-2">Reference Information</h4>
<section>
<p><strong>CVE
<!-- -->:
</strong><span><a href="/cve/CVE-2004-0804">CVE-2004-0804</a></span></p>
</section>
<section></section>
<div>
<section>
<p><strong>CERT
<!-- -->:
</strong><span><a target="_blank" rel="noopener noreferrer" href="https://www.kb.cert.org/vuls/id/555304">555304</a></span></p>
</section>
</div>
</section>
编辑:这是我目前与杰克·阿什顿建议的代码
import bs4 as bs
from urllib.request import urlopen, Request
import urllib
import sys
import re
with open("path to file with id's") as f:
for line in f:
active = line
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3'}
reg_url = "https://www.tenable.com/plugins/nessus/" + str(active)
req = Request(url=reg_url, headers=headers)
try:
source = urlopen(req).read()
except urllib.error.HTTPError as e:
if e.getcode() == 404: # check the return code
continue
if e.getcode() == 502:
continue
raise
soup = bs.BeautifulSoup(source,'lxml')
result = re.search(r"<span>(.*CVE.*)</span>", soup)
print(result[0])
使用python,这里有一种从该页面提取CVE的方法。我不确定CVE是什么,你想从中得到什么,但既然你知道CVE将出现在标签的href/文本中,你可以用regex搜索它。你可以根据自己的喜好修改它,这只是开始
import re
test = """
<section>
<h4 class="u-m-t-2">Reference Information</h4>
<section>
<p><strong>CVE
<!-- -->:
</strong><span><a href="/cve/CVE-2004-0804">CVE-2004-0804</a></span></p>
</section>
<section></section>
<div>
<section>
<p><strong>CERT
<!-- -->:
</strong><span><a target="_blank" rel="noopener noreferrer" href="https://www.kb.cert.org/vuls/id/555304">555304</a></span></p>
</section>
</div>
</section>
"""
result = re.search(r"<span>(.*CVE.*)</span>", test)
print(result[0]) # <a href="/cve/CVE-2004-0804">CVE-2004-0804</a>
使用python,这里有一种从该页面提取CVE的方法。我不确定CVE是什么,你想从中得到什么,但既然你知道CVE将出现在标签的href/文本中,你可以用regex搜索它。你可以根据自己的喜好修改它,这只是开始
import re
test = """
<section>
<h4 class="u-m-t-2">Reference Information</h4>
<section>
<p><strong>CVE
<!-- -->:
</strong><span><a href="/cve/CVE-2004-0804">CVE-2004-0804</a></span></p>
</section>
<section></section>
<div>
<section>
<p><strong>CERT
<!-- -->:
</strong><span><a target="_blank" rel="noopener noreferrer" href="https://www.kb.cert.org/vuls/id/555304">555304</a></span></p>
</section>
</div>
</section>
"""
result = re.search(r"<span>(.*CVE.*)</span>", test)
print(result[0]) # <a href="/cve/CVE-2004-0804">CVE-2004-0804</a>
从bs4导入BeautifulSoup
导入请求
def主URL:
r=requests.geturl
soup=BeautifulSoupr.content,“html.parser”
目标=[
f{url[:23]+x['href']}表示汤中的x。选择a[href^=\/cve\/cve-]
打印目标
mainhttps://www.tenable.com/plugins/nessus/19090
输出:
['https://www.tenable.com/cve/CVE-2004-0804']
从bs4导入BeautifulSoup
导入请求
def主URL:
r=requests.geturl
soup=BeautifulSoupr.content,“html.parser”
目标=[
f{url[:23]+x['href']}表示汤中的x。选择a[href^=\/cve\/cve-]
打印目标
mainhttps://www.tenable.com/plugins/nessus/19090
输出:
['https://www.tenable.com/cve/CVE-2004-0804']
印刷品:
CVE-2004-0804
或:
或:
印刷品:
CVE-2004-0804
或:
或:
如果没有要搜索的id,您可以编写正则表达式来查找CVE。可能有助于确定要使用的正则表达式。页面中的“毕竟”部分有一个强标题CVE,因此您可以按照您认为合适的模式抓取所有内容。@JackAshton在强标题之后抓取所有内容的最佳方式是什么?我是bs4新手,这些文档让我有点困惑。关于您对regex的编辑和使用,这不起作用,因为regex.search需要一个字符串。因此,您不能将soup传递给re.search,而是应该将其传递给soup.text。我认为我对bs4不是非常熟悉,您只需要将soup对象作为一个HTMLsoup字符串提供给regex。text不会返回标记,它应该是strsoup,没有id进行搜索。您可以编写一个regex表达式来查找CVE。可能有助于确定要使用的正则表达式。页面中的“毕竟”部分有一个强标题CVE,因此您可以按照您认为合适的模式抓取所有内容。@JackAshton在强标题之后抓取所有内容的最佳方式是什么?我是bs4新手,这些文档让我有点困惑。关于您对regex的编辑和使用,这不起作用,因为regex.search需要一个字符串。所以你不能把soup传递给re.search,你应该把它传递给soup.text。我想我对bs4不是很熟悉,你只需要把soup对象作为一个HTMLsoup字符串传递给regex。text不会返回标记,它应该是strsoup。我运行这个时,我发现一个类型错误。TypeError:预期的字符串或字节类似于对象,我将把我拥有的内容发布为editIt。当我运行此命令时,会出现类型错误。TypeError:预期的字符串或字节类似于对象,我将发布我的编辑内容