Scrapy爬虫实例——校花网
学习爬虫有一段时间了,今天使用Scrapy框架将校花网的图片爬取到本地。Scrapy爬虫框架相对于使用requests库进行网页的爬取,拥有更高的性能。
Scrapy官方定义:Scrapy是用于抓取网站并提取结构化数据的应用程序框架,可用于广泛的有用应用程序,如数据挖掘,信息处理或历史存档。
建立Scrapy爬虫工程
在安装好Scrapy框架后,直接使用命令行进行项目的创建:
E:\ScrapyDemo>scrapy startproject xiaohuar
New Scrapy project 'xiaohuar', using template directory 'c:\\users\\lei\\appdata\\local\\programs\\python\\python35\\lib
\\site-packages\\scrapy\\templates\\project', created in:
E:\ScrapyDemo\xiaohuar You can start your first spider with:
cd xiaohuar
scrapy genspider example example.com
创建一个Scrapy爬虫
创建工程的时候,会自动创建一个与工程同名的目录,进入到目录中执行如下命令:
E:\ScrapyDemo\xiaohuar>scrapy genspider -t basic xiaohua xiaohuar.com
Created spider 'xiaohua' using template 'basic' in module:
xiaohuar.spiders.xiaohua
命令中"xiaohua"是生成Spider中*.py文件的文件名,"xiaohuar.com"是将要爬取网站的URL,可以在程序中更改。
编写Spider代码
编写E:\ScrapyDemo\xiaohuar\xiaohuar\spiders中的xiaohua.py文件。主要是配置URL和对请求到的页面的解析方式。
# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
import re class XiaohuaSpider(scrapy.Spider):
name = 'xiaohua'
allowed_domains = ['xiaohuar.com']
start_urls = []
for i in range(43):
url = "http://www.xiaohuar.com/list-1-%s.html" %i
start_urls.append(url) def parse(self, response):
if "www.xiaohuar.com/list-1" in response.url:
# 下载的html源代码
html = response.text
# 网页中图片存储地址:src="/d/file/20160126/905e563421921adf9b6fb4408ec4e72f.jpg"
# 通过正则匹配到所有的图片
# 获取的是图片的相对路径的列表
img_urls = re.findall(r'/d/file/\d+/\w+\.jpg',html) # 使用循环对图片页进行请求
for img_url in img_urls:
# 将图片的URL补全
if "http://" not in img_url:
img_url = "http://www.xiaohuar.com%s" %img_url # 回调,返回response
yield Request(img_url)
else:
# 下载图片
url = response.url
# 保存的图片文件名
title = re.findall(r'\w*.jpg',url)[0]
# 保存图片
with open('E:\\xiaohua_img\\%s' % title, 'wb') as f:
f.write(response.body)
这里使用正则表达式对图片的地址进行匹配,其他网页也都大同小异,需要根据具体的网页源代码进行分析。
运行爬虫
E:\ScrapyDemo\xiaohuar>scrapy crawl xiaohua
-- :: [scrapy.utils.log] INFO: Scrapy 1.4. started (bot: xiaohuar)
-- :: [scrapy.utils.log] INFO: Overridden settings: {'BOT_NAME': 'xiaohuar', 'SPIDER_MODULES': ['xiaohuar.
spiders'], 'ROBOTSTXT_OBEY': True, 'NEWSPIDER_MODULE': 'xiaohuar.spiders'}
-- :: [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.logstats.LogStats']
-- :: [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
-- :: [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
-- :: [scrapy.middleware] INFO: Enabled item pipelines:
[]
-- :: [scrapy.core.engine] INFO: Spider opened
-- :: [scrapy.extensions.logstats] INFO: Crawled pages (at pages/min), scraped items (at items/min
)
-- :: [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/robots.txt> (referer: None)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/list-1-0.html> (referer: None
)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170721/cb96f1b106b3d
b4a6bfcf3d2e880dea0.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170824/dcc166b0eba6a
37e05424cfc29023121.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170916/7f78145b1ca16
2eb814fbc03ad24fbc1.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170919/2f728d0f110a2
1fea95ce13e0b010d06.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170819/9c3dfeef7e08c
c0303ce233e4ddafa7f.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170917/715515e7fe1f1
cb9fd388bbbb00467c2.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170628/f3d06ef49965a
edbe18286a2f221fd9f.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170513/6121e3e90ff3b
a4c9398121bda1dd582.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170516/6e295fe48c332
45be858c40d37fb5ee6.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170707/f7ca636f73937
e33836e765b7261f036.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170528/b352258c83776
b9a2462277dec375d0c.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170527/4a7a7f1e6b69f
126292b981c90110d0a.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170715/61110ba027f00
4fb503ff09cdee44d0c.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170520/dd21a21751e24
a8f161792b66011688c.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170529/8140c4ad797ca
01f5e99d09c82dd8a42.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170603/e55f77fb3aa3c
7f118a46eeef5c0fbbf.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170529/e5902d4d3e408
29f9a0d30f7488eab84.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170604/ec3794d0d42b5
38bf4461a84dac32509.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170603/c34b29f68e8f9
6d44c63fe29bf4a66b8.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170701/fb18711a6af87
f30942d6a19f6da6b3e.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170619/e0456729d4dcb
ea569a1acbc6a47ab69.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] DEBUG: Crawled () <GET http://www.xiaohuar.com/d/file/20170626/0ab1d89f54c90
df477a90aa533ceea36.jpg> (referer: http://www.xiaohuar.com/list-1-0.html)
-- :: [scrapy.core.engine] INFO: Closing spider (finished)
-- :: [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': ,
'downloader/request_count': ,
'downloader/request_method_count/GET': ,
'downloader/response_bytes': ,
'downloader/response_count': ,
'downloader/response_status_count/200': ,
'finish_reason': 'finished',
'finish_time': datetime.datetime(, , , , , , ),
'log_count/DEBUG': ,
'log_count/INFO': ,
'request_depth_max': ,
'response_received_count': ,
'scheduler/dequeued': ,
'scheduler/dequeued/memory': ,
'scheduler/enqueued': ,
'scheduler/enqueued/memory': ,
'start_time': datetime.datetime(, , , , , , )}
-- :: [scrapy.core.engine] INFO: Spider closed (finished)
scrapy crawl xiaohua
图片保存
在图片保存过程中"\"需要进行转义。
>>> import requests
>>> r = requests.get("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508693697147&di=23eb655d8e450f84cf39453bc1029bc0&imgtype=0&src=http%3A%2F%2Fb.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fc9fcc3cec3fdfc038b027f7bde3f8794a5c226fe.jpg")
>>> open("E:\xiaohua_img\01.jpg",'wb').write(r.content)
File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode by
>>> open("E:\\xiaohua_img\1.jpg",'wb').write(r.content)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument: 'E:\\xiaohua_img\x01.jpg'
>>> open("E:\\xiaohua_img\\1.jpg",'wb').write(r.content)
34342
Scrapy爬虫实例——校花网的更多相关文章
- Python 爬虫 爬校花网!!
爬虫:是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本 1.福利来了 校花网 ,首先说为什么要爬这个网站呢,第一这个网站简单爬起来容易不会受到打击,第二呢 你懂得... 1.第一步,需要下载 ...
- scrapy爬取校花网男神图片保存到本地
爬虫四部曲,本人按自己的步骤来写,可能有很多漏洞,望各位大神指点指点 1.创建项目 scrapy startproject xiaohuawang scrapy.cfg: 项目的配置文件xiaohua ...
- 爬虫下载校花网美女信息-lxml
# coding=utf-8 # !/usr/bin/env python ''' author: dangxusheng desc : 下载校花网上的个人信息:名字-学校-图片地址-点赞数 date ...
- python爬虫基础应用----爬取校花网视频
一.爬虫简单介绍 爬虫是什么? 爬虫是首先使用模拟浏览器访问网站获取数据,然后通过解析过滤获得有价值的信息,最后保存到到自己库中的程序. 爬虫程序包括哪些模块? python中的爬虫程序主要包括,re ...
- 爬虫(猫眼电影+校花网+github+今日头条+拉钩)
Requests+正则表达式爬取猫眼TOP100榜电影信息 MARK:将信息写入文件解决乱码方法,开启进程池秒爬. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- Python之爬虫-校花网
Python之爬虫-校花网 #!/usr/bin/env python # -*- coding:utf-8 -*- import re import requests # 拿到校花网主页的内容 re ...
- Python 爬虫 校花网
爬虫:是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本. 福利来了 校花网 ,首先说为什么要爬这个网站呢,第一这个网站简单爬起来容易,不会受到打击,第二呢 你懂得.... 1.第一步,需要下 ...
- Python-爬取校花网视频(单线程和多线程版本)
一.参考文章 python爬虫爬取校花网视频,单线程爬取 爬虫----爬取校花网视频,包含多线程版本 上述两篇文章都是对校花网视频的爬取,由于时间相隔很久了,校花网上的一些视频已经不存在了,因此上述文 ...
- day1之校花网小试牛刀
一 利用生成器来完成爬去校花网视频 import requests import re import os import hashlib import time DOWLOAD_PATH=r'D:\D ...
随机推荐
- 【S2-052】漏洞复现(CVE-2017-9805)
一.漏洞描述 Struts2 的REST插件,如果带有XStream组件,那么在进行反序列化XML请求时,存在未对数据内容进行有效验证的安全隐患,可能发生远程命令执行. 二.受影响版本 Struts2 ...
- VMware下Linux网络配置局域网和外网访问
要使用Linux系统很重要的一个操作就是使Linux系统能够访问互联网,只有Linux系统能够访问互联网才能够去下载很多自己所需要的资源,如果不能访问互联网那么使用Linux系统往往会卡在这一步,假设 ...
- Python 日志处理(三) 日志状态码分析、浏览器分析
在企业中,从日志中提取数据进行分析,可以帮助企业更加了解用户行为,用户最感兴趣的产品或者内容,分析得到数据后,可以决定企业在今后的走向. 从这些日志数据中,比较重要的有: 1. 用户访问最多的url, ...
- mybatis 一对一关联映射实例
在实际项目开发中,经常存在一对一的关系,如一个人对应一张身份证信息,这就是一对一的关系.下面是一个简单的实例: 1.建表过程我就省略了,主要是一张Person表,一张IDCard表,其相关属性见步骤2 ...
- Android 7.0 中 ContentProvider 实现原理
欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:汪毅雄 导语: 本文描述了ContentProvider发布者和调用者这两在Framework层是如何实现的. 作为Android的四大 ...
- PHP 使用redis实现秒杀
PHP 使用redis实现秒杀 使用redis队列,因为pop操作是原子的,即使有很多用户同时到达,也是依次执行,推荐使用(mysql事务在高并发下性能下降很厉害,文件锁的方式也是) 先将商品库存如队 ...
- 微信公众平台创建自定义菜单的PHP代码
授人以鱼不如授人以渔.在方倍工作室上问了一下,创建自定义菜单的代码多少钱,一张口就一百,好吧,那我就给你们一人省一百块钱吧,你们说该如何谢谢我?事先说明一下啊,你的PHP版本要高于4.0.2才支持cU ...
- Git命令汇总(补充篇)
上一篇<Git命令汇总基础篇>总结了使用Git的基本命令,这一篇作为补充主要给大家讲一些平时使用中的技巧和总结 . 学会了这些命令,已经基本解决了使用Git中大部分问题. 1.gitign ...
- QT的radioButton组的使用
在使用Qt的radioButton控件时,会产生一个疑问,如何让你选择的那个radio得到一个数据,进行判断,网上的一些资料有些不全,容易出错. 所以你得做件事,给每个radioButton进行赋初值 ...
- Hbuilder app开发,使用mui.ajax和服务器交互,后台获取不到值,显示null的解决方法
先上一个能用的js代码: function login() { var uname=document.getElementById("username").value.trim() ...