如何从我掌握的html文档中提取特定内容

时间:2016-01-07 13:55:02

标签: python html url beautifulsoup

我是Beautifulsoup和urllib的新人 我想要做的是阅读pm25.in的数据,这是一个提供中国大气质量数据的网站。

我的早期尝试

### set a specific city name, token is the public key for free use
city = 'zhuhai'
html_doc = urllib.urlopen("http://www.pm25.in/api/querys/co.json?        
city=zhuhai&token=5j1znBVAsnSf5xQyNQyq").read().decode('utf-8')
soup = BeautifulSoup(html_doc)    

结果

<html>

<body>
  <p>
    [{"aqi":29,"area":"珠海","co":0.591,"co_24h":0.955,"position_name":"吉大","primary_pollutant":null,"quality":"优","station_code":"1367A","time_point":"2016-01-07T20:00:00Z"},{"aqi":51,"area":"珠海","co":0.913,"co_24h":1.059,"position_name":"前山","primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":"1368A","time_point":"2016-01-07T20:00:00Z"},{"aqi":35,"area":"珠海","co":0.699,"co_24h":0.885,"position_name":"唐家","primary_pollutant":null,"quality":"优","station_code":"1369A","time_point":"2016-01-07T20:00:00Z"},{"aqi":52,"area":"珠海","co":0.874,"co_24h":0.949,"position_name":"斗门","primary_pollutant":"颗粒物(PM10)","quality":"良","station_code":"1370A","time_point":"2016-01-07T20:00:00Z"},{"aqi":67,"area":"珠海","co":0.769,"co_24h":0.962,"position_name":null,"primary_pollutant":"臭氧8小时","quality":"良","station_code":null,"time_point":"2016-01-07T20:00:00Z"}]
  </p>
</body>

</html>

我的目标

这是我手动编辑为模板的数据框。

http://i4.tietuku.com/71f10394dbedd8d3.png

我想知道如何从原始html_doc中提取这些有用的数据 我已经尝试了soup.find_all(),但我不知道如何设置参数来实现这一点。

1 个答案:

答案 0 :(得分:1)

您可以使用json模块将soup转换为列表:

import json
...
l = soup.p.get_text()  # find p tag and extract text from it
result = json.loads(l) # convert text into python list

现在您已拥有列表result,您可以按照需要的方式操作数据。