如何获取软件包的OS版本与架构信息并通过Launchpad API下载?
Hey there, let's break down your two requirements with practical, actionable solutions:
需求一:获取OS版本/架构信息并通过Launchpad API下载对应内容
第一步:抓取本地OS版本与架构信息
You can use Python's built-in platform library to pull this info without any extra dependencies. Here's a quick, usable snippet:
import platform # Get Ubuntu-specific OS details (adjust for other distros if needed) ubuntu_codename = platform.linux_distribution()[2] # e.g., "jammy", "focal" os_version = platform.version() # Full version string with kernel details # Get system architecture system_arch = platform.machine() # e.g., "x86_64", "aarch64" arch_bits = platform.architecture()[0] # e.g., "64bit"
第二步:通过Launchpad API下载匹配内容
Even with launchpadlib's metadata limitations, it works seamlessly for fetching package download links once you have the OS codename and architecture. Here's how to implement it:
from launchpadlib.launchpad import Launchpad # Authenticate anonymously (public package content doesn't require login) launchpad = Launchpad.login_anonymously( "package-download-script", "production", "~/launchpadlib-cache" ) # Target the Ubuntu distribution and your specific release series ubuntu = launchpad.distributions["ubuntu"] target_series = ubuntu.getSeries(name_or_version=ubuntu_codename) # Fetch a specific package (example: 'nginx') target_package = target_series.getSourcePackage(name="nginx") # Filter binary packages by your system architecture matching_binaries = target_package.getPublishedBinaries( distro_arch_series=target_series.getDistroArchSeries(archtag=system_arch), status="Published" ) # Grab the latest download URL latest_binary = matching_binaries[0] print(f"Download link for {target_package.name} ({system_arch}): {latest_binary.file_url}")
需求二:通过HTML解析获取OS版本系列与架构信息
Since launchpadlib doesn't expose this specific metadata and you couldn't find a dedicated tool, web scraping with HTML parsing is a solid workaround. Let's use requests and BeautifulSoup to extract Ubuntu's release codename/version pairs like your example:
Step-by-Step Implementation
- First, install the required tools:
pip install requests beautifulsoup4
- Use this script to scrape and format the release info:
import requests from bs4 import BeautifulSoup # Target Ubuntu's official release history page url = "https://wiki.ubuntu.com/Releases" response = requests.get(url) soup = BeautifulSoup(response.text, "html.parser") # Locate the release table (adjust selector if page structure changes) release_table = soup.find("table", class_="wikitable") release_rows = release_table.find_all("tr")[1:] # Skip the header row # Extract codename and version pairs into your desired format os_series_list = [] for row in release_rows: columns = row.find_all("td") if len(columns) >= 2: codename = columns[0].text.strip().lower() version = columns[1].text.strip() os_series_list.append((codename, version)) # Print the result (matches your example structure) print(os_series_list)
Key Notes:
- Always check the target website's
robots.txtbefore scraping to ensure it's allowed. - Page layouts can change over time, so you may need to update CSS selectors if the release table's class or structure shifts.
- For architecture info tied to specific packages, extend this approach by scraping the package's Launchpad page (e.g., the binary packages tab) and extracting architecture tags from the HTML elements.
内容的提问来源于stack exchange,提问作者Lee Sihun
相关产品推荐
相关产品推荐

