subzeroid / instagrapi

🔥 The fastest and powerful Python library for Instagram Private API 2024
https://hikerapi.com/p/bkXQlaVe
MIT License
4.37k stars 684 forks source link

[BUG] JSONDecodeError in public_request when accessing followers #1826

Open cart12345 opened 8 months ago

cart12345 commented 8 months ago

Try Instagrapi SaaS with a free trial https://hikerapi.com/p/5GBWznd3

Describe the bug

When i try to get the followers i am getting

2024-02-28 22:04:32,410:INFO - https://i.instagram.com/api/v1/users/64086997147/info/
2024-02-28 22:04:32,678:INFO - None [400] GET https://i.instagram.com/api/v1/users/64086997147/info/ (269.0.0.18.75, OnePlus 6T Dev)
2024-02-28 22:04:33,682:INFO - https://i.instagram.com/api/v1/challenge/
2024-02-28 22:04:34,205:INFO - None [200] GET https://i.instagram.com/api/v1/challenge/ (269.0.0.18.75, OnePlus 6T Dev)
2024-02-28 22:04:35,212:INFO - https://i.instagram.com/api/v1/challenge/
2024-02-28 22:04:36,310:INFO - None [200] POST https://i.instagram.com/api/v1/challenge/ (269.0.0.18.75, OnePlus 6T Dev)
2024-02-28 22:04:37,315:INFO - https://i.instagram.com/api/v1/users/64086997147/info/
2024-02-28 22:04:38,377:INFO - None [200] GET https://i.instagram.com/api/v1/users/64086997147/info/ (269.0.0.18.75, OnePlus 6T Dev)
2024-02-28 22:04:39,706:INFO - [] [200] GET https://www.instagram.com/graphql/query/?variables=%7B%22user_id%22%3A%2265137788661%22%2C%22include_reel%22%3Atrue%7D&query_hash=ad99dd9d3646cc3c0dda65debcd266a7
2024-02-28 22:04:42,015:INFO - [] [201] GET https://www.instagram.com/mrbeastfanpage634/?__a=1&__d=dis
2024-02-28 22:04:42,016:ERROR - Status 201: JSONDecodeError in public_request (url=https://www.instagram.com/mrbeastfanpage634/?__a=1&__d=dis) >>> 
2024-02-28 22:04:45,211:INFO - [] [201] GET https://www.instagram.com/mrbeastfanpage634/?__a=1&__d=dis
2024-02-28 22:04:45,212:ERROR - Status 201: JSONDecodeError in public_request (url=https://www.instagram.com/mrbeastfanpage634/?__a=1&__d=dis) >>> 
2024-02-28 22:04:48,481:INFO - [] [201] GET https://www.instagram.com/mrbeastfanpage634/?__a=1&__d=dis
2024-02-28 22:04:48,482:ERROR - Status 201: JSONDecodeError in public_request (url=https://www.instagram.com/mrbeastfanpage634/?__a=1&__d=dis) >>> 
2024-02-28 22:04:49,485:INFO - https://i.instagram.com/api/v1/users/65137788661/info/
2024-02-28 22:04:50,381:INFO - None [200] GET https://i.instagram.com/api/v1/users/65137788661/
        cl.load_settings(f"{file_name}")
        user_info = cl.user_info_by_username(target)
        print(user_info)
        if user_info is None:
            raise HTTPException(status_code=404, detail="User not found")

        user_id = user_info.pk
        followers = cl.user_followers(user_id=user_id, amount=amount)
        scraped_usernames = [cl.user_info(
            follower_id).username for follower_id in followers if cl.user_info(follower_id)]
        print(scraped_usernames)

Desktop (please complete the following information):**

yo-aiv1 commented 8 months ago

it is not a bug, just changing in the api, instead of accessing the keys in the return dict using dict.<key>, you should use the method .get also you should use the method user_info_by_username_v1 not user_info_by_username and add .model_dump() so your code should look like this

        cl.load_settings(f"{file_name}")
        user_info = cl.user_info_by_username_v1(target).model_dump()
        print(user_info)
        if user_info is None:
            raise HTTPException(status_code=404, detail="User not found")

        user_id = user_info.get("pk")
        followers = cl.user_followers(user_id=user_id, amount=amount)
        scraped_usernames = [cl.user_info(
            follower_id).username for follower_id in followers if cl.user_info(follower_id)]
        print(scraped_usernames)
6CRIPT commented 1 month ago

Hi, tried your solution as follows:

`user_info = cl.user_info_by_username_v1("userxd").model_dump() if user_info is None: raise Exception("Usuario no encontrado")

user_id = user_info.get("pk")

inicio = time() seguidores = cl.user_followers(user_id=user_id) usernames_seguidores = [cl.user_info(follower_id).username for follower_id in seguidores.keys()] serie_seguidores = pd.Series(usernames_seguidores, name='Seguidores')

seguidos = cl.user_following(user_id=user_id) usernames_seguidos = [cl.user_info(following_id).username for following_id in seguidos.keys()] serie_seguidos = pd.Series(usernames_seguidos, name='Seguidos') ` and got the same issue again: "Status 201: JSONDecodeError in public_request (url=https://www.instagram.com/" and so on

Any ideas?

liwei-gif commented 2 weeks ago

x2 same error

my code:

import json
from instagrapi import Client

# Name of the session file
file_name = "session.json"
# Target username (the user from whom you want to get followers)
target = "bbtypebeats"  # Change this to the desired username
amount = 0  # Change this if you want a specific number of followers

# Initialize the client
cl = Client()

# Load the saved session
cl.load_settings(file_name)

# Get user information by username
user_info = cl.user_info_by_username(target).model_dump()
print(user_info)  # Print user information

# Check if the user was found
if user_info is None:
    raise Exception("User not found")

# Get the user_id of the target user
user_id = user_info.get("pk")

# Get the list of followers
followers = cl.user_followers(user_id=user_id, amount=amount)

# Save the usernames of the followers
scraped_usernames = [cl.user_info(follower_id).username for follower_id in followers if cl.user_info(follower_id)]

# Print the usernames of the followers
print(scraped_usernames)

# Save the output to a text file
with open("followers_output.txt", "w") as f:
    if not scraped_usernames:
        f.write("No followers found.\n")
    else:
        for username in scraped_usernames:
            f.write(f"Username: {username}\n")

print("The usernames of the followers have been saved in followers_output.txt.")

My console:

Status 201: JSONDecodeError in public_request (url=https://www.instagram.com/bbtypebeats/?__a=1&__d=dis) >>> 
Status 201: JSONDecodeError in public_request (url=https://www.instagram.com/bbtypebeats/?__a=1&__d=dis) >>> 
Status 201: JSONDecodeError in public_request (url=https://www.instagram.com/bbtypebeats/?__a=1&__d=dis) >>> 

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/public.py", line 187, in _send_public_request
    self.last_public_json = response.json()
  File "/usr/lib/python3/dist-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/user.py", line 191, in user_info_by_username
    user = self.user_info_by_username_gql(username)
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/user.py", line 144, in user_info_by_username_gql
    return extract_user_gql(self.public_a1_request(f"/{username!s}/")["user"])
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/public.py", line 231, in public_a1_request
    response = self.public_request(
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/public.py", line 122, in public_request
    raise e
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/public.py", line 99, in public_request
    return self._send_public_request(url, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/public.py", line 201, in _send_public_request
    raise ClientJSONDecodeError(
instagrapi.exceptions.ClientJSONDecodeError: JSONDecodeError Expecting value: line 1 column 1 (char 0) while opening https://www.instagram.com/bbtypebeats/

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 851, in urlopen
    return self.urlopen(
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 851, in urlopen
    return self.urlopen(
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 851, in urlopen
    return self.urlopen(
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 841, in urlopen
    retries = retries.increment(method, url, response=response, _pool=self)
  File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 574, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='i.instagram.com', port=443): Max retries exceeded with url: /api/v1/users/bbtypebeats/usernameinfo/ (Caused by ResponseError('too many 429 error responses'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/elevate/Escritorio/instagram/v1/cf.py", line 17, in <module>
    user_info = cl.user_info_by_username(target).model_dump()
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/user.py", line 199, in user_info_by_username
    user = self.user_info_by_username_v1(username)
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/user.py", line 162, in user_info_by_username_v1
    result = self.private_request(f"users/{username}/usernameinfo/")
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/private.py", line 541, in private_request
    raise e
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/private.py", line 526, in private_request
    self._send_private_request(endpoint, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/instagrapi/mixins/private.py", line 345, in _send_private_request
    response = self.private.get(
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 557, in get
    return self.request('GET', url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 544, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 657, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 507, in send
    raise RetryError(e, request=request)
requests.exceptions.RetryError: HTTPSConnectionPool(host='i.instagram.com', port=443): Max retries exceeded with url: /api/v1/users/bbtypebeats/usernameinfo/ (Caused by ResponseError('too many 429 error responses'))