简体   繁体   中英

i'm trying to scrape names from instagram using selenium webdriver?

so i'm trying to get a list of name the commented on a post but the array comes back empty ? and i would like to get the list of names of people who liked the post but i still get the same result. i'v tried using the class name and nothing.

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

username ="________"
password ="_______"
search = "_______"

#getting webdriver path 
chrome_path =r"C:\Users\dr_m_\Desktop\chromedriver.exe"

#opening google chrome 
driver = webdriver.Chrome(chrome_path)

#going to instagram
driver.get("https://www.instagram.com/accounts/login/?hl=en")

#entering the username 
Users=driver.find_element_by_name('username')
Users.send_keys(username)
time.sleep(2)

#entering password
pas=driver.find_element_by_name('password')
pas.send_keys(password)

 #clicking the login button 
 driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/span""").click()

 time.sleep(5)

 #go to account 
 driver.get("https://www.instagram.com/"+search+"/")

 time.sleep(10)

 #open first pic
 driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/div/div[2]/article/div[1]/div/div[1]/div[1]/a/div""").click()

 time.sleep(5)

 #getting the XPATH of the comments 
 c_name=driver.find_elements_by_xpath("""//*[@id="react-root"]/section/main/div/div/article/div[2]/div[1]/ul/li[2]/div/div/div/a""")

#print name
for x in range(0,len(c_name)):
    print("name:")
    print(c_name[x])

i tried other ways but still get the same result. it wont go in the loop .

  1. Get all visible comments on photo - This may also include the profile owner comments and Load more comments button text

  2. Get count of comments

  3. for all elements found navigate inside all 'li' tags till tag 'a'

  4. get title attribute from tag 'a' which contains commenter name

Note - if condition is used because as in some cases li tag does not contains commenter name

Using xpath

 all_comments=driver.find_elements_by_xpath("//ul/li")
 total_comment_count = len(total_comment_count)
 for x in range(1,total_comment_visible):
    if driver.find_element_by_xpath("//ul//li["+x+"]/div/div/div/a"):
       print driver.find_element_by_xpath("//ul//li["+x+"]/div/div/div/a").get_attribute("title")

Using cssSelector

c_name=driver.find_elements_by_css_selector("a.FPmhX.notranslate.TlrDj")

#print name
for x in range(0,len(c_name)):
    print("name:")
    print(c_name[x])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM