国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現抓取城市的PM2.5濃度和排名

2019-11-25 17:55:37
字體:
來源:轉載
供稿:網友

主機環境:(Python2.7.9 / Win8_64 / bs4)

利用BeautifulSoup4來抓取 www.pm25.com 上的PM2.5數據,之所以抓取這個網站,是因為上面有城市PM2.5濃度排名(其實真正的原因是,它是百度搜PM2.5出來的第一個網站!)

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

最后吐槽一下:上海的空氣質量怎么這么差!!!

PM25.py

復制代碼 代碼如下:

#!/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指數
    quality = soup.select(".bi_aqiarea_right span")  # 空氣質量等級
    result = soup.find("div",class_ ='bi_aqiarea_bottom')   # 空氣質量描述
    print city.text + u'AQI指數:' + aqi.text + u'/n空氣質量:' + 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()

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梁河县| 郎溪县| 鹿邑县| 定边县| 平罗县| 屏东市| 依安县| 玛多县| 漳州市| 宣武区| 应城市| 龙里县| 固安县| 栾城县| 林口县| 涪陵区| 呼和浩特市| 靖西县| 台东县| 乡宁县| 平罗县| 东乌珠穆沁旗| 潢川县| 丹凤县| 嘉定区| 馆陶县| 泰顺县| 广安市| 新闻| 密山市| 怀来县| 洪泽县| 浙江省| 常熟市| 望奎县| 鄂托克旗| 黄平县| 惠安县| 崇义县| 永兴县| 错那县|