python爬虫天气实例scrapy_2017.08.04 Python网络爬虫之Scrapy爬虫实战二 天气预报...

本文介绍了如何使用Python的Scrapy框架爬取天气预报网站的数据,包括创建项目、定义Items、编写Spider、设置选择器、处理响应、定义Pipeline保存数据,以及最后运行爬虫的详细步骤。
摘要由CSDN通过智能技术生成

1.项目准备:网站地址:http://quanzhou.tianqi.com/

2c265d8841cf1a86a525b9ea65b6963b.png

2.创建编辑Scrapy爬虫:

scrapy startproject weather

scrapy genspider HQUSpider quanzhou.tianqi.com

26c6abca9691ba67b1005e770140398f.png

项目文件结构如图:

3efc42bdc3ea83564ed36438c9b848a3.png

3.修改Items.py:

3cb712ed74ea6b247abeb3bf2ca025fc.png

4.修改Spider文件HQUSpider.py:

(1)先使用命令:scrapy shell http://quanzhou.tianqi.com/   测试和获取选择器:

9e6b847d1c05363276154a91d3e4f4aa.png

(2)试验选择器:打开chrome浏览器,查看网页源代码:

d539b1470d4dfe3e74f0a76bc45d5731.png

(3)执行命令查看response结果:

2c69460afd7101226133595be8fd190b.png

(4)编写HQUSpider.py文件:

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

import scrapy

from weather.items import WeatherItem

class HquspiderSpider(scrapy.Spider):

name = ‘HQUSpider‘

allowed_domains = [‘tianqi.com‘]

citys=[‘quanzhou‘,‘datong‘]

start_urls = []

for city in citys:

start_urls.append(‘http://‘+city+‘.tianqi.com/‘)

def parse(self, response):

subSelector=response.xpath(‘//div[@class="tqshow1"]‘)

items=[]

for sub in subSelector:

item=WeatherItem()

cityDates=‘‘

for cityDate in sub.xpath(‘./h3//text()‘).extract():

cityDates+=cityDate

item[‘cityDate‘]=cityDates

item[‘week‘]=sub.xpath(‘./p//text()‘).extract()[0]

item[‘img‘]=sub.xpath(‘./ul/li[1]/img/@src‘).extract()[0]

temps=‘‘

for temp in sub.xpath(‘./ul/li[2]//text()‘).extract():

temps+=temp

item[‘temperature‘]=temps

item[‘weather‘]=sub.xpath(‘./ul/li[3]//text()‘).extract()[0]

item[‘wind‘]=sub.xpath(‘./ul/li[4]//text()‘).extract()[0]

items.append(item)

return items

(5)修改pipelines.py我,处理Spider的结果:

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

# Define your item pipelines here

#

# Don‘t forget to add your pipeline to the ITEM_PIPELINES setting

# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import time

import os.path

import urllib2

import sys

reload(sys)

sys.setdefaultencoding(‘utf8‘)

class WeatherPipeline(object):

def process_item(self, item, spider):

today=time.strftime(‘%Y%m%d‘,time.localtime())

fileName=today+‘.txt‘

with open(fileName,‘a‘) as fp:

fp.write(item[‘cityDate‘].encode(‘utf-8‘)+‘\t‘)

fp.write(item[‘week‘].encode(‘utf-8‘)+‘\t‘)

imgName=os.path.basename(item[‘img‘])

fp.write(imgName+‘\t‘)

if os.path.exists(imgName):

pass

else:

with open(imgName,‘wb‘) as fp:

response=urllib2.urlopen(item[‘img‘])

fp.write(response.read())

fp.write(item[‘temperature‘].encode(‘utf-8‘)+‘\t‘)

fp.write(item[‘weather‘].encode(‘utf-8‘)+‘\t‘)

fp.write(item[‘wind‘].encode(‘utf-8‘)+‘\n\n‘)

time.sleep(1)

return item

103401b350bb32303ae84a90e543c0e6.png

(6)修改settings.py文件,决定由哪个文件来处理获取的数据:

2f330023cb03f337fa3f3607fc27bdf8.png

(7)执行命令:scrapy crawl HQUSpider

6fd5d9c1f08b6bba98c48160eb21cc95.png

到此为止,一个完整的Scrapy爬虫就完成了。

原文:http://www.cnblogs.com/hqutcy/p/7284302.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值