使用next_element从Github抓取日期



我想在提交部分从Github中抓取日期。 为了擦作者,我使用:

import urllib.request
import bs4 as bs
url = 'https://github.com/USER/PROJECT/commits/master'
source = urllib.request.urlopen(url).read()
soup = bs.BeautifulSoup(source,'lxml')
author = [author.text for author in soup.find_all("a",class_="commit-author")]

它返回了确实提交给定项目的作者,但我也想抓取每个提交的日期。我试过了:

dates = [date for date in soup.find("a",class_="commit-author").next_element.next_element.next_element]

不幸的是,它只返回 1 个日期。我知道这是由于使用find引起的,但是当我想使用find_all时,我得到了 AttributeError,即 ResultSet 对象没有属性next_element。那么有没有其他方法可以获取所有日期?

尝试在您最喜欢的用户和项目上以这种方式运行它,看看它是否有效:

author = soup.find_all("a",class_="commit-author")
for i in author:
print("Author: ",i.text,"Date: ",i.next_element.next_element.next_element.text)

最新更新