最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)抓取城市的PM2.5濃度和排名

 更新時間:2015年03月19日 14:54:42   投稿:hebedich  
本文給大家介紹的是一則使用Python實(shí)現(xiàn)抓取城市的PM2.5數(shù)據(jù)和排名,

主機(jī)環(huán)境:(Python2.7.9 / Win8_64 / bs4)

利用BeautifulSoup4來抓取 www.pm25.com 上的PM2.5數(shù)據(jù),之所以抓取這個網(wǎng)站,是因?yàn)樯厦嬗谐鞘蠵M2.5濃度排名(其實(shí)真正的原因是,它是百度搜PM2.5出來的第一個網(wǎng)站?。?/p>

程序里只對比了兩個城市,所以多線程的速度提升并不是很明顯,大家可以弄10個城市并開10個線程試試。

最后吐槽一下:上海的空氣質(zhì)量怎么這么差?。。?/p>

PM25.py

復(fù)制代碼 代碼如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# by ustcwq
import urllib2
import threading
from time import ctime
from bs4 import BeautifulSoup
def getPM25(cityname):
    site = 'http://www.pm25.com/' + cityname + '.html'
    html = urllib2.urlopen(site)
    soup = BeautifulSoup(html)
    city = soup.find(class_ = 'bi_loaction_city')   # 城市名稱
    aqi = soup.find("a",{"class","bi_aqiarea_num"})  # AQI指數(shù)
    quality = soup.select(".bi_aqiarea_right span")  # 空氣質(zhì)量等級
    result = soup.find("div",class_ ='bi_aqiarea_bottom')   # 空氣質(zhì)量描述
    print city.text + u'AQI指數(shù):' + aqi.text + u'\n空氣質(zhì)量:' + quality[0].text + result.text
    print '*'*20 + ctime() + '*'*20
def one_thread():   # 單線程
    print 'One_thread Start: ' + ctime() + '\n'
    getPM25('hefei')
    getPM25('shanghai')
def two_thread():   # 多線程
    print 'Two_thread Start: ' + ctime() + '\n'
    threads = []
    t1 = threading.Thread(target=getPM25,args=('hefei',))
    threads.append(t1)
    t2 = threading.Thread(target=getPM25,args=('shanghai',))
    threads.append(t2)
    for t in threads:
        # t.setDaemon(True)
        t.start()
if __name__ == '__main__':
    one_thread()
    print '\n' * 2
    two_thread()

以上就是本文所述的全部內(nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評論

河北区| 河曲县| 安徽省| 五河县| 莱芜市| 钟祥市| 昌图县| 疏附县| 石狮市| 福海县| 钟祥市| 福泉市| 兴化市| 天柱县| 淮南市| 龙州县| 固安县| 丹江口市| 无为县| 玉树县| 缙云县| 屏山县| 崇明县| 宝兴县| 鄂温| 河源市| 郎溪县| 蒲江县| 靖州| 玉树县| 湾仔区| 望都县| 集贤县| 吉水县| 阳新县| 安塞县| 托克逊县| 德州市| 洞口县| 锦州市| 北安市|