安装方法:pipinstallBeautifulSoup4BeautifulSoup详细使用文档
墨迹天气抓取演示
墨迹天气没有提供专门的天气接口api,但我们可以用BeautifulSoup来简单的爬取到信息。墨迹天气真的很准呢,里面还有空气质量可以直接获取到,很方便呢。
定位方法:https://tianqmojcom/weather/china/beijing不确定省后面怎么拼,直接用省的拼写进入页面,然后找到对应的市县区进入后就有路径了。
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = urlopen('https://tianqi.moji.com/weather/china/beijing/haidian-district')
soup = BeautifulSoup(url, 'html.parser') # parser 解析
alert = soup.find('p', class_='wea_alert clearfix').em
print('空气质量:' + alert.string)
weather = soup.find('p', class_='wea_weather clearfix')
print('当前温度:' + weather.em.string + '℃')
print('天气:' + weather.b.string)
文章为作者独立观点,不代表股票交易接口观点