【问题标题】:I can't do json()['graphql']['user'] for instagram API我不能为 instagram API 做 json()['graphql']['user']
【发布时间】:2022-01-10 09:43:38
【问题描述】:

我正在尝试制作一个工具,可以从 Instagram 个人资料页面中获取 JSON 格式的所有信息。

例如:https://www.instagram.com/dave_saa/?__a=1

每当我尝试这样做时,都会出现错误。错误是:simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0).

更详细的错误

Traceback (most recent call last):
  File "C:\Users\disco\PycharmProjects\IgOSINT\main.py", line 9, in <module>
    json_found_for_site = request_for_site.json()
  File "C:\Users\disco\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 910, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\disco\AppData\Local\Programs\Python\Python39\lib\site-packages\simplejson\__init__.py", line 525, in loads
    return _default_decoder.decode(s)
  File "C:\Users\disco\AppData\Local\Programs\Python\Python39\lib\site-packages\simplejson\decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "C:\Users\disco\AppData\Local\Programs\Python\Python39\lib\site-packages\simplejson\decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我的 Python 代码

import requests
from termcolor import colored
import json
import simplejson

target = str(input(colored('[+] Enter Target Username: ', 'blue')))

request_for_site = requests.get('https://www.instagram.com/' + target + '/?__a=1')
json_found_for_site = request_for_site.json()['graphql']['user']

if (request_for_site.status_code == 200):
    print(colored('[+++] TARGET FOUND !', 'green'))
    print(colored(
        '''
        [1] USERNAME
        [2] FULL NAME
        [3] BIO
        [4] HIGHLIGHTS
        [5] PHONE NUMBER
        [6] IS ACCOUNT PRIVATE OR PUBLIC [recommended FIRST]
        [7] Profile Picture
        [8] Followers
        [9] Followed
        [10] ID
        [11] IS VERIFIED
        ''', 'red'
    ))
    tool_option = str(input(colored('[+] ENTER NUMBER OPTION TO FIND: ', 'blue')))
    if (tool_option == '1'):
        print(json_found_for_site['username'])
    elif (tool_option == '2'):
        print(json_found_for_site['full_name'])



该程序仍在开发中,尚未完成。但我得到了错误,所以我暂时停止并在这里寻求帮助。

请有人帮忙。

【问题讨论】:

  • 解码前检查响应码!
  • 响应码是
  • 200 仅表示 HTTP 服务器理解您使用的 URL,但并不意味着它发送 JSON 数据。或者它可能会发送带有不同于您期望的数据的 JSON - 即。它可以发送错误消息。您应该首先显示request_for_site.json() 或更好的request_for_site.text,看看您真正得到了什么。错误可能表明它没有得到 JSON 而是空字符串。
  • 顺便说一句:某些服务器可能会检查您在请求中使用的标头 - 尤其是 User-Agent - 但 requests 发送一些想法,例如 python/3.8 而不是 Mozilla/5.0... 所以服务器可能会检测到它是脚本/ bot 并发送不同的值,然后你期望 - 即。它可能会发送带有验证码的 HTML 以确认您是人类。
  • 当我运行您的代码并检查 print( request_for_site.text ) 时,我会看到标题为 LOGIN 的 HTML。如果我在private mode 中的浏览器中测试您的链接,那么它也会重定向到带有登录表单的页面。所以你的代码必须先登录到服务器。

标签: python json python-3.x api python-requests


【解决方案1】:

您正在解析的文档是 HTML,而不是 JSON。您无法使用 JSON 解析器解析 HTML,要解析 HTML,您需要 HTML 解析器。

【讨论】:

  • 问题不同。如果您未登录,那么您将获得带有登录表单的 HTML,但如果您已登录,则会获得预期的 JSON 数据。
  • 如何解析 HTML?转换成 JSON
  • @RDMotivationKrunklier:我不明白你的意思。 “将 HTML 解析为 JSON”是什么意思?
  • 我如何使用 python 获取请求登录呢?然后获取 JSON。你能显示代码吗?谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
  • 2018-10-24
  • 2020-08-05
  • 1970-01-01
相关资源
最近更新 更多