最近,我做了一个应用程序并将其上传到我的 GitHub 发布页面。我想制作一个检查更新以获取最新版本的功能(在发布页面中)。
我尝试使用 requests
模块来抓取我的发布页面以获取最新版本。这是我的代码的最小示例:
import requests
from lxml import etree
response = requests.get("https://github.com/v2ray/v2ray-core/releases")
html = etree.HTML(response.text)
Version = html.xpath("/html/body/div[4]/div/main/div[3]/div/div[2]/div[1]/div/div[2]/div[1]/div/div/a")
print(Version)
我认为 xpath 是正确的,因为我使用
chrome -> copy -> copy xpath
.但它给我一个[]
.它找不到最新版本。
最佳答案
一种直接的方法是使用 GitHub API ,很容易做到,不需要xpath。
网址应为:
https://api.github.com/repos/{owner}/{repo}/releases/latest
因此,如果您想获取存储库的最新发布版本。这是一个仅使用 requests
的简单示例模块:import requests
response = requests.get("https://api.github.com/repos/v2ray/v2ray-core/releases/latest")
print(response.json()["name"])
关于python - 如何仅使用 python-requests 在 Github 中获取最新版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60716016/