Kev*_*ayo 4 html python 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)
Run Code Online (Sandbox Code Playgroud)
我认为 xpath 是正确的,因为我使用chrome -> copy -> copy xpath. 但它返回给我一个[].it 找不到最新版本。
一个直接的方法是使用Github API,它很容易做到,不需要 xpath。
网址应该是:
https://api.github.com/repos/{owner}/{target_repos}/releases/latest
Run Code Online (Sandbox Code Playgroud)
因此,如果您想获取存储库的最新发布版本。这里是一个简单的示例,仅使用requests模块:
import requests
response = requests.get("https://api.github.com/repos/v2ray/v2ray-core/releases/latest")
print(response.json()["name"])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1802 次 |
| 最近记录: |