【问题标题】:Dumping data to outputfile with python json.dump(Array, outfile)使用 python json.dump(Array, outfile) 将数据转储到输出文件
【发布时间】:2023-04-04 22:08:02
【问题描述】:

我正在尝试使用以下代码将我从站点“抓取”的数据写入 json 输出文件:

from bs4 import BeautifulSoup
import requests
import json

path = ["https://www.test.be?page=,https://www.test2.be?page="]

adresArr = []
for i in path:
    pagina = 0;
    for x in range(0, 4):
        url = i + str(pagina)
        response = requests.get(url, timeout=5)
        content = BeautifulSoup(response.content, "html.parser")
        for adres in content.findAll('tr', attrs={"class": "odd clickable-row"}):
            adresObject = {
                "postcode": adres.find('td', attrs={"class": "views-field views-field-field-locatie-postal-code"}).text.encode('utf-8'),
                "naam": adres.find('td', attrs={"class": "views-field views-field-field-locatie-thoroughfare"}).text.encode('utf-8'),
                "plaats": adres.find('td', attrs={"class": "views-field views-field-field-locatie-locality"}).text.encode('utf-8')
            }
            adresArr.append(adresObject)


        for adres in content.findAll('tr', attrs={"class": "odd clickable-row active"}):
            adresObject = {
                "postcode": adres.find('td', attrs={"class": "views-field views-field-field-locatie-postal-code"}).text.encode('utf-8'),
                "naam": adres.find('td', attrs={"class": "views-field views-field-field-locatie-thoroughfare"}).text.encode('utf-8'),
                "plaats": adres.find('td', attrs={"class": "views-field views-field-field-locatie-locality"}).text.encode('utf-8')
            }
            adresArr.append(adresObject)

            pagina = x

    with open('adresData.json', 'w') as outfile:
         json.dump(adresArr, outfile)

我收到以下错误字节类型的对象不是 json 可序列化的

如果我打印数组本身,它看起来没问题。但我坚持将其写入 json 文件。
我做错了什么?

这是我第一次在 python 中编码(并且没有很多编码经验)所以请让你的答案清楚明白:)

提前致谢

【问题讨论】:

    标签:
    python
    json
    dump