gpt4 book ai didi

python-3.x - 像带有 Selenium python 的 Instagram 照片

转载 作者:行者123 更新时间:2023-12-04 02:57:56 27 4
gpt4 key购买 nike

我正在尝试编写我自己的 bot python selenium bot,以便在 Instagram 上进行一些实验。我成功登录并在搜索栏中搜索了一个主题标签,这将我带到了以下网页:
web page

但我不知道如何喜欢提要中的照片,我尝试使用 xpath 和以下路径进行搜索:
"//[@id="reactroot"]/section/main/article/div 1/div/div/div 1/div 1/a/div"
但它没有用,有人有想法吗?

最佳答案

首先,在您的情况下,建议使用 Python 的官方 Instagram Api ( documentation here on github )。

它会让你的机器人更简单、更易读,最重要的是更轻、更快。所以这是我的第一个建议。

如果您确实需要使用 Selenium,我还建议您下载适用于 Chrome 的 Selenium IDE 插件 here因为它可以节省你很多时间,相信我。你可以找到一个不错的教程on Youtube .

现在让我们谈谈可能的解决方案及其实现。经过一些研究,我发现帖子下方心脏图标的 xpath 表现如下:
第一个帖子的图标的xpath是:

xpath=//button/span

第二个帖子图标的xpath是:
xpath=//article[2]/div[2]/section/span/button/span

第三个帖子图标的xpath是:
xpath=//article[3]/div[2]/section/span/button/span

等等。 “文章”附近的第一个数字对应于帖子的编号。

enter image description here

因此,您可以设法获得您想要的帖子编号,然后单击它:

def get_heart_icon_xpath(post_num):
"""
Return heart icon xpath corresponding to n-post.
"""
if post_num == 1:
return 'xpath=//button/span'
else:
return f'xpath=//article[{post_num}]/div[2]/section/span/button/span'

try:
# Get xpath of heart icon of the 19th post.
my_xpath = get_heart_icon_xpath(19)
heart_icon = driver.find_element_by_xpath(my_xpath)
heart_icon.click()
print("Task executed successfully")
except Exception:
print("An error occurred")

希望能帮助到你。如果发现其他问题,请告诉我。

关于python-3.x - 像带有 Selenium python 的 Instagram 照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52012145/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com