如何获得这样的图像?
有什么链接结构可以获取它吗?
就像你看到的,有一些数据,例如用户名,RepoName,Logo,描述,N 个贡献,N 个问题,N 个星星,N 个分叉。
有时我只是将正常链接
https://github.com/user/repoName
粘贴到社交媒体中即可获取它,但我现在尝试社交媒体不会自动获取图像......
或者通常,我会去谷歌并用“我的名字”和“Github”搜索“我的仓库”的名称, 当我发现它时,我下载了它,但找不到它。
我也尝试在这里搜索,但没有解决任何问题: 如何使用 Git API 或任何逻辑获取我的 Github 存储库的社交预览图像链接?
我认为 GitHub 是自己做的,我想你之前已经创建了存储库,所以你需要等待。
但是如果您现在需要解决方案
这个可以用:
这里有一个例子:
按照说明/文档并将名称更改为您的名字 GitHub 配置文件/存储库
![Readme Card](https://github-readme-stats.vercel.app/api/pin/?username=laaouatni&repo=gcode.js)
就像你看到的一样,是一张图片,也是一个只有一颗星的新仓库,所以我认为你的也可以正常工作!
但是,如果您很久以前创建了存储库,则链接中的第一个答案(具有 4 个以上赞成票的答案)可以正常工作:https://stackoverflow.com/a/61948112/17716837 可以完成这项工作
如果没有用ingo返回镜像,这意味着GitHub暂时没有repo镜像
这里是完整的 python 代码,用于获取有关存储库的信息,包括预览图像
import requests
from bs4 import BeautifulSoup
def fetch_github_repo_info(repo_url):
# Extract the owner and repo name from the URL
parts = repo_url.rstrip('/').split('/')
owner = parts[-2]
repo = parts[-1]
# Fetch repository data from GitHub API
api_url = f"https://api.github.com/repos/{owner}/{repo}"
headers = {
'Accept': 'application/vnd.github.v3+json'
}
response = requests.get(api_url, headers=headers)
response.raise_for_status()
return response.json()
def fetch_github_preview_metadata(repo_url):
# Fetch the HTML content of the repository page
response = requests.get(repo_url)
response.raise_for_status()
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Extract Open Graph meta tags
og_title = soup.find('meta', property='og:title')
og_description = soup.find('meta', property='og:description')
og_image = soup.find('meta', property='og:image')
og_url = soup.find('meta', property='og:url')
# Extract content from the meta tags
title = og_title['content'] if og_title else 'No title available'
description = og_description['content'] if og_description else 'No description available'
image = og_image['content'] if og_image else 'No image available'
url = og_url['content'] if og_url else repo_url
return {
'title': title,
'description': description,
'image': image,
'url': url
}
def display_preview(metadata, topics):
print("Title: ", metadata['title'])
print("Description: ", metadata['description'])
print("Image URL: ", metadata['image'])
print("URL: ", metadata['url'])
print("Topics: ", ', '.join(topics))
if __name__ == '__main__':
repo_url = input("Enter GitHub repository URL: ")
repo_info = fetch_github_repo_info(repo_url)
metadata = fetch_github_preview_metadata(repo_url)
topics = repo_info.get('topics', [])[:5] # Get first 5 topics
display_preview(metadata, topics)