簡體   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