将Python字典转储到JSON文件
我想将废弃的数据转储到json文件中。我相信它已经是一个很好的格式(字典、列表、字符串等)。我如何才能输出到json文件中将Python字典转储到JSON文件,python,json,web-scraping,dump,Python,Json,Web Scraping,Dump,我想将废弃的数据转储到json文件中。我相信它已经是一个很好的格式(字典、列表、字符串等)。我如何才能输出到json文件中 #!/usr/bin/python #weather.scraper from bs4 import BeautifulSoup import urllib import json def main(): """weather scraper""" r = urllib.urlopen("https://www.wundergro
#!/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()
您需要使用以下各项:
with open('output.json', 'w') as jsonFile:
json.dump(scrapedData, jsonFile)
将字典写入工作目录中的output.json
文件的位置。您可以提供完整路径,例如
open('C:\Users\user\Desktop\output.json','w')
而不是open('output.json','w')
,以将文件输出到用户的桌面。非常感谢!您还知道如何指定保存位置吗?现在,它会自动保存到我的文档中。@malina我已经编辑了我的答案。如果这解决了你的问题,请告诉我。