【问题标题】:Is there any way to sort things by date using requests and pandas?有没有办法使用请求和熊猫按日期对事物进行排序?
【发布时间】:2020-06-10 02:55:36
【问题描述】:

就像在主题中一样。我的代码看起来像这样。一切正常,但似乎字典在某处被剪掉了。最新的 CVE 来自 2016 年......而不是 2020 年。就像在下面的屏幕上一样。这里有什么问题?我怎样才能获得 2020 CVE? Pycharm 不能加载其余的吗?我尝试在cmd中运行,结果更糟糕,在2014年被砍掉了。我该怎么办?

import pandas as pd
import requests

keyword = 'oracle'
url = 'https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword={}'.format(keyword)
html_data = requests.get(url).text

df = pd.read_html(html_data)
data = df[2].to_dict(orient='records')

for dict in data:
    for key in dict:
        print(key, dict[key])

这就是它在 cve.mitre.org 上的样子

【问题讨论】:

    标签: python pandas python-requests


    【解决方案1】:

    最好将其用作DataFrame,然后您可以按列过滤:

    keyword = 'oracle'
    url = 'https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword={}'.format(keyword)
    html_data = requests.get(url).text
    
    df = pd.read_html(html_data)
    df = df[2]
    df['Year'] = df['Name'].str.split('-').str[1].astype(int)
    df = df[df['Year']>2016]
    print(df)
    
    
                      Name                                        Description  Year
    0        CVE-2020-9402  Django 1.11 before 1.11.29, 2.2 before 2.2.11,...  2020
    1        CVE-2020-9315  ** PRODUCT NOT SUPPORTED WHEN ASSIGNED ** Orac...  2020
    2        CVE-2020-9314  ** PRODUCT NOT SUPPORTED WHEN ASSIGNED ** Orac...  2020
    3        CVE-2020-8428  fs/namei.c in the Linux kernel before 5.5 has ...  2020
    4        CVE-2020-7221  mysql_install_db in MariaDB 10.4.7 through 10....  2020
    ...                ...                                                ...   ...
    2632    CVE-2017-10001  Vulnerability in the Oracle Hospitality Simpho...  2017
    2633  CVE-2017-1000030  Oracle, GlassFish Server Open Source Edition 3...  2017
    2634  CVE-2017-1000029  Oracle, GlassFish Server Open Source Edition 3...  2017
    2635  CVE-2017-1000028  Oracle, GlassFish Server Open Source Edition 4...  2017
    2636    CVE-2017-10000  Vulnerability in the Oracle Hospitality Report...  2017
    

    【讨论】:

    • 酷,如果可行,您可以将其标记为答案并考虑投票
    • 但是如果我尝试将它发送到数据库呢?我认为这种格式不像 python 的字典那样容易。我是对还是错?有什么建议吗?
    • 我不想只分析这些数据,我想将其发送到数据库,然后能够通过 Web 应用程序将其导出为不同的格式。希望你能理解我。
    • 您应该能够轻松地将 pandas 数据框添加到任何数据库。查看 pyodbc。
    • 好的,我会检查每一种可能性,我会给你一个反馈。祝我好运。
    猜你喜欢
    • 2018-07-05
    • 2020-03-16
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2021-11-22
    • 2014-05-24
    相关资源
    最近更新 更多