selenium instagram bot - 获取 post 作者的用户名

selenium instagram bot - getting username of post author

我最近一直在开发我的 Instagram 机器人,但我唯一的问题是找到 xpath。 在成功完成点赞和评论后,我也开始关注。 我想跟踪我的机器人在 csv 文件中关注的人。我成功了!我能够使用代码制作文件。试图获取用户名如何成为一个问题,因为我似乎无法获取用户名的 xpath 来获取文本。我在网上搜索了 xpath,但找不到任何相关信息。 我应该用 class 搜索元素吗?

        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")

如果您有 post link 喜欢:https://www.instagram.com/p/B_YIs5oBEly/ 您可以通过 javascript 获得 post 作者。例如:

driver.get(<your_post_link>)

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

如果你想要一个元素的Xpath,右键点击网站,点击Inspect,找到你想要的元素的Xpath。从元素菜单中右键单击您的元素。单击复制并 select 复制 xpath,如:

要从 post ("https://www.instagram.com/p/XXXXXX/") 中获取名称,请使用:

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

要从主页 ("https://www.instagram.com/XXXXXX/") 获取名称,请使用 :

//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