繁体   English   中英

等效于 curl 命令的 python

[英]Equivalent python for curl command

我是 Curl package 的新手,并试图为以下 curl 命令找到等效的 python 代码:

$cves包含 cves 列表,我正在尝试从 api 获取搜索结果

for cve in $cves
do
    score=$(curl https://www.cvedetails.com/cve/$cve 2>&1 | grep "cvssbox" | tr "</div></td>" " "| rev | cut -b12-15 | rev)
    
done

一种采用 CVE 列表并创建将每个 CVE id 映射到其严重性分数的字典的解决方案。

import requests

baseurl = "https://www.cvedetails.com/cve"

baron_samedit = "CVE-2021-3156"
cves = [baron_samedit, "CVE-2021-20612", "CVE-2021-39979"]  # for the sake of the example

scores: dict[str, float] = {}
for cve in cves:
    r = requests.get(f"{baseurl}/{cve}")
    cvss_line = [x for x in r.text.splitlines() if "cvssbox" in x][0]
    scores[cve] = float(cvss_line.rsplit("</div>", 1)[0].rsplit(">", 1)[1])
    print(scores)  # {'CVE-2021-3156': 7.2, 'CVE-2021-20612': 7.8, 'CVE-2021-39979': 10.0}

这是我想到的最直接的解决方案(没有正则表达式)并且非常接近您的 shell 命令。

您可以深入研究其他有趣的解决方案:

  • 请求 API(如果有的话)(比抓取更好):看看这里
  • 如果需要抓取,请使用beautifulsoup

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM