像带有硒 python 的 instagram 照片
Posted
技术标签:
【中文标题】像带有硒 python 的 instagram 照片【英文标题】:Like instagram photo with selenium python 【发布时间】:2018-08-24 21:52:12 【问题描述】:我正在尝试编写我自己的 bot python selenium bot 以供 instagram 进行一些实验。我成功登录并在搜索栏中搜索了一个主题标签,这导致我进入以下网页:
但我不知道如何喜欢提要中的照片,我尝试使用 xpath 和以下路径进行搜索: "//[@id="reactroot"]/section/main/article/div1/div/div/div1/div1/a/div" 但它不起作用,有人有想法吗?
【问题讨论】:
【参考方案1】:首先,在您的情况下,建议使用 Python 的官方 Instagram Api (documentation here on github)。
这将使您的机器人更简单、更具可读性,最重要的是更轻、更快。所以这是我的第一个建议。
如果您确实需要使用 Selenium,我还建议您下载适用于 Chrome 的 Selenium IDE 插件here,因为它可以为您节省很多时间,相信我。你可以找到一个很好的教程on Youtube。
现在让我们谈谈可能的解决方案及其实施。经过一些研究,我发现帖子左下方心脏图标的 xpath 的行为如下: 第一篇的icon的xpath是:
xpath=//button/span
第二个帖子的图标的xpath是:
xpath=//article[2]/div[2]/section/span/button/span
第三个帖子的图标的xpath是:
xpath=//article[3]/div[2]/section/span/button/span
等等。 “文章”附近的第一个数字对应帖子的编号。
所以你可以设法得到你想要的帖子的编号,然后点击它:
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")
希望对您有所帮助。如果发现其他问题,请告诉我。
【讨论】:
首先感谢您的回答,我不使用 instagram api 因为我想在 selenium 中提高自己,真正的目标不是机器人,而是一些关于 selenium 的知识!我将在链接中观看教程并尝试您的代码,当我有更多答案时我会回复您:) 使用 find_element 获取异常,我猜它来自 php:selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression xpath=//article[3] /div[2]/section/span/button/span 因为以下错误:TypeError: Failed to execute 'evaluate' on 'Document': 结果不是节点集,因此无法转换为所需的类型。 (会话信息:chrome=68.0.3440.106)(驱动程序信息:chromedriver=2.41.578706 (5f725d1b4f0a4acbf5259df887244095596231db),platform=Mac OS X 10.13.6 x86_64)你能帮忙吗? 嗯,xpath 可能会因浏览器而异。要从心形图标右键单击提取xpath,单击inspect或按Crtl+Shift+I。现在你应该看到页面的 html 代码,像这样:<span class="glyphsSpriteHeart__outline__24__grey_9 u-__7" aria-label="Like"></span>
。右键单击它并单击 复制 > 复制 XPath。
有更新版本的instagram api吗?【参考方案2】:
我正在尝试做同样的事情) 这是一个工作方法。首先,我们找到一个帖子类(v1Nh3),然后我们捕获链接属性(href)。
posts = bot.find_elements_by_class_name('v1Nh3')
links = [elem.find_element_by_css_selector('a').get_attribute('href') for elem in posts]
【讨论】:
【参考方案3】:我实现了一个喜欢 Instagram 页面上所有图片的功能。它可以用在“探索”页面上,也可以简单地用在用户的个人资料页面上。
我是这样做的。
首先,为了从 Instagram 的主页导航到配置文件页面,我为“SearchBox”创建了一个 xPath,并为与索引对应的下拉菜单结果中的元素创建了一个 xPath。
def search(self, keyword, index):
""" Method that searches for a username and navigates to nth profile in the results where n corresponds to the index"""
search_input = "//input[@placeholder=\"Search\"]"
navigate_to = "(//div[@class=\"fuqBx\"]//descendant::a)[" + str(index) + "]"
try:
self.driver.find_element_by_xpath(search_input).send_keys(keyword)
self.driver.find_element_by_xpath(navigate_to).click()
print("Successfully searched for: " + keyword)
except NoSuchElementException:
print("Search failed")
然后我打开第一张图片:
def open_first_picture(self):
""" Method that opens the first picture on an Instagram profile page """
try:
self.driver.find_element_by_xpath("(//div[@class=\"eLAPa\"]//parent::a)[1]").click()
except NoSuchElementException:
print("Profile has no picture")
喜欢他们每个人:
def like_all_pictures(self):
""" Method that likes every picture on an Instagram page."""
# Open the first picture
self.open_first_picture()
# Create has_picture variable to keep track
has_picture = True
while has_picture:
self.like()
# Updating value of has_picture
has_picture = self.has_next_picture()
# Closing the picture pop up after having liked the last picture
try:
self.driver.find_element_by_xpath("//button[@class=\"ckWGn\"]").click()
print("Liked all pictures of " + self.driver.current_url)
except:
# If driver fails to find the close button, it will navigate back to the main page
print("Couldn't close the picture, navigating back to Instagram's main page.")
self.driver.get("https://www.instagram.com/")
def like(self):
"""Method that finds all the like buttons and clicks on each one of them, if they are not already clicked (liked)."""
unliked = self.driver.find_elements_by_xpath("//span[@class=\"glyphsSpriteHeart__outline__24__grey_9 u-__7\" and @aria-label=\"Like\"]//parent::button")
liked = self.driver.find_elements_by_xpath("//span[@class=\"glyphsSpriteHeart__filled__24__red_5 u-__7\" and @aria-label=\"Unlike\"]")
# If there are like buttons
if liked:
print("Picture has already been liked")
elif unliked:
try:
for button in unliked:
button.click()
except StaleElementReferenceException: # We face this stale element reference exception when the element we are interacting is destroyed and then recreated again. When this happens the reference of the element in the DOM becomes stale. Hence we are not able to get the reference to the element.
print("Failed to like picture: Element is no longer attached to the DOM")
此方法检查图片是否有指向下一张图片的“下一步”按钮:
def has_next_picture(self):
""" Helper method that finds if pictures has button \"Next\" to navigate to the next picture. If it does, it will navigate to the next picture."""
next_button = "//a[text()=\"Next\"]"
try:
self.driver.find_element_by_xpath(next_button).click()
return True
except NoSuchElementException:
print("User has no more pictures")
return False
如果您想了解更多信息,请随时查看我的 Github 存储库:https://github.com/mlej8/InstagramBot
【讨论】:
以上是关于像带有硒 python 的 instagram 照片的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法阻止硒被重定向到登录?不使用 api 抓取 Instagram