我正在制作python Instagram用户名检查器。完成后,我偶然遇到了一个问题,该问题使我无法从JSON中提取元素。 JSON是用于检查用户名是否在Instagram上存在的
。我得到的错误是TypeError: byte indices must be integers
当我要检查时,JSON的输出将是什么。我使用下面的代码。
print(a["user"])
JSON本身看起来像这样(用户也可以为False)
{'authenticated': False, 'user': True, 'status': 'ok'}
^那就是我这样输出的时候:
打印(f)
如何解决此错误?
我尝试过一点切换代码并尝试以不同的方式拉json,但这会使情况变得更糟。
import string
import random
import requests
import json
import time
from bs4 import BeautifulSoup
import json, random, re, requests
a = 2
def checkuser(usr, a):
BASE_URL = 'https://www.instagram.com/accounts/login/'
LOGIN_URL = BASE_URL + 'ajax/'
headers_list = [
"Mozilla/5.0 (Windows NT 5.1; rv:41.0) Gecko/20100101"\
" Firefox/41.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2)"\
" AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2"\
" Safari/601.3.9",
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0)"\
" Gecko/20100101 Firefox/15.0.1",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"\
" (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36"\
" Edge/12.246"
]
USERNAME = usr
PASSWD = '*****'
USER_AGENT = headers_list[random.randrange(0,4)]
session = requests.Session()
session.headers = {'user-agent': USER_AGENT}
session.headers.update({'Referer': BASE_URL})
req = session.get(BASE_URL)
soup = BeautifulSoup(req.content, 'html.parser')
body = soup.find('body')
pattern = re.compile('window._sharedData')
script = body.find("script", text=pattern)
script = script.get_text().replace('window._sharedData = ', '')[:-1]
data = json.loads(script)
csrf = data['config'].get('csrf_token')
login_data = {'username': USERNAME, 'password': PASSWD}
session.headers.update({'X-CSRFToken': csrf})
login = session.post(LOGIN_URL, data=login_data, allow_redirects=True)
a = login.content
while True:
def generate(size, chars=string.ascii_uppercase+string.digits):
return ''.join(random.choice(chars) for _ in range(size))
username = generate(4)
url = f"https://www.instagram.com/{username}"
req = requests.get(url).text
if "Page Not Found" in req:
BASE_URL = 'https://www.instagram.com/accounts/login/'
LOGIN_URL = BASE_URL + 'ajax/'
headers_list = [
"Mozilla/5.0 (Windows NT 5.1; rv:41.0) Gecko/20100101"\
" Firefox/41.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2)"\
" AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2"\
" Safari/601.3.9",
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0)"\
" Gecko/20100101 Firefox/15.0.1",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"\
" (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36"\
" Edge/12.246"
]
USERNAME = username
PASSWD = '*****'
USER_AGENT = headers_list[random.randrange(0,4)]
session = requests.Session()
session.headers = {'user-agent': USER_AGENT}
session.headers.update({'Referer': BASE_URL})
req = session.get(BASE_URL)
soup = BeautifulSoup(req.content, 'html.parser')
body = soup.find('body')
pattern = re.compile('window._sharedData')
script = body.find("script", text=pattern)
script = script.get_text().replace('window._sharedData = ', '')[:-1]
data = json.loads(script)
csrf = data['config'].get('csrf_token')
login_data = {'username': USERNAME, 'password': PASSWD}
session.headers.update({'X-CSRFToken': csrf})
login = session.post(LOGIN_URL, data=login_data, allow_redirects=True)
a = login.content
f = json.loads(a.decode('utf-8'))
print(f)
print(a["user"])
我希望它显示True
或False
相反,我收到错误消息:
{'authenticated': False, 'user': True, 'status': 'ok'}