我有一个Bash脚本,可以下载源文件并安装软件包。我想从网站上获取软件包的最新版本,而不是手动更新脚本。Beautifulsoup似乎是理想的选择。例如,以Gnome软件包的网站-
https://download.gnome.org/sources/
说我想要gnome-common的最新版本。到目前为止,我已经提出了:
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import requests
import re
URL="https://download.gnome.org/sources/gnome-common"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
links=soup.find_all("a", href=re.compile("[0-9]"))
print(links)
这会从网站上获取所有软件包的版本,但还有更多,因此我不确定如何继续。我想要的只是版本号列表,然后我要确定最新版本(最高编号),然后将其添加到URL中,转到该站点并获取最新的源tarfile。
因此,对于gnome-common,最新版本是3.18,而https://download.gnome.org/sources/gnome-common/3.18/的最新版本是3.18.0,这是我将在Bash脚本中下载的。
根据该网页,最新版本位于表格的最后位置。因此,您可以通过index获得它-1
。然后,您可以通过.get_text()
方法获取URL并将其附加到当前URL。然后,您必须获取tar文件的链接。您可以使用该find_all()
功能来获取页面中的所有链接。然后,您可以使用循环查找以扩展名结尾的文件.tar.xz
。
然后使用urllib.request
下载文件:
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import requests
import re
import urllib.request
tar_file_link = ""
URL="https://download.gnome.org/sources/gnome-desktop/"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
links=soup.find_all("a", href=re.compile("[0-9]"))
latest_link = links[-1].get_text()
URL+=latest_link
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
rows = soup.find_all("a", text=re.compile(".tar.xz"))
tar_file_link = rows[-1].get_text()
if tar_file_link != "":
URL+=tar_file_link
print(URL)
urllib.request.urlretrieve(URL, tar_file_link)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句