How to read the content of an website?

倖福魔咒の 提交于 2019-12-06 05:19:01

If you are interested in the Air Quality Index number, find the div with aqivalue class:

>>> import urllib
>>> from bs4 import BeautifulSoup
>>> 
>>> url = "http://aqicn.org/city/shenyang/usconsulate/json"
>>> soup = BeautifulSoup(urllib.urlopen(url), "html.parser")
>>> soup.find("div", class_="aqivalue").get_text()
u'171'

The first url http://aqicn.org/city/shenyang/usconsulate/json actually does not give back JSON data. It gives back HTML data. If you're really interested in this content, you have to parse the HTML data.

You can do this with Beautifulsoup's HTML parser, though the lxml.html package is somewhat more straightforward.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!