Python实现抓取城市的PM2.5浓度和排名


Posted in Python onMarch 19, 2015

主机环境:(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()

Python实现抓取城市的PM2.5浓度和排名

Python实现抓取城市的PM2.5浓度和排名

以上就是本文所述的全部内容了,希望大家能够喜欢。

Python 相关文章推荐
Python httplib,smtplib使用方法
Sep 06 Python
python 实现自动远程登陆scp文件实例代码
Mar 13 Python
python django事务transaction源码分析详解
Mar 17 Python
python实现图书管理系统
Mar 12 Python
Python使用win32com模块实现数据库表结构自动生成word表格的方法
Jul 17 Python
python中cPickle类使用方法详解
Aug 27 Python
pygame实现简易飞机大战
Sep 11 Python
python3+PyQt5 数据库编程--增删改实例
Jun 17 Python
Python视频编辑库MoviePy的使用
Apr 01 Python
python中if及if-else如何使用
Jun 02 Python
python 实现围棋游戏(纯tkinter gui)
Nov 13 Python
在Ubuntu中安装并配置Pycharm教程的实现方法
Jan 06 Python
python在windows命令行下输出彩色文字的方法
Mar 19 #Python
python通过colorama模块在控制台输出彩色文字的方法
Mar 19 #Python
python实现颜色rgb和hex相互转换的函数
Mar 19 #Python
python实现从一组颜色中找出与给定颜色最接近颜色的方法
Mar 19 #Python
python遍历类中所有成员的方法
Mar 18 #Python
python获取图片颜色信息的方法
Mar 18 #Python
Python调用C语言开发的共享库方法实例
Mar 18 #Python
You might like
linux php mysql数据库备份实现代码
2009/03/10 PHP
php addslashes和mysql_real_escape_string
2010/01/24 PHP
PHP实现通过Luhn算法校验信用卡卡号是否有效
2015/03/23 PHP
php简单统计字符串单词数量的方法
2015/06/19 PHP
PHP7扩展开发之hello word实现方法详解
2018/01/15 PHP
在laravel5.2中实现点击用户头像更改头像的方法
2019/10/14 PHP
B/S开发中常用javaScript技术与代码
2007/03/09 Javascript
javascript 学习之旅 (1)
2009/02/05 Javascript
jQuery中json对象的复制方式介绍(数组及对象)
2013/06/08 Javascript
js 数值转换为3位逗号分隔的示例代码
2014/02/19 Javascript
node.js中的fs.futimes方法使用说明
2014/12/17 Javascript
jquery实现适用于门户站的导航下拉菜单效果代码
2015/08/24 Javascript
javascript判断网页是关闭还是刷新
2015/09/12 Javascript
jQuery设置Cookie及删除Cookie实例分析
2016/04/15 Javascript
详解堆的javascript实现方法
2016/11/29 Javascript
详解Vue 普通对象数据更新与 file 对象数据更新
2017/04/26 Javascript
微信小程序实现身份证取景框拍摄
2020/09/09 Javascript
基于Python实现通过微信搜索功能查看谁把你删除了
2016/01/27 Python
python实现斐波那契数列的方法示例
2017/01/12 Python
Python实现的微信公众号群发图片与文本消息功能实例详解
2017/06/30 Python
python自动查询12306余票并发送邮箱提醒脚本
2018/05/21 Python
如何用Python制作微信好友个性签名词云图
2019/06/28 Python
对Python3中列表乘以某一个数的示例详解
2019/07/20 Python
学Python 3的理由和必要性
2019/11/19 Python
django实现web接口 python3模拟Post请求方式
2019/11/19 Python
python、Matlab求定积分的实现
2019/11/20 Python
TensorFlow实现自定义Op方式
2020/02/04 Python
Pytorch损失函数nn.NLLLoss2d()用法说明
2020/07/07 Python
香港太阳眼镜网上商店:SmartBuyGlasses香港
2016/07/22 全球购物
澳洲在线厨具商店:Kitchen Style
2018/05/05 全球购物
Carolina工作鞋官网:Carolina Footwear
2019/03/14 全球购物
写好自荐信的技巧
2013/11/08 职场文书
服装机修工岗位职责
2013/12/26 职场文书
新员工试用期自我鉴定
2014/04/17 职场文书
广播节目策划方案
2014/05/23 职场文书
入党宣誓大会后的感想
2015/08/10 职场文书