首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从我掌握的html文档中提取特定内容

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

Stack Overflow用户
提问于 2016-01-07 13:55:01
回答 1查看 21关注 0票数 0

我是新来的美女汤和乌尔利布。

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

我的早期尝试

代码语言:javascript
复制
### 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)    

结果

代码语言:javascript
复制
<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>

我的目标

下面是我作为模板手动编辑的dataframe。

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

我想知道如何从原始html_doc中提取这些有用的数据。

我已经尝试过soup.find_all(),但我不知道如何设置这些参数来实现这一目标。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-07 14:09:52

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

代码语言:javascript
复制
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,您可以以所需的方式操作数据。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34656929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档