selenium instagram机器人-获取帖子作者的用户名

问题描述 投票:1回答:2

我最近一直在使用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")
python selenium selenium-webdriver xpath selenium-firefoxdriver
2个回答
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


0
投票

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

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

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

//a[@rel="nofollow"]/preceding-sibling::h2/text()
© www.soinside.com 2019 - 2024. All rights reserved.