python爬虫抓取全国pm2.5的空气质量(BeautifulSoup3)

这个编码格式真的是很闹心啊,看来真的得深入学习一下编码格式,要不这各种格式错误。

这个编码还和编辑器有关系,最开始的时候实在sublime Text里编辑的代码,运行起来卡卡的,特别顺畅,但突然发现它不支持raw_input和input,所以令临时换到了python官方提供的idle中。之后就出现了各种奇葩编码错误。。。。。。

程序大概意思就是,你输入一个城市的拼音,它就会返回这个城市的空气污染情况啊,有些城市可能会没有,这个完全取决与网站上有那些城市啊,当你想退出的时候就输入quit,就退出来了。

里面还有一个多线程的写法,可以体验一下单线程和多线程之间的速度是有很大差距的。。。

# -*- coding: utf-8 -*-

import urllib2
import threading
from time import ctime
import BeautifulSoup    #besutifulsoup的第三版
import re

def getPM25(cityname):
    site = 'http://www.pm25.com/' + cityname + '.html'
    html = urllib2.urlopen(site)
    soup = BeautifulSoup.BeautifulSoup(html)

    #city = soup.find(class_ = 'bi_loaction_city')   # 城市名称
    city = soup.find("h2",{"class":"bi_loaction_city"})
    aqi = soup.find("a",{"class":"bi_aqiarea_num"})  # AQI指数
    quality = soup.find("span",{"class":"bi_aqiarea_wuran wuranlevel_1"})  # 空气质量等级
    result = soup.find("div",{"class":'bi_aqiarea_bottom'})   # 空气质量描述
    replacechar = re.compile("<.*?>")  #为了将<>全部替换成空
    print city.string + u'\nAQI指数:' + aqi.string + u'\n空气质量:' + quality.string + replacechar.sub('',str(result)).decode('utf-8')
    print '*'*20 + ctime() + '*'*20

def one_thread(cityname1):   # 单线程
    print 'One_thread Start: ' + ctime() + '\n'
    getPM25(cityname1)

def two_thread():   # 多线程
    print 'Two_thread Start: ' + ctime() + '\n'
    threads = []
    t1 = threading.Thread(target=getPM25,args=('beijing',))
    threads.append(t1)
    t2 = threading.Thread(target=getPM25,args=('shenyang',))
    threads.append(t2)

    for t in threads:
        # t.setDaemon(True)
        t.start()

if __name__ == '__main__':

    print "*"*20+"welcome to 京东放养的爬虫"+"*"*20
    while True:
        cityname1 = raw_input("please input cityname:")
        if cityname1 == 'quit':
            break
        one_thread(cityname1)
    #print '\n' * 2
    #two_thread(cityname)
时间: 2024-08-24 19:45:54

python爬虫抓取全国pm2.5的空气质量(BeautifulSoup3)的相关文章

python 爬虫抓取心得

quanwei9958 转自 python 爬虫抓取心得分享 urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quote('要编码的字符串') query = urllib.quote(singername) url = 'http://music.baidu.com/search?key='+query response = urllib.urlopen(url) text = response.read()

Python爬虫抓取网页图片

本文通过python 来实现这样一个简单的爬虫功能,把我们想要的图片爬取到本地. 下面就看看如何使用python来实现这样一个功能. # -*- coding: utf-8 -*- import urllib import re import time import os #显示下载进度 def schedule(a,b,c): ''''' a:已经下载的数据块 b:数据块的大小 c:远程文件的大小 ''' per = 100.0 * a * b / c if per > 100 : per =

python爬虫抓取站长之家IP库,仅供练习用!

python爬虫抓取站长之家IP库,单线程的,仅供练习,IP库数据有43亿条,如果按此种方法抓取至少得数年,所以谨以此作为练手,新手代码很糙,请大家见谅. #!/usr/bin/python #coding=UTF-8 import urllib2 import re import os import csv import codecs user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-

python 爬虫抓取心得分享

/** author: insun title:python 爬虫抓取心得分享 blog:http://yxmhero1989.blog.163.com/blog/static/112157956201311821444664/ **/    0x1.urllib.quote('要编码的字符串') 如果你要在url请求里面放入中文,对相应的中文进行编码的话,可以用: urllib.quote('要编码的字符串') query = urllib.quote(singername) url = 'h

python爬虫抓取哈尔滨天气信息

python 爬虫 爬取哈尔滨天气信息 - http://www.weather.com.cn/weather/101050101.shtml 环境: windows7 python3.4(pip install requests:pip install BeautifulSoup4) 代码:(亲测可以正确执行) 1 # coding:utf-8 2 """ 3 总结一下,从网页上抓取内容大致分3步: 4 1.模拟浏览器访问,获取html源代码 5 2.通过正则匹配,获取指定

Python爬虫抓取技术的门道

web是一个开放的平台,这也奠定了web从90年代初诞生直至今日将近30年来蓬勃的发展.然而,正所谓成也萧何败也萧何,开放的特性.搜索引擎以及简单易学的html.css技术使得web成为了互联网领域里最为流行和成熟的信息传播媒介:但如今作为商业化软件,web这个平台上的内容信息的版权却毫无保证,因为相比软件客户端而言,你的网页中的内容可以被很低成本.很低的技术门槛实现出的一些抓取程序获取到,这也就是这一系列文章将要探讨的话题-- 网络爬虫 . 有很多人认为web应当始终遵循开放的精神,呈现在页面

Python爬虫抓取 python tutorial中文版,保存为word

看到了中文版的python tutorial,发现是网页版的,刚好最近在学习爬虫,想着不如抓取到本地 首先是网页的内容 查看网页源码后发现可以使用BeautifulSoup来获取文档的标题和内容,并保存为doc文件. 这里需要使用from bs4 import BeautifulSoup 来导入该模块 具体代码如下: # 输出所在网址的内容from bs4 import BeautifulSoup def introduce(url): res = requests.get(url) res.e

Python爬虫--抓取糗事百科段子

今天使用python爬虫实现了自动抓取糗事百科的段子,因为糗事百科不需要登录,抓取比较简单.程序每按一次回车输出一条段子,代码参考了 http://cuiqingcai.com/990.html 但该博主的代码似乎有些问题,我自己做了修改,运行成功,下面是代码内容: 1 # -*- coding:utf-8 -*- 2 __author__ = 'Jz' 3 import urllib2 4 import re 5 6 #糗事百科爬虫类 7 class QSBK: 8 #初始化 9 def __

python 爬虫抓取 MOOC 中国课程的讨论区内容

一:selenium 库 selenium 每次模拟浏览器打开页面,xpath 匹配需要抓取的内容.可以,但是特别慢,相当慢.作为一个对技术有追求的爬虫菜鸡,狂补了一些爬虫知识.甚至看了 scrapy 框架,惊呆了,真棒! 网上很多关于 selenium 库的详细介绍,这里略过此方法. 二: requests 库 编写一个爬虫小脚本,requests 库极为方便.接下来进入正题,如何抓取 MOOC 中国上课程的讨论内容! 1. 分析网页数据 打开你需要抓取数据的课程页面,点击讨论区之后页面加载讨