python sdweather_爬虫框架抓取练习-JMU SD weatherspider,scrapy,jmusdWeatherSpider

python爬取中国天气网福建省当日温度

一、效果图

5f6d1f9b351331c5a785dfdad75728fd.png

二、运行步骤

创建项目:scrapy startproject sdWeatherSpider

执行命令: scrapy crawl everyCityinSD ,运行爬虫程序

三、主要代码

everyCityinSD.py

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

import scrapy

from urllib.request import urlopen

from sdWeatherSpider.items import SdweatherspiderItem

import re

class EverycityinsdSpider(scrapy.Spider):

name = 'everyCityinSD'

allowed_domains = ['www.weather.com.cn']

start_urls = ['http://www.weather.com.cn/fujian/index.shtml']

def parse(self, response):

# 处理每个城市的天气预报页面数据

item = SdweatherspiderItem()

citys=[]

weather=[]

selector = response.xpath('//div[@class="forecastBox"]')

for dl in selector.xpath('./dl'):

city = dl.xpath('./dt//a//text()').extract()[0]

high=dl.xpath('./dd//a//span//text()').extract()[0]

low=dl.xpath('./dd//b//text()').extract()[0]

tem='high'+high+'low'+low

citys.append(city)

weather.append(tem)

# 存放天气数据

item['city'] = citys

item['weather'] = weather

return [item]

pass

有部分配置文件要修改。如果自行创建框架了,就只要替换everyCityinSD.py   ,settings.py,pipelines.py,items.py,以及画图的draw

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值