我为instagram写了一个脚本。我需要一个方法来返回一个关注者列表。我的粉丝不可见(只有10个),我必须向下滚动页面。我使用selenium webdriver和python来自动化这个过程。但不幸的是它并没有向下滚动。这是我的代码
def get_followers(self):
try:
driver.find_elements_by_css_selector('a._t98z6')[0].click()
except Exception as e:
print("Sorry, i don't have access to your followers: {0}".format(e))
else:
followers = []
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
try:
WebDriverWait(driver, 20).until(lambda x: x.find_element_by_css_selector("li._6e4x5"))
except:
break
followers = driver.find_elements_by_css_selector("a._2g7d5.notranslate._o5iw8")
return followers
任何解决方案都将非常感激。感谢。
答案 0 :(得分:2)
followers_panel = browser.find_element_by_xpath(
'//body/div[3]/div/div[2]/div/div[2]'
)
i = 1
while i < number_of_followers:
try:
follower = browser.find_element_by...
i += 1
except NoSuchElementException:
self.browser.execute_script(
"arguments[0].scrollTop = arguments[0].scrollHeight",followers_panel
)
通过这种方式,您可以抓取所有关注者。
答案 1 :(得分:0)
我也一直在尝试找到一种在关注者弹出窗口或对话框中滚动的方法,并且还没有找到一种集中于关注者框的方法来使用我通常在WebDriver中使用的滚动功能。我通过以下方法解决了这一问题:单击跟随者链接之后,只需发送ARROW_DOWN键,直到其一直向下滚动,这由计数列表和完整列表来表示,最后一个ARROW_DOWN键之后的完整列表保持不变。我相信有关对话框的内容是多余的,但是。这是我的代码:
def listfollowers (instaURL):
actions = ActionChains(driver)
assert isinstance(instaURL, object)
driver.get(instaURL)
time.sleep(3) # Let the user actually see something!
followersbutton = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='followers']")))
followersbutton.click()
time.sleep(2)
dialoguebox = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "body > div:nth-child(14) > div > div.zZYga > div > div.j6cq2 > ul > div")))
actions.move_to_element(dialoguebox)
actions.click()
actions.perform()
actions.reset_actions()
followerlist = []
scrollfollowercount = driver.find_elements_by_class_name("UYK0S")
while len(followerlist) < len(scrollfollowercount):
profiles = driver.find_elements_by_class_name("UYK0S") #the followers
for profile in profiles:
profileurl = profile.get_attribute('href')
followerlist.append(profileurl)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ARROW_DOWN)
actions.send_keys(Keys.ARROW_DOWN) #included a few times for good measure
actions.perform()
actions.reset_actions()
scrollfollowercount = driver.find_elements_by_class_name("UYK0S")
if len(scrollfollowercount) == len(followerlist):
break
print(followerlist)
因此对于我的MAIN部分,我具有登录位,然后调用我的函数listfollowers()
actions = ActionChains(driver)
driver.get("https://www.instagram.com/kapow.fitness/followers")
time.sleep(2)
username = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(1) > div")))
username.click()
actions.send_keys("djsynfinity")
actions.perform()
password = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#react-root > section > main > div > article > div > div:nth-child(1) > div > form > div:nth-child(2) > div > div.f0n8F")))
password.click()
actions.reset_actions()
actions.send_keys("Rox1rox2")
actions.send_keys(Keys.RETURN)
actions.perform()
time.sleep(2)
listfollowers("https://www.instagram.com/kapow.fitness/")
答案 2 :(得分:0)
我设法使用ActionChain进行滚动。但是随着列表的增加和计算机+ Internet的速度,它变得很慢。刚开始时,只有20-24个名称,每卷可获得10个名称。然后,当您单击最后一个元素时,我曾经单击最后一个元素,出现了10个新用户,您再次单击了最后一个元素。就像这样。
from selenium.webdriver.common.action_chains import ActionChains
def get_list():
element=[]
ran_num=int(random.randint(0,len(subject)-1))
search(subject[ran_num])
br.find_element_by_class_name("_e3il2").click() #open first image
time.sleep(2)
br.find_element_by_partial_link_text('likes').click()
time.sleep(2)
while len(element)<150:
element=br.find_elements_by_xpath("//*[@class='_9mmn5']")
i=len(element)-1
element[i].click()
time.sleep(1.50)
likers=br.find_elements_by_xpath("//*[@class='_2g7d5 notranslate _o5iw8']") #get the username
for i in range(len(likers)):
insta_id=likers[i].text
if (insta_id not in main_list):
main_list.append(insta_id)
with open ('to_like.txt','a') as f:
f.write('%s\n'%insta_id)
return()
答案 3 :(得分:0)
这是有效的方法。 不要更改睡眠时间,因为它们可以使滚动器重新加载新的关注者,而不必再次将其滚动回到顶部。
FList = driver.find_element_by_css_selector('div[role=\'dialog\'] ul')
numberOfFollowersInList = len(FList.find_elements_by_css_selector('li'))
FList.click()
actionChain = webdriver.ActionChains(driver)
time.sleep(random.randint(2,4))
while (numberOfFollowersInList < max):
actionChain.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
numberOfFollowersInList = len(FList.find_elements_by_css_selector('li'))
time.sleep(0.4)
print(numberOfFollowersInList)
actionChain.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
time.sleep(1)