0

Everything in the first piece of code works flawlessly, but the second one does nothing, even though the code is being registered. What I eventually want to happen is for the code to find the links of the first 6 Instagram posts, and store their metrics (for now just the views). But I want to do this across different accounts. Meaning different link directories. What this code is supposed to as of now is to get the "href" attribute of the "v1Nh3 kIKUG _bz0w" class and store it in a list. I've referenced the HTML for a better idea of what we're dealing with here. Help!!

from selenium import webdriver
from time import sleep
import random
import re

#bot detect evasion
waiting_time = [4,4,8.2,4,6,13,7.2,3.4,6.5,9]
number =  3 #random.choice(waiting_time)
bigger_number = 4000000

class seeder_gram():

    def __init__(self, username, phone, email, password):

        self.username = username
        self.phone = phone
        self.email = email
        self.password = password
        self.driver = webdriver.Chrome("/Users/apple/Downloads/chromedriver")
        self.driver.get('https://instagram.com')

        sleep(number)

        #username insert
        username_form = self.driver.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input')
        username_form.clear()
        username_form.send_keys("nothingtoseehere")

        #password insert
        password_form = self.driver.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input')
        password_form.clear()
        password_form.send_keys("nothingtoseehere")

        #clicks button
        button_click = self.driver.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button')
        button_click.click()

        sleep(number)

        #close noti bar
        try:                                                    
            noti_pop_up = self.driver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[3]/button[2]')
            noti_pop_up.click()
        except: 
            pass
        
        #list of targets
        account_list = ["nothingtoseehere", "nothingtoseehere", "nothingtoseehere", "nothingtoseehere", "nothingtoseehere", "nothingtoseehere"]
        account_picker = random.choice(account_list)

        #searches for target
        self.driver.get('https://instagram.com/' + account_picker + '/')
        
        sleep(number)

        #post list
        post_id_finder = self.driver.find_elements_by_class_name("v1Nh3 kIKUG  _bz0w")
        list_of_links = []
        
        def get_href(post_id_finder):
            for post in post_id_finder:
                link = post_id_finder.get_attribute('href')
                link.append(list_of_links)
                print(list_of_links)

        get_href(post_id_finder)


        """
        #gives unique post directory
        #eventually this will be a list that appends links depending on post
        post_id = "/p/CLWlksPHsvN/"

        #searches for the first grid
        grid_search = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/div[4]/article/div[1]/div/div[1]')


        #goes to post
        post_viewer = self.driver.get('https://www.instagram.com' + post_id)

        #checks view
        video_views = self.driver.find_element_by_xpath('/html/body/div[1]/section/main/div/div[1]/article/div[3]/section[2]/div/span')
        views = re.findall(r'\d', video_views.text)
        print(views)

        #combines view value


        
        post_index = 1

        while post_index != 6:


            #finds videos that show views
            
            #what if the video isnt based on views??

            #logs views in list 


            #goes back one page

            #moves on to next post
            post_index += 1
        """

        #back up serch feature in case first doesn't work, needs more work added though to make functional
        """
        search_up = self.driver.find_element_by_xpath('/html/body/div[1]/section/nav/div[2]/div/div/div[2]/input')
        search_up.clear()
        search_up.send_keys(account_picker)
        """

        sleep(bigger_number)

#registered users
user1 = seeder_gram("nothingtoseehere", "nothingtoseehere", "nothingtoseehere", "nothingtoseeheres")
user2 = seeder_gram("nothingtoseehere", "nothingtoseehere", "nothingtoseehere", "nothingtoseehere")


seeder_gram()

This is the code I'm having trouble with:

#post list
        post_id_finder = self.driver.find_elements_by_class_name("v1Nh3 kIKUG  _bz0w")
        list_of_links = []
        
        def get_href(post_id_finder):
            for post in post_id_finder:
                link = post_id_finder.get_attribute('href')
                link.append(list_of_links)
                print(list_of_links)

        get_href(post_id_finder)

Here is what the HTML looks like, if there's a better way to do this: enter image description here

Hussein
  • 97
  • 7

1 Answers1

0

You can't use multiple class names in find_element_by_class name, you're best off using something like find by css selector.

Simmo
  • 1
  • 2