python 我如何修复这个字符串的代码为json

66bbxpm5  于 2023-01-16  发布在  Python
关注(0)|答案(1)|浏览(132)
import json
import random
from src.instadm import InstaDM
from selenium.common.exceptions import NoSuchElementException

f = open('infos/accounts.json',)
accounts = json.load(f)

with open('infos/usernames.txt', 'r') as f:
    usernames = [line.strip() for line in f]

with open('infos/messages.txt', 'r') as f:
    messages = [line.strip() for line in f]

while True:
    if not usernames:
        print('Finished usernames.')
        break
    
    dm_num = int(input('How many DM you want to send in each account: '))

    for account in accounts:
        if not usernames:
            break
            # Auto login
        insta = InstaDM(username=account["username"], password=account["password"], headless=False)

        for i in range(dm_num):
            username = username.pop()
            # Send message
            try:
                success = insta.sendMessage(user=username, message=random.choice(messages))
                if not success:
                    insta.teardown()
                continue
            except NoSuchElementException:
                # User not found = move on to the next user
                continue
            except Exception as e:
                if "timed out. Element not found with XPATH : //textarea[@placeholder]" in str(e):
                    insta.teardown()
                continue
            # other error occured = move on to the next user 
        #Log out
        insta.teardown()

我尝试运行以下一个我收到这个错误.

Traceback (most recent call last):
  File "C:\Users\whata\OneDrive\Desktop\Instagram-Bot-Scrape-DM-Users-main\Instagram-Bot-Scrape-DM-Users-main\dm.py", line 7, in <module>
    accounts = json.load(f)
               ^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.496.0_x64__qbz5n2kfra8p0\Lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
           ^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.496.0_x64__qbz5n2kfra8p0\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.496.0_x64__qbz5n2kfra8p0\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.496.0_x64__qbz5n2kfra8p0\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 6 column 4 (char 90)
wvt8vs2t

wvt8vs2t1#

f = open('infos/accounts.json',) # <----- you don't need this ','
accounts = json.load(f)

如果问题仍存在:检查json文件中的字符串值是否位于“”而不是“”中。
另外,最好使用上下文管理器:

with open('infos/accounts.json', 'r') as f:
    accounts = json.load(f)

相关问题