scrapy

Scrapy -- Tutorial

大城市里の小女人 提交于 2020-01-02 13:23:00
1. 安装 # 会自动解决依赖. $ pip install scrapy 相关依赖的库介绍: lxml : XML 和 HTML 解析器. parsel : 基于 lxml 的 HTML/XML 数据提取器 w3lib : a multi-purpose helper for dealing with URLs and web page encodings. twisted : an asynchronous networking framework. cryptography and pyOpenSSL : to deal with various network-level security needs. 2. Tutorial 2.1. Creating a project $ scrapy startproject tutorial $ tree tutorial/ scrapy.cfg # deploy configuration file tutorial/ # project's Python module, you'll import your code from here __init__.py items.py # project items definition file pipelines.py # project pipelines file

4-Python爬虫框架-Scrapy

拥有回忆 提交于 2020-01-02 13:22:33
scrapy 爬虫框架 框架 爬虫框架 scrapy pyspider crawley scrapy框架介绍 https://doc.scrapy.org/en/latest/ http://scrapy-chs.readthedocs.io/zh_CN/latest/index.html 安装 利用pip scrapy概述 包含各个部件 ScrapyEngine: 神经中枢,大脑,核心、 Scheduler调度器:引擎发来的request请求,调度器需要处理,然后交换引擎 Downloader下载器:把引擎发来的requests发出请求,得到response Spider爬虫: 负责把下载器得到的网页/结果进行分解,分解成数据+链接 ItemPipeline管道: 详细处理Item DownloaderMiddleware下载中间件: 自定义下载的功能扩展组件 SpiderMiddleware爬虫中间件:对spider进行功能扩展 爬虫项目大概流程 新建项目:scrapy startproject xxx 明确需要目标/产出: 编写item.py 制作爬虫 : 地址 spider/xxspider.py 存储内容: pipelines.py, ItemPipeline 对应的是pipelines文件 爬虫提取出数据存入item后,item中保存的数据需要进一步处理,比如清洗,去重

Scrapy框架----- 入门案例

最后都变了- 提交于 2020-01-02 13:22:21
入门案例 学习目标 创建一个Scrapy项目 定义提取的结构化数据(Item) 编写爬取网站的 Spider 并提取出结构化数据(Item) 编写 Item Pipelines 来存储提取到的Item(即结构化数据) 一. 新建项目(scrapy startproject) 在开始爬取之前,必须创建一个新的Scrapy项目。进入自定义的项目目录中,运行下列命令: scrapy startproject mySpider 其中, mySpider 为项目名称,可以看到将会创建一个 mySpider 文件夹,目录结构大致如下: 下面来简单介绍一下各个主要文件的作用: scrapy.cfg :项目的配置文件 mySpider/ :项目的Python模块,将会从这里引用代码 mySpider/items.py :项目的目标文件 mySpider/pipelines.py :项目的管道文件 mySpider/settings.py :项目的设置文件 mySpider/spiders/ :存储爬虫代码目录 二、明确目标(mySpider/items.py) 我们打算抓取: http://www.itcast.cn/channel/teacher.shtml 网站里的所有讲师的姓名、职称和个人信息。 打开mySpider目录下的items.py Item 定义结构化数据字段,用来保存爬取到的数据

基于scrapy源码实现的自定义微型异步爬虫框架

谁说胖子不能爱 提交于 2020-01-02 13:19:14
一、scrapy原理 Scrapy 使用了 Twisted异步网络库来处理网络通讯。整体架构大致如下 Scrapy主要包括了以下组件: 引擎(Scrapy) 用来处理整个系统的数据流处理, 触发事务(框架核心) 调度器(Scheduler) 用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL(抓取网页的网址或者说是链接)的优先队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址 下载器(Downloader) 用于下载网页内容, 并将网页内容返回给蜘蛛(Scrapy下载器是建立在twisted这个高效的异步模型上的) 爬虫(Spiders) 爬虫是主要干活的, 用于从特定的网页中提取自己需要的信息, 即所谓的实体(Item)。用户也可以从中提取出链接,让Scrapy继续抓取下一个页面 项目管道(Pipeline) 负责处理爬虫从网页中抽取的实体,主要的功能是持久化实体、验证实体的有效性、清除不需要的信息。当页面被爬虫解析后,将被发送到项目管道,并经过几个特定的次序处理数据。 下载器中间件(Downloader Middlewares) 位于Scrapy引擎和下载器之间的框架,主要是处理Scrapy引擎与下载器之间的请求及响应。 爬虫中间件(Spider Middlewares) 介于Scrapy引擎和爬虫之间的框架

爬虫框架之Scrapy(四 ImagePipeline)

