简体   繁体   中英

selenium instagram bot - getting username of post author

I've been working lately on my instagram bot but my only problem is finding the xpaths. after successfully being able to do the likes and comments I start working on following aswell. I wanted to track the people that my bot followed inside csv file. i successfully did it. i was able to make the file using the code. trying to grab the username how ever became a problem because i can't seem to get the xpath of the username to grab the text. i searched online for the xpath and couldn't find anything about it? should i search the element by class instead?

        usr = driver.find_element_by_xpath('Can't find this xpath').text
        try:
            if usr not in prev_user_list:
                if driver.find_element_by_xpath("Can't find this xpath").text == "follow":
                    print("following " + usr)
                    driver.find_element_by_xpath("Can't find this xpath").click()
                    time.sleep(10)
                    print("Followed! adding to the list...")
                    new_followed.append(usr)
                    print(usr + " has been added to the list")

If you have an post link like: https://www.instagram.com/p/B_YIs5oBEly/ You can get post author with javascript. As a example:

driver.get(<your_post_link>)

postAuthor = driver.execute_script("return $(".sqdOP").innerHTML")
print("Post Author:",postAuthor)

If you want Xpath of an element, right click on website, click Inspect, find which xpath of element do you want. Right click on your element from elements menu. click copy and select copy xpath like:

查找元素的 Xpath

To get the name from a post (" https://www.instagram.com/p/XXXXXX/ ") use:

//div [@class="e1e1d"]/a/text()

To get the name from a main page (" https://www.instagram.com/XXXXXX/ ") use:

//a[@rel="nofollow"]/preceding-sibling::h2/text()

To get the name from a post ("https://www.instagram.com/p/XXXXXX/") use:

name = self.driver.find_element_by_xpath("//div[@class='e1e1d']").text

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