【问题标题】:How to get the short and long description of a not installed pip package?如何获取未安装的 pip 包的简短描述和详细描述?
【发布时间】:2019-04-28 02:12:59
【问题描述】:

令我非常失望的是,pip 包管理器没有显示任何尚未安装的包的信息。获得任何东西的唯一方法似乎是 grep 使用pip search XXX |grep -i XXX 输出简短描述的输出。

  • 问:有没有简单的方法来获取 pip 包 XXX 的详细描述?
    (从命令行,无需安装。)

也许在 PyPI 中使用 wgetcurl 的聪明方法可行?


编辑: 我设法得到了一个 curl 单线:

这是 Bash 单行代码:

curl -sG -H 'Host: pypi.org' -H 'Accept: application/json' https://pypi.org/pypi/numpy/json | awk -F "description\":\"" '{ print $2 }' |cut -d ',' -f 1

# NumPy is a general-purpose array-processing package designed to...

但是,最好采用一种不同且更可靠的方法。

【问题讨论】:

  • 或者,如果您更喜欢更底层的方法,您可以直接查询 PyPI 的 API(SimpleJSON)。示例:python -c "import requests; data = requests.get('https://pypi.org/pypi/lxml/json').json(); print(data['info']['description'])".
  • @hoefling 太棒了!正是我想要的。请发布答案,以便我接受。 (yolk 也不错,但为了这个单一的目的有点臃肿过度。)

标签: python pip package pypi


【解决方案1】:

PyPI 提供了一个 API 来访问包元数据:

  • Simple:来自https://pypi.org/simple/<pkgname> 的响应是一个 HTML 页面,它是下载 URL 的列表,可以使用任何 HTML 解析器进行解析,例如 beautifulsouplxml

  • JSON:来自http://pypi.org/pypi/<pkgname>/json 的响应是可以使用任何 JSON 处理工具处理的 JSON 字符串。来自使用requests 的 cmets 示例:

    In [1]: import requests

    In [2]: data = requests.get('https://pypi.org/pypi/lxml/json').json()

    In [3]: data['info']['summary']
    Out[3]: 'Powerful and Pythonic XML processing library combining libxml2/libxslt with the ElementTree API.'

    In [4]: data['info']['description']
    Out[4]: 'lxml is a Pythonic, mature binding for the libxml2 and libxslt libraries.  It\nprovides safe and convenient access to these libraries using the ElementTree\nAPI.\n\nIt extends the ElementTree API significantly to offer support for XPath,\nRelaxNG, XML Schema, XSLT, C14N and much more.\n\nTo contact the project, go to the `project home page\n<http://lxml.de/>`_ or see our bug tracker at\nhttps://launchpad.net/lxml\n\nIn case you want to use the current in-development version of lxml,\nyou can get it from the github repository at\nhttps://github.com/lxml/lxml .  Note that this requires Cython to\nbuild the sources, see the build instructions on the project home\npage.  To the same end, running ``easy_install lxml==dev`` will\ninstall lxml from\nhttps://github.com/lxml/lxml/tarball/master#egg=lxml-dev if you have\nan appropriate version of Cython installed.\n\n\nAfter an official release of a new stable series, bug fixes may become\navailable at\nhttps://github.com/lxml/lxml/tree/lxml-4.2 .\nRunning ``easy_install lxml==4.2bugfix`` will install\nthe unreleased branch state from\nhttps://github.com/lxml/lxml/tarball/lxml-4.2#egg=lxml-4.2bugfix\nas soon as a maintenance branch has been established.  Note that this\nrequires Cython to be installed at an appropriate version for the build.\n\n4.2.5 (2018-09-09)\n==================\n\nBugs fixed\n----------\n\n* Javascript URLs that used URL escaping were not removed by the HTML cleaner.\n  Security problem found by Omar Eissa.\n\n\n\n\n'

命令行替代方案是使用yolk。安装

$ pip install yolk3k

以上查询lxml 以获取yolk 的摘要和描述:

$ yolk -M lxml -f summary,description
summary: Powerful and Pythonic XML processing library combining libxml2/libxslt with the ElementTree API.
description: lxml is a Pythonic, mature binding for the libxml2 and libxslt libraries.  It
provides safe and convenient access to these libraries using the ElementTree
API.

It extends the ElementTree API significantly to offer support for XPath,
RelaxNG, XML Schema, XSLT, C14N and much more.

...

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
相关资源
最近更新 更多