Searching PyPI by topic

人盡茶涼 提交于 2019-12-03 15:37:46

问题


For every python package you can specify a list of classifiers. Among others there is a Topic classifier, that puts the package in the specified categories that can be browsed on PyPI.

For example, numpy has the following topics:

Topic :: Software Development
Topic :: Scientific/Engineering

Is there a way to search by topic programmatically using pip search or other third-party libraries?


回答1:


You can search PyPI by classifier via the XMLRPC API, using the browse() method:

try:
    import xmlrpclib  # Python 2
except ImportError:
    import xmlrpc.client as xmlrpclib  # Python 3

pypi = xmlrpclib.ServerProxy('http://pypi.python.org/pypi')

packages = pypi.browse([
    "Topic :: Software Development",
    "Topic :: Scientific/Engineering",
])

In the example above, packages contains a list of [package, version] lists for all packages which satisfy both the "Topic :: Software Development" and "Topic :: Scientific/Engineering" classifiers:

>>> {pkg: ver for pkg, ver in packages if "numpy" in pkg}
{
    'nose-numpyseterr': '0.1',
    'msgpack-numpy': '0.3.2',
    'numpy': '1.8.1',
    'idx2numpy': '1.0b'
}

From there, you can retrieve more information about a given release:

>>> release = pypi.release_data('numpy', '1.8.1')
>>> release['download_url']
'http://sourceforge.net/projects/numpy/files/NumPy/'
>>> release['platform']
'Windows,Linux,Solaris,Mac OS-X,Unix'
>>> release['downloads']
{
    'last_day': 5818,
    'last_month': 187688,
    'last_week': 44764
}

... etc.



来源:https://stackoverflow.com/questions/24289397/searching-pypi-by-topic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!