我想收集GitHub用户从2004年到现在的每月贡献,如图所示。
,并将月度数据输出到CSV文件中,带有对应的月份列(例如,2022_10)。这些文本的XPath是:
# //*[@id="js-contribution-activity"]/div/div/div/div/details/summary/span[1]
下面是我的CSV文件(Df1):
|LinkedIn网站|GitHub网站|用户
-|-|
0|https://www.linkedin.com/in/chad-roberts-b86699/|https://github.com/crobby|Crobby
1|https://www.linkedin.com/in/grahamdumpleton/|https://github.com/GrahamDumpleton|GrahamDumpleton
以下是我到目前为止的最大努力:
for index, row in df1.iterrows():
try:
user = row['user']
except:
pass
for y in range(2004, 2023):
for m in range(1, 13):
try:
current_url = f'https://github.com/{user}?tab=overview&from={y}-{m}-01&to={y}-{m}-31'
print(current_url)
driver.get(current_url)
time.sleep(0.1)
contribution = driver.findElement(webdriver.By.xpath("//*[@id='js-contribution-activity']/div/div/div/div/details/summary/span[1]")).getText();
df1.loc[index, f'{str(y)}_{str(m)}'] = contribution
except:
pass
print(df1)
df1.to_csv('C:/Users/fredr/Desktop/output today.csv')
我搞不懂为什么没有产出。谢谢你的帮助。
2条答案
按热度按时间r3i60tvu1#
您需要使用
WebDriverWait
expected_conditions
显式等待。我看到那里有多个
contribution
字段,因此您需要将所有这些元素收集为一个列表,然后迭代该列表以提取每个元素文本。你需要改进你的定位器,它们应该尽可能短而清晰。
此外,您还在代码中混合了Java和Python。
getText()
和;
来自Java...试试这个:
flmtquvp2#
我没有尝试过使用Selify,但只使用了
requests
和lxml
这个XPath表达式看起来很管用。