Python和Selenium获取instagram中喜欢帖子的人的列表

时间:2019-09-18 09:48:20

标签: python-3.x selenium-webdriver

我是python的新手,所以请别咬我=) 接下来的问题是,我不明白如何使用python和selenium在instagram中滚动模态窗口。我的任务是吸引所有喜欢该帖子的人。现在,我在模态打开一个之后就卡住了

from lxml import html
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys



chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-setuid-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument('--headless')
driver = webdriver.Chrome('/usr/bin/chromedriver', 
chrome_options=chrome_options)


driver.get('https://www.instagram.com/p/B2Y2nwpCiYa/?igshid=sv6ovbyg07hx')
driver.implicitly_wait(0.7)

for elem in driver.find_elements_by_xpath(
      './/span[@class = "glyphsSpriteGrey_Close u-__7"]'):
    elem.click()
    print('close login')

button = driver.find_elements_by_xpath('.//button[@class = "sqdOP 
 yWX7d    _8A5w5    "]')
button[0].click()
driver.implicitly_wait(60)
#user_loc = By.CSS_SELECTOR, 
  'div.Igw0E.rBNOH.eGOV_.ybXk5._4EzTm.XfCBB.HVWg4'
user_loc = By.CSS_SELECTOR, 'button.sqdOP.L3NKy'
#user_loc = By.CSS_SELECTOR, 'div._7UhW9.xLCgt.MMzan.KV-D4.fDxYl'
# Wait for first XHR complete
wait(driver, 20).until(EC.visibility_of_element_located(user_loc))
# Get current length of user list
current_len = len(driver.find_elements(*user_loc))
print(current_len)
while True:
   driver.find_element(*user_loc).send_keys(Keys.END)
   try:
      wait(driver, 10).until(lambda x: 
      len(driver.find_elements(*user_loc)) > current_len)
      current_len = len(driver.find_elements(*user_loc))
 # # Return full list of songs

   except TimeoutException:
       user_list = [user for user in driver.find_elements(*user_loc)]
       break

 driver.get_screenshot_as_file('test.png')

 print(len(user_list))

0 个答案:

没有答案