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

时间:2020-04-28 04:03:05

标签: python selenium selenium-webdriver xpath selenium-firefoxdriver

我最近一直在使用instagram机器人,但是我唯一的问题是找到xpath。 成功完成喜欢和评论后,我也开始关注以下内容。 我想跟踪我的机器人在csv文件中关注的人。我成功做到了!我能够使用代码制作文件。试图抓住用户名怎么成为一个问题,因为我似乎无法获得用户名的xpath来抓住文本。我在网上搜索了xpath,却找不到任何相关信息。 我应该按类搜索元素吗?

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

3 个答案:

答案 0 :(得分:0)

如果您有一个帖子链接,例如:https://www.instagram.com/p/B_YIs5oBEly/ 您可以使用javascript获取帖子作者。例如:

driver.get(<your_post_link>)

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

如果要元素的Xpath,请在网站上单击鼠标右键,单击“检查”,找到所需的元素XPath。在“元素”菜单上右键单击您的元素。单击复制并选择复制xpath,如:

Finding Xpath of an element

答案 1 :(得分:0)

要从帖子(“ https://www.instagram.com/p/XXXXXX/”中获取姓名,请使用:

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

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

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

答案 2 :(得分:0)

要从帖子(“ https://www.instagram.com/p/XXXXXX/”)中获取名称,请使用:

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