风格不统一 提交于 2020-01-02 13:18:17
ImagePipeline 使用scrapy框架我们除了要下载文本,还有可能需要下载图片,scrapy提供了ImagePipeline来进行图片的下载。 ImagePipeline还支持以下特别的功能: 1 生成缩略图:通过配置 IMAGES_THUMBS = {'size_name': (width_size,heigh_size),} 2 过滤小图片:通过配置 IMAGES_MIN_HEIGHT 和 IMAGES_MIN_WIDTH 来过滤过小的图片。 具体其他功能可以看下参考官网手册: https://docs.scrapy.org/en/latest/topics/media-pipeline.html . ImagePipelines的工作流程 1 在spider中爬取需要下载的图片链接,将其放入item中的image_urls. 2 spider将其传送到pipieline 3 当ImagePipeline处理时,它会检测是否有image_urls字段,如果有的话,会将url传递给scrapy调度器和下载器 4 下载完成后会将结果写入item的另一个字段images,images包含了图片的本地路径,图片校验,和图片的url。 示例 爬取巴士lol的英雄美图 只爬第一页 http://lol.tgbus.com/tu/yxmt/ 第一步:items.py import

PIP安装Python的scipy,scrapy等包出现“failed building wheel for xxx”问题解决办法

只谈情不闲聊 提交于 2020-01-02 11:53:39
本文转载自: https://www.cnblogs.com/harvey888/p/5467276.html 作者:harvey888 转载请注明该声明。 1.在这里下载对应的.whl文件,注意别改文件名! http://www. lfd.uci.edu/~gohlke/pyt honlibs/#lxml Ctrl + F,输入lxml,找到下面这段 Lxml, a binding for the libxml2 and libxslt libraries. lxml‑3.4.4‑cp27‑none‑win32.whl lxml‑3.4.4‑cp27‑none‑win_amd64.whl lxml‑3.4.4‑cp33‑none‑win32.whl lxml‑3.4.4‑cp33‑none‑win_amd64.whl lxml‑3.4.4‑cp34‑none‑win32.whl lxml‑3.4.4‑cp34‑none‑win_amd64.whl lxml‑3.4.4‑cp35‑none‑win32.whl lxml‑3.4.4‑cp35‑none‑win_amd64.whl cp后面是Python的版本号,27表示2.7,根据你的Python版本选择下载。 2.直接进入pip所在的目录\c:\python34\scripts 然后,把你要安装的whl文件都复制在这里啦。

Why scrapy crawler stops?

旧城冷巷雨未停 提交于 2020-01-02 10:18:49
问题 I have written a crawler using scrapy framework to parse a products site. The crawler stops in between suddenly without completing the full parsing process. I have researched a lot on this and most of the answers indicate that my crawler is being blocked by the website. Is there any mechanism by which I can detect whether my spider is being stopped by website or does it stop on its own? The below is info level log entry of spider . 2013-09-23 09:59:07+0000 [scrapy] INFO: Scrapy 0.18.0 started

Wait until the webpage loads in Scrapy

你说的曾经没有我的故事 提交于 2020-01-02 10:05:13
问题 I am using scrapy script to load URL using "yield". MyUrl = "www.example.com" request = Request(MyUrl, callback=self.mydetail) yield request def mydetail(self, response): item['Description'] = response.xpath(".//table[@class='list']//text()").extract() return item The URL seems to take minimum 5 seconds to load. So I want Scrapy to wait for some time to load the entire text in item['Description']. I tried "DOWNLOAD_DELAY" in settings.py but no use. 回答1: Make a brief view on firebug or another

How to add a third party Scrapy middleware

无人久伴 提交于 2020-01-02 08:56:33
问题 I'm working with scrapy 1.1 . I want to add scrapy-fake-user-agent Scrapy middleware that would rotate user agents seamlessly and randomly. User Agent strings are supplied by the fake-useragent module. following the directions from the site, I have: DOWNLOADER_MIDDLEWARES = { 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None, 'scrapy_fake_useragent.middleware.RandomUserAgentMiddleware': 400, } However when I run it I get: ImportError: No module named scrapy_fake_useragent

Avoid bad requests due to relative urls

淺唱寂寞╮ 提交于 2020-01-02 08:52:28
问题 I am trying to crawl a website using Scrapy, and the urls of every page I want to scrap are all written using a relative path of this kind: <!-- on page https://www.domain-name.com/en/somelist.html (no <base> in the <head>) --> <a href="../../en/item-to-scrap.html">Link</a> Now, in my browser, these links work, and you get to urls like https://www.domain-name.com/en/item-to-scrap.html (despite the relative path going back up twice in hierarchy instead of once) But my CrawlSpider does not