欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間: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ù),之所以抓取這個(gè)網(wǎng)站,是因?yàn)樯厦嬗谐鞘蠵M2.5濃度排名(其實(shí)真正的原因是,它是百度搜PM2.5出來的第一個(gè)網(wǎng)站?。?/p>

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

最后吐槽一下:上海的空氣質(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ì)量等級(jí)
    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)文章

最新評(píng)論