我有一个用户列表,我想要在Instagram上提取其关注者。我试图在“关注者”对话框中滚动,并将每个关注者追加到一个列表中,因此在继续为下一个用户执行相同操作之前,我最终获得了该用户的完整关注者列表。
这是我的代码:
for username in username_column:
currentRow=currentRow+1
userprofile = browser.get('https://www.instagram.com/' + username)
# Find the "followers" link, click it and wait 2 seconds
followersLink = browser.find_element_by_xpath("//ul/li[2]/a")
followersLink.click()
time.sleep(2)
# Find the dialog box
followersList = browser.find_element_by_css_selector("div[role=\'dialog\'] ul")
numberOfFollowersInList = len(followersList.find_elements_by_css_selector('li'))
followersList.click()
actionChain = webdriver.ActionChains(browser)
followers = []
# As long as the number of followers in the list is lower than number required
# press SPACE. After each press, we refresh the number of users we have in the list
# and print it.
while (numberOfFollowersInList < max):
actionChain.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
time.sleep(2)
numberOfFollowersInList = len(followersList.find_elements_by_css_selector('li'))
followersInList = followersList.find_elements_by_css_selector('li')
for element in followersInList:
followerData = element.text
print(followerData)
followers.append(followerData)
#print(followers)
followersList.click()
但是,这会返回很多重复的关注者数据,当我在Instagram上观看浏览器(驱动程序)时,就像卡住了一样,同时滚动并且不知道从滚动位置开始滚动一旦。我希望我写的清楚。
提前谢谢!