提取Instagram关注者使用JavaScript?

发布于 2025-02-06 21:43:34 字数 4122 浏览 2 评论 0原文

在Instagram配置文件末尾,它曾经可以使用?__ a = 1以JSON格式获取配置文件数据,并轻松从那里提取信息。当我现在尝试时,我得到的只是:

for(;;); {“ redirect”:“ https:\/\/www.instagram.com \/pubgm_silvanus \/code>

如果我使用以下内容:

https://www.instagram.com/pubgm_silvanus?___A=1

您是否获得了相同的功能?是否还有其他简单的方法可以使用JavaScript提取追随者号码(在这种情况下 11 )?

这是我使用的代码:


# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import time

import requests
from requests.exceptions import HTTPError

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import pandas as pd

path = 'C:\Crawling\pythonProject1\driver\chromedriver.exe'
options = Options()
# options.add_argument('--headless')
options.add_argument('--lang=en')
# options.add_argument('--disable-gpu')  # Last I checked this was necessary.

driver = webdriver.Chrome(executable_path=path, options=options)#, options=options)
df1 = pd.DataFrame(columns=['Account', 'Followers'])


def login(username, password):
    driver.get("http://www.instagram.com")

    # bypass cookie consent
    accept = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "/html/body/div[4]/div/div/button[1]"))).click()

    # target username
    uname = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']")))
    pwd = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']")))

    # enter username and password
    uname.clear()
    uname.send_keys(username)
    pwd.clear()
    pwd.send_keys(password)


    time.sleep(5)
    # target the login button and click it
    button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()

    time.sleep(5)
    alert = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not now")]'))).click()
    alert2 = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not '
                                                                                   'Now")]'))).click()

    # We are logged in!

    return 0


def get_accounts_list():
    accounts_list = []
    with open("./accounts.txt") as file:
        lines = file.readlines()

    for line in lines:
        account = line.strip() + "?__a=1"
        accounts_list.append(account)
    return accounts_list


def create_data_frame(accounts_list):
    for account in accounts_list:
        try:
            driver.get(account)
            page_source = driver.page_source
            number_of_followers = page_source.split('"edge_followed_by":{"count":')[1].split('}')[0]
            df1.loc[len(df1)] = [account.split("?__a=1")[0], number_of_followers]
            df1.to_csv("accounts.csv", encoding='utf-8', index=False, header=False)
            print(account.split("?__a=1")[0], number_of_followers)
        except Exception as err:
            print(account)
            print("Error, I had to skip this account")
            continue
    return 1


def launcher(message):
    print(message)
    # Credentials.
    username = ""  # edit this
    password = ""  # and this
    try:
        login(username, password)
    except BaseException as login_err:
        print("Error occurred while trying to log in:", login_err)
    
    accounts_list = get_accounts_list()
    if create_data_frame(accounts_list):
        print("Done.")
        df1.to_csv("accounts.csv", encoding='utf-8', index=False, header=False)
        driver.close()


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # specify the path to chromedriver.exe (download and save on your computer)
    # open the webpage
    launcher('Booting up....')```

It used to be possible to use ?__a=1 at the end of an Instagram profile URL to get the profile data in JSON format and easily extract information from there. When I try now, all I get is this:

for (;;);{"redirect":"https:\/\/www.instagram.com\/pubgm_silvanus\/"}

If I use this:

https://www.instagram.com/pubgm_silvanus?__a=1

Are you getting the same? Is there any other easy way to extract the follower number (in this case 11) with JavaScript?

Here is the code I am using:


# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import time

import requests
from requests.exceptions import HTTPError

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import pandas as pd

path = 'C:\Crawling\pythonProject1\driver\chromedriver.exe'
options = Options()
# options.add_argument('--headless')
options.add_argument('--lang=en')
# options.add_argument('--disable-gpu')  # Last I checked this was necessary.

driver = webdriver.Chrome(executable_path=path, options=options)#, options=options)
df1 = pd.DataFrame(columns=['Account', 'Followers'])


def login(username, password):
    driver.get("http://www.instagram.com")

    # bypass cookie consent
    accept = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "/html/body/div[4]/div/div/button[1]"))).click()

    # target username
    uname = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']")))
    pwd = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']")))

    # enter username and password
    uname.clear()
    uname.send_keys(username)
    pwd.clear()
    pwd.send_keys(password)


    time.sleep(5)
    # target the login button and click it
    button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()

    time.sleep(5)
    alert = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not now")]'))).click()
    alert2 = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '//button[contains(text(), "Not '
                                                                                   'Now")]'))).click()

    # We are logged in!

    return 0


def get_accounts_list():
    accounts_list = []
    with open("./accounts.txt") as file:
        lines = file.readlines()

    for line in lines:
        account = line.strip() + "?__a=1"
        accounts_list.append(account)
    return accounts_list


def create_data_frame(accounts_list):
    for account in accounts_list:
        try:
            driver.get(account)
            page_source = driver.page_source
            number_of_followers = page_source.split('"edge_followed_by":{"count":')[1].split('}')[0]
            df1.loc[len(df1)] = [account.split("?__a=1")[0], number_of_followers]
            df1.to_csv("accounts.csv", encoding='utf-8', index=False, header=False)
            print(account.split("?__a=1")[0], number_of_followers)
        except Exception as err:
            print(account)
            print("Error, I had to skip this account")
            continue
    return 1


def launcher(message):
    print(message)
    # Credentials.
    username = ""  # edit this
    password = ""  # and this
    try:
        login(username, password)
    except BaseException as login_err:
        print("Error occurred while trying to log in:", login_err)
    
    accounts_list = get_accounts_list()
    if create_data_frame(accounts_list):
        print("Done.")
        df1.to_csv("accounts.csv", encoding='utf-8', index=False, header=False)
        driver.close()


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # specify the path to chromedriver.exe (download and save on your computer)
    # open the webpage
    launcher('Booting up....')```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2025-02-13 21:43:34

您提供的此链接确实以JSON格式显示所有配置文件数据。

要获取追随者号码,请使用此路径
graphql.user.edge_followed_by.count

This link you provided does show all the profile data in JSON format.

To get follower number, use this path
graphql.user.edge_followed_by.count

Path

Using fetch API you can fetch the JSON and put it in array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文