You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Python编写的Instagram点赞机器人添加点赞计数限制及已点赞检测?

Enhancements for Your Instagram Like Bot: Like Limit & Already-Liked Detection

Hey there! Let's upgrade your Instagram bot with the two features you want—a like counter with upper limit and skip logic for already-liked photos. Here's how to implement both step by step:

1. Add Like Limit & Counter

First, we'll set a maximum number of likes you want the bot to perform, then track its progress. Once it hits the limit, the bot will stop processing further photos to avoid overdoing it.

  • Define a MAX_LIKES variable (set it to any number you prefer, e.g., 50)
  • Initialize a current_likes counter at 0 to track completed likes
  • Increment the counter every time a photo is successfully liked
  • Add a check to exit the loop once current_likes reaches MAX_LIKES

2. Detect Already-Liked Photos

Instagram uses the aria-label attribute on the like button to show its state:

  • Unliked photos have a button with aria-label="Like"
  • Liked photos have a button with aria-label="Unlike"

We'll check this attribute first—if the button says "Unlike", we skip the photo entirely. If it says "Like", we proceed to click it.

Full Modified Code

Here's the updated code with both features integrated:

import time
import random

# Configure your maximum like limit here
MAX_LIKES = 50
current_likes = 0

unique_photos = len(pic_hrefs)
for pic_href in pic_hrefs:
    # Exit loop immediately if we've hit the like limit
    if current_likes >= MAX_LIKES:
        print(f"Reached maximum likes limit ({MAX_LIKES}). Stopping bot.")
        break

    driver.get(pic_href)
    time.sleep(random.randint(2, 5))  # Randomize load sleep to mimic human behavior

    try:
        # Wait for the like button to fully load
        time.sleep(random.randint(3, 10))
        
        # Locate the like button and check its state
        like_button = driver.find_element_by_class_name("_8-yf5 ")
        button_state = like_button.get_attribute("aria-label")

        if button_state == "Unlike":
            print(f"Photo already liked. Skipping...")
            unique_photos -= 1
            continue  # Move to the next photo
        elif button_state == "Like":
            # Perform the like action
            like_button.click()
            current_likes += 1
            print(f"Liked photo! Current count: {current_likes}/{MAX_LIKES}")
            
            # Random sleep after liking to avoid detection
            for second in reversed(range(0, random.randint(18, 28))):
                print_same_line(f"#{hashtag}: unique photos left: {unique_photos} | Sleeping {second}")
                time.sleep(2)
                
    except Exception as e:
        print(f"Error processing photo: {str(e)}")
        time.sleep(2)
    
    unique_photos -= 1

Quick Notes:

  • Randomized Intervals: I adjusted some sleep times to be more random—this helps avoid triggering Instagram's anti-bot systems, since human behavior isn't perfectly uniform.
  • Error Resilience: The try/except block catches issues like missing buttons (e.g., restricted photos) and moves on to the next item without crashing.
  • Flexible Limit: Tweak MAX_LIKES to any number based on how many likes you want the bot to handle in one session.

内容的提问来源于stack exchange,提问作者iflexi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 21:48:45