我正在尝试使用请求包读取保存在 github 中的文本文件。 这是我正在使用的 python 代码:
import requests
url = 'https://github.com/...../filename'
page = requests.get(url)
print page.text
我不是在获取文本,而是在读取 HTML 标签。 如何从文件中读取文本而不是 HTML 标记?
最佳答案
已经有一些好的解决方案了,但是如果你使用requests
就跟着Github的API .
所有内容的端点是
GET /repos/:owner/:repo/contents/:path
但请记住,Github API 的默认行为是使用 base64
对内容进行编码。
在您的情况下,您将执行以下操作:
#!/usr/bin/env python3
import base64
import requests
url = 'https://api.github.com/repos/{user}/{repo_name}/contents/{path_to_file}'
req = requests.get(url)
if req.status_code == requests.codes.ok:
req = req.json() # the response is a JSON
# req is now a dict with keys: name, encoding, url, size ...
# and content. But it is encoded with base64.
content = base64.decodestring(req['content'])
else:
print('Content was not found.')
关于python - 使用 python 读取 github 文件返回 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38491722/