How to get the latest release version in Github only use python-requests?

Viewed 5346

Recently,I make an app and upload it to my GitHub release page.I want to make a function to check update to get the latest version(in the release pages).

I try to use requests module to crawl my release page to get the latest version.Here it a minimal example of my code:

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)

I think the xpath is right,because I use chrome -> copy -> copy xpath.But it return me a [].it can't find the latest version.

1 Answers

A direct way is to use GitHub API, it is easy to do and don't need xpath.


The URL should be:

https://api.github.com/repos/{owner}/{repo}/releases/latest

So if you want to get the latest release version of the repository. Here is an easy example only using the requests module:

import requests

response = requests.get("https://api.github.com/repos/v2ray/v2ray-core/releases/latest")
print(response.json()["name"])
Related