公开Github回购的网络垃圾问题

2024-10-04 01:22:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从公共Github repo(https://github.com/stlrda/redb_python/tree/master/python/DAGs)中获取每个文件的名称和日期时间。我在下面发布的代码可以工作,但不是所有时间都可以。有时,当它运行DAGs[counter]['age'] = x.find('.no-wrap')[0].attrs['datetime']行时,会出现索引超出范围的错误。我很困惑为什么这段代码有时会起作用,而其他时候却找不到日期时间。有没有办法解决这个问题,找到每次跑步的日期时间

session = HTMLSession()
r = session.get('https://github.com/stlrda/redb_python/tree/master/python/DAGs')

div = r.html.find('tbody', first=True)
title = div.find('.content')

DAGs = []

#Grab the names of each DAG in the repo
for x in range((len(title))):

    if x == 0:
        continue
    else:
        info = {"name": title[x].text}
        DAGs.append(info)

#Update the dictionary with the age of the DAG
gitTable = div.find('.js-navigation-item')

counter = 0
for x in gitTable:
    DAGs[counter]['age'] = x.find('.no-wrap')[0].attrs['datetime']
#     print (x.find('.no-wrap')[0].attrs['datetime'])
    counter+=1

代码失败时,gitTable变量包含以下内容:

[<Element 'tr' class=('js-navigation-item',)>,
 <Element 'tr' class=('js-navigation-item',)>,
 <Element 'tr' class=('js-navigation-item',)>,
 <Element 'tr' class=('js-navigation-item',)>]

gitTable列表中其中一项的html为:

>>>gitTable[0].html
'<tr class="js-navigation-item">\n<td class="icon">\n<svg aria-label="file" class="octicon octicon-file" height="16" role="img" version="1.1" viewbox="0 0 12 16" width="12"><path d="M6 5H2V4h4v1zM2 8h7V7H2v1zm0 2h7V9H2v1zm0 2h7v-1H2v1zm10-7.5V14c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V2c0-.55.45-1 1-1h7.5L12 4.5zM11 5L8 2H1v12h10V5z" fill-rule="evenodd"/></svg>\n<img alt="" class="spinner" height="16" src="https://github.githubassets.com/images/spinners/octocat-spinner-32.gif" width="16"/>\n</td>\n<td class="content">\n<span class="css-truncate css-truncate-target"><a class="js-navigation-open" href="/stlrda/redb_python/blob/master/python/DAGs/MigratetoPG_DAG.py" id="5554cd417ad3b8097206c9a0e81566d0-7416c3966dc565eb1b0115b89fa72116e4cc3ee6" title="MigratetoPG_DAG.py">MigratetoPG_DAG.py</a></span>\n</td>\n<td class="message">\n<span class="css-truncate css-truncate-target">\n</span>\n</td>\n<td class="age">\n<span class="css-truncate css-truncate-target"/>\n</td>\n</tr>'

Tags: the时间jsfinditemcsstrclass
1条回答
网友
1楼 · 发布于 2024-10-04 01:22:54

看起来我在尝试刮取GitHub时走了一条更加艰难的道路,完全忽略了他们的API

提交和内容端点能够为我提供所需的文件名和日期时间信息。下面是端点的示例

我找不到一个同时提供文件名和日期时间数据的端点,所以如果有人知道,请告诉我

日期时间>https://api.github.com/repos/github帐户/repo名称/commits?路径=文件夹路径

名称>https://api.github.com/repos/github帐户/repo名称/contents/文件夹路径

相关问题 更多 >