【问题标题】:Beautifulsoup to extract within tags and output as a JSONBeautifulsoup 在标签内提取并输出为 JSON
【发布时间】:2016-11-28 10:40:29
【问题描述】:

如上一个问题所述,我正在使用 Beautiful soup with python 从网站检索天气数据。

网站的外观如下:

<channel>
<title>2 Hour Forecast</title>
<source>Meteorological Services Singapore</source>
<description>2 Hour Forecast</description>
<item>
<title>Nowcast Table</title>
<category>Singapore Weather Conditions</category>
<forecastIssue date="18-07-2016" time="03:30 PM"/>
<validTime>3.30 pm to 5.30 pm</validTime>
<weatherForecast>
<area forecast="TL" lat="1.37500000" lon="103.83900000" name="Ang Mo Kio"/>
<area forecast="SH" lat="1.32100000" lon="103.92400000" name="Bedok"/>
<area forecast="TL" lat="1.35077200" lon="103.83900000" name="Bishan"/>
<area forecast="CL" lat="1.30400000" lon="103.70100000" name="Boon Lay"/>
<area forecast="CL" lat="1.35300000" lon="103.75400000" name="Bukit Batok"/>
<area forecast="CL" lat="1.27700000" lon="103.81900000" name="Bukit Merah"/>` 
<channel>

我设法使用这些代码检索了我需要的信息:

import requests
from bs4 import BeautifulSoup
import urllib3
import json


weather = []

#getting the time

r = requests.get('http://www.nea.gov.sg/api/WebAPI/?dataset=2hr_nowcast&keyref=<keyrefno>')
soup = BeautifulSoup(r.content, "xml")
time = soup.find('validTime').string
print "validTime: " + time

for currentdate in soup.find_all('item'):
 element = currentdate.find('forecastIssue')
 print "date: " + element['date']

for currentdate in soup.find_all('item'):
 element = currentdate.find('forecastIssue')
 print "time: " + element['time'] 

for area in soup.find('weatherForecast').find_all('area'):
 print area


 #file writing
with open("c:/scripts/nea.json", 'w') as outfile:
json.dumps(weather, outfile)
#outfile.write(",")

这是我得到的输出(在 CMD 中):

C:\scripts>python neaweather.py                                                     
2.30 pm to 4.30 pm                                                              
date: 25-07-2016                                                              
time: 02:30 PM                                                                 
<area forecast="LR" lat="1.37500000" lon="103.83900000" name="Ang Mo Kio"/>   
<area forecast="LR" lat="1.32100000" lon="103.92400000" name="Bedok"/>        
<area forecast="LR" lat="1.35077200" lon="103.83900000" name="Bishan"/>       
<area forecast="LR" lat="1.30400000" lon="103.70100000" name="Boon Lay"/>     
<area forecast="LR" lat="1.35300000" lon="103.75400000" name="Bukit Batok"/>  
<area forecast="LR" lat="1.27700000" lon="103.81900000" name="Bukit Merah"/>

我有几个问题不知道如何解决:

  1. 有什么方法可以检索area forecast="LR" lat="1.37500000" lon="103.83900000" name="宏茂桥" 没有它的标签?

    我尝试将“.text”添加到我的代码中,但总是会出错

  2. 我希望我的输出采用 JSON 格式,因为它不是表格格式,如有关如何使用 python 创建 JSON 文件的教程中所示:/

编辑:我已设法在 JSON 文件中打开数据,但是如何将 unicode 字符串格式化为普通字符串,因为结果包含 u'?

【问题讨论】:

  • 1.您的问题不是很清楚,因为&lt;area forecast="LR" lat="1.37500000" lon="103.83900000" name="Ang Mo Kio"/&gt; 是一个具有许多属性forecastlatlonname)的单个标签(area)。 2.也不是很清楚。如果你想要一个 JSON 格式,你可以自己解析它并创建一个 JSON 格式的输出..
  • 嗨,很抱歉造成混乱。我想检索区域预测的属性。另外,我可以知道如何以 JSON 格式解析它吗?谢谢
  • 您可以像这样使用 attrs 属性:area_attrs_li = [area.attrs for area in soup.find('weatherForecast').find_all('area')]
  • 我使用了你提供的代码,结果是: 请问如何删除u?

标签: python json beautifulsoup


【解决方案1】:

在你的代码中试试这个:

with open("nea.json",'a+') as fs:
    for area in soup.find('weatherForecast').find_all('area'):
        fs.write(str(area.attrs))

【讨论】:

  • 嗨,它有效! :) 但我可以知道如何在 json 文件中的每个属性之间有一条线吗? :)
  • 写文件的时候加个"\n",试试看。
  • 抱歉,这听起来可能很愚蠢,但我应该在哪里添加“\n”? :x
  • fs.write(str(area.attrs)+"\n") 这将在每一行之后放置一行。
  • 您好,可以通过电子邮件将结果发送给您吗?它似乎有什么问题:/
猜你喜欢
  • 1970-01-01
  • 2021-12-05
  • 2017-03-13
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多