From e9da8bc0d52f847e0064ed26cb465dc4dd50b543 Mon Sep 17 00:00:00 2001 From: weixin_45693548 Date: Wed, 6 Sep 2023 17:46:00 +0800 Subject: [PATCH] Wed Sep 6 17:46:00 CST 2023 inscode --- search_pypi.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 search_pypi.py diff --git a/search_pypi.py b/search_pypi.py new file mode 100644 index 0000000..e6d187f --- /dev/null +++ b/search_pypi.py @@ -0,0 +1,31 @@ +from bs4 import BeautifulSoup +from typing import Dict, Optional, List +import requests + +def find_packages(query: str) -> Optional[List[Dict[str, str]]]: + query = '+'.join(query.split()) + response = requests.get(f'https://pypi.org/search/?q={query}') + if response.status_code != requests.codes.ok: + return None + + soup = BeautifulSoup(response.text, 'html.parser') + package_snippets = soup.find_all('a', class_='package-snippet') + search_results = list() + for package_snippet in package_snippets: + span_elems = package_snippet.find_all('span') + name = span_elems[0].text.strip() + version = span_elems[1].text.strip() + release_date = span_elems[2].text.strip() + desc = package_snippet.p.text.strip() + search_results.append( + dict( + name=name, + version=version, + release_date=release_date, + description=desc + ) + ) + return search_results + +if __name__ == '__main__': + print(find_packages('flask')) \ No newline at end of file -- GitLab