python存储字典到json文件,将Python字典转储到JSON文件

怼烎@ 2022-08-30 06:24 311阅读 0赞

![Image 1][]

I want to dump data that i scrapped into a json file. I believe it already is in a good format (dictionary, list, string, etc.) How can I output into a json file?

#!/usr/bin/python

#weather.scraper

from bs4 import BeautifulSoup

import urllib

import json

def main():

“””weather scraper”””

r = urllib.urlopen(“https://www.wunderground.com/history/airport/KPHL/2016/1/1/MonthlyHistory.html?&reqdb.zip=&reqdb.magic=&reqdb.wmo=&MR=1").read()

soup = BeautifulSoup(r, “html.parser”)

tables = soup.find_all(“table”, class_=”responsive airport-history-summary-table”)

scrapedData = {}

for table in tables:

print ‘Weather Philadelphia’

for tr in table.find_all(“tr”):

firstTd = tr.find(“td”)

if firstTd and firstTd.has_attr(“class”) and “indent” in firstTd[‘class’]:

values = {}

tds = tr.find_all(“td”)

maxVal = tds[1].find(“span”, class_=”wx-value”)

avgVal = tds[2].find(“span”, class_=”wx-value”)

minVal = tds[3].find(“span”, class_=”wx-value”)

if maxVal:

values[‘max’] = maxVal.text

if avgVal:

values[‘avg’] = avgVal.text

if minVal:

values[‘min’] = minVal.text

if len(tds) > 4:

sumVal = tds[4].find(“span”, class_=”wx-value”)

if sumVal:

values[‘sum’] = sumVal.text

scrapedData[firstTd.text] = values

print scrapedData

if __name__ == “__main__“:

main()

解决方案

You need to use the following:

with open(‘output.json’, ‘w’) as jsonFile:

json.dump(scrapedData, jsonFile)

Where is going to write the dictionary to the output.json file in the working directory.

You can provide full path like open(‘C:\Users\user\Desktop\output.json’, ‘w’) instead of open(‘output.json’, ‘w’) for example to output the file to the user’s Desktop.

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,311人围观)

还没有评论,来说两句吧...

相关阅读

    相关 python如何读写字典文件

    ython一般保存文本文件只能保存str字符,那么我们如何来读写字典呢?两种方法: 一种是曲线救国,即把字典里的元素一个个的扣出来写到文件里 第二种通过json包来做...