【发布时间】: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