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

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

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

2.创建编辑Scrapy爬虫:

scrapy startproject weather

scrapy genspider HQUSpider quanzhou.tianqi.com

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

项目文件结构如图:

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

3.修改Items.py:

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

4.修改Spider文件HQUSpider.py:

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

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

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

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

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

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

(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

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

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

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

(7)执行命令:scrapy crawl HQUSpider

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

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

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

Original: https://blog.csdn.net/weixin_42365688/article/details/113672625
Author: 闪电肉
Title: python爬虫天气实例scrapy_2017.08.04 Python网络爬虫之Scrapy爬虫实战二 天气预报…

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/791570/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • AI绘画,Midjourney极简入门

    前几天看报道说: 一位小哥用AI绘画工具Midjourney生成的作品,在美国科罗拉多州博览会的艺术比赛中获得了第一名。作者表示,他多次调整了输入的提示词,生成了100多幅画作,经…

    Python 2023年10月31日
    034
  • 【爬虫实例2】91网视频爬取

    1、 导入模块 import requests import re 2、获取m3u8文件 url地址 url = ‘http://www.wwmulu.com/rj/xhcl/pl…

    Python 2023年6月11日
    075
  • Pjax 下动态加载插件方案

    在纯静态网站里,有时候会动态更新某个区域往会选择 Pjax(swup、barba.js)去处理,他们都是使用 ajax 和 pushState 通过真正的永久链接,页面标题和后退按…

    Python 2023年10月20日
    034
  • pandas中dataframe部分操作总结

    文章目录 * – 基本操作 – + 1.构建dataframe + * (1)创建 + 2.对于大型的dataframe,head方法将只选出头部的五行;t…

    Python 2023年8月7日
    048
  • Dubbo-时间轮设计

    前言 Dubbo源码阅读分享系列文章,欢迎大家关注点赞 SPI实现部分 Dubbo-SPI机制 Dubbo-Adaptive实现原理 Dubbo-Activate实现原理 Dubb…

    Python 2023年10月15日
    032
  • 【行人轨迹预测数据集——ETH、UCY】

    下载地址 ETH数据集之前的链接已经失效了,可以通过ETHz官网搜索关键词”walking pedestrians dataset”,我找到在Compute…

    Python 2023年9月30日
    0106
  • python+requests+pytest 接口自动化框架(二)

    目录 一、Fixture固件 scope: 1.基础应用:scope是function 2.scope为class 3.scope作用域是module或package/sessio…

    Python 2023年9月10日
    055
  • Python-Pandas库中的透视表

    啊哦~你想找的内容离你而去了哦 内容不存在,可能为如下原因导致: ① 内容还在审核中 ② 内容以前存在,但是由于不符合新 的规定而被删除 ③ 内容地址错误 ④ 作者删除了内容。 可…

    Python 2023年8月16日
    051
  • Django manage.py 命令详解

    查看命令的作用的语句 C:\Users\Administrator> python manage.py help Type ‘manage.py help <subco…

    Python 2023年6月9日
    069
  • Google colab降级conda、cudnn、安装tensorflow1.x

    面向的问题:现在google colab支持tensoflow2.x和1.x,默认使用2.x版本 google提供版本切换方式 方法一 %tensorflow_version 1….

    Python 2023年9月8日
    047
  • Python爬取往期股票数据,分析中奖规律!

    快过年了,手头有点紧,但是作为一个男人,身上怎么能够没有大把钞票呢? 于是我决定用Python来分析一波股票,赢了会所嫩*,输了下海干活! 好了,上面是我吹牛逼的,不过确实有小伙伴…

    Python 2023年10月30日
    036
  • 【VisionMaster SDK开发】第三讲 C#二次开发介绍及应用案例

    目录 前言 一、VM SDK(4.2版本)介绍 二、开发步骤 * 2.1 方案加载 2.2 参数修改 2.3 流程执行 2.4 结果获取 总结 前言 VisionMaster(后简…

    Python 2023年9月16日
    036
  • python数据可视化——生成数据一

    数据可视化: 指的是通过可视化表示来探索数据,他与数据分析紧密相关,而数据分析指的是使用代码来探索数据集的规律和关联,数据集可以是用一行代码就能表示的小型数字列表,也可以是数千兆字…

    Python 2023年9月3日
    050
  • python在线电影网站-四

    VISION在线电影网站一4 目录 django具体实现 * 一、新建项目 – 配置环境 新建项目 启动Django项目 二、Django根据数据库表生成Model &…

    Python 2023年8月4日
    069
  • 为什么建议初学者选择Python入门?

    这个星球的编程语言有数百上千种,能够称为热门的也只有那十几种而已,比如C、C++、Java、C#、PHP等,这些编程语言也都十分火爆。 但对于新人来讲,尤其是现阶段对编程语言的优劣…

    Python 2023年10月30日
    050
  • 【图文详解】入职必备——SVN使用教程

    文章目录 * – 前言 – 1、SVN简介 – 2、SVN仓库 – 3、SVN客户端 – 4、SVN基础操作 &#821…

    Python 2023年9月16日
    088
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球