【问题标题】:Python error: AttributeError: 'str' object has no attribute 'read'Python错误:AttributeError:'str'对象没有属性'read'
【发布时间】:2020-09-28 19:50:00
【问题描述】:

我的完整代码:

import requests as req
import json
Bin = int(300000)
BinMax = int(600000)
File = open("C:/Users/admin/Desktop/PS Now Generaetors/Bins.txt", 'a')

while bin != BinMax:
    json1 = req.get("https://lookup.binlist.net/" + str(Bin))
    json2 = json1.text
    jsonout = json.load(json2)
    country = jsonout["country"]
    cc = country["alpha2"]
    if cc == "US" or "AT" or "BE" or "CA" or "FR" or "De" or "IE" or "JP" or "LU" or "NL" or "CH" or "GB" or "ES" or "IT" or "PT" or "NO" or "DK" or "FI" or "SE" or "PH":
        print (bin, "writed")
        File.write("\n" + str(Bin) + ";" + cc)
    bin =+ 1

完全错误:

Traceback (most recent call last):
  File "C:\Users\admin\Desktop\PS Now Generaetors\Bin generator.py", line 10, in <module>
    jsonout = json.load(json2)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'

如何解决?

【问题讨论】:

  • 您的文件似乎包含一个字符串,而不是 JSON 对象。
  • 你的if声明is wrong
  • 如果您使用请求并且想要 json,您可以只执行 jsonout = json1.json() 而不是获取文本然后使用 json.loads()

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


【解决方案1】:

你必须使用

json.loads(json2)

而不是“加载”。

查看更多 https://www.w3schools.com/python/python_json.asp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    相关资源
    最近更新 更多