【问题标题】:selenium instagram bot - getting username of post authorselenium instagram bot - 获取帖子作者的用户名
【发布时间】:2020-08-11 19:21:13
【问题描述】:

我最近一直在研究我的 instagram 机器人,但我唯一的问题是找到 xpath。 在成功完成喜欢和 cmets 之后,我也开始关注。 我想跟踪我的机器人在 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")

【问题讨论】:

  • 你能分享一个关于你找不到的元素的 HTML 代码 sn-p 吗?

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


【解决方案1】:

如果您有类似的帖子链接: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,在网站上右击,点击Inspect,找到你想要的元素的xpath。右键单击元素菜单中的元素。单击复制并选择复制xpath,如:

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2019-07-19
        • 2022-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多