python - 如何仅使用 python-requests 在 Github 中获取最新版本?

标签 python html git github python-requests

最近,我做了一个应用程序并将其上传到我的 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/

相关文章:

javascript - 编码用户输入以存储在 MongoDB 中

Python:在 setup.py 中处理 numpy、scipy 和 matplotlib 依赖项

HTML图像问题

git - `Git add --patch` 多个文件 : go to hunk from previous file

git - 从远程 git 仓库中获取特定的提交

python - 调用存储函数或过程不会插入和保留更改

Python - 如何验证与 Fabric 模块的 SSH 连接?

jquery - 图片幻灯片上的文字 - jQuery cycle2

ios - 在 safari/iOS 上寻找 HTML 5 视频

git - git ssh config没有与名称关联的地址