我正在尝试构建一个instagram机器人,该机器人可以跟踪个人资料,并对帖子发表喜欢和评论。下面是控制“喜欢”过程的代码的相关部分。它可以工作,但是如果打开的照片已经被喜欢,则机器人会再次点击“赞”,从而有效地使其不喜欢它。我该如何预防?
try:
button_like = webdriver.find_element_by_xpath('html/body/div[4]/div[2]/div/article/div[2]/section[1]/span[1]/button')
button_like.click()
likes += 1
sleep(randint(18,25))
except Exception:
print("An error occurred when trying to like images")
[XPATH]与like按钮相同,无论它是否已被喜欢。
您可以通过检查区域标签的属性值(从赞:]更改来检查图像是否已被喜欢)。
:<svg aria-label="Like" class="_8-yf5 " fill="#262626" height="24" viewBox="0 0 48 48" width="24">
致不喜欢
<svg aria-label="Unlike" class="_8-yf5 " fill="#ed4956" height="24" viewBox="0 0 48 48" width="24">
因此您可以在执行点击之前通过检查属性的值来检查图像是否已被喜欢。