scrapy

scrapy基础

主宰稳场 提交于 2020-01-07 17:44:21
一、创建一个scrapy 项目 在开始爬取之前,我们首先要创建一个scrapy项目,在命令行输入一下命令即可创建。 scrapy startproject xxx 二、编写第一个scrapy蜘蛛 创建第一个scrapy蜘蛛文件 上面我们已经成功创建了一个scrapy 项目,在spiders目录下面,有一个scrapy 文档,下面来创造一只scrapy蜘蛛 文件名这里我就取名为:cjj_spider.py (保存在pachong/spiders目录下),已经成功创建了一个scrapy蜘蛛文件,我们要爬取哪个网站、爬取这个网站的神马数据,统统在这个文件里面编写。 编写第一个蜘蛛 首先介绍下scrapy遵守的规则: 1、首先我们需要创建一个类,并继承scrapy的一个子类:scrapy.Spider 或者是其他蜘蛛类型,除了Spider还有很多牛X的蜘蛛类型; 2、然后定义一个蜘蛛名,name=“” 后面我们运行的话需要用到; 3、定义我们需要爬取的网址,没有网址蜘蛛肿么爬,所以这是必须的 4、继承scrapy的一个方法:start_requests(self),这个方法的作用就是通过上面定义的链接去爬取页面,简单理解就是下载页面。 import scrapy class test(scrapy.Spider): #需要继承scrapy.Spider类 name = "pachong"

爬虫(十八):Scrapy框架(五) Scrapy通用爬虫

余生颓废 提交于 2020-01-07 15:59:10
1. Scrapy通用爬虫 通过Scrapy,我们可以轻松地完成一个站点爬虫的编写。但如果抓取的站点量非常大,比如爬取各大媒体的新闻信息,多个Spider则可能包含很多重复代码。 如果我们将各个站点的Spider的公共部分保留下来,不同的部分提取出来作为单独的配置,如爬取规则、页面解析方式等抽离出来做成一个配置文件,那么我们在新增一个爬虫的时候,只需要实现这些网站的爬取规则和提取规则即可。 这一章我们就来了解下Scrapy通用爬虫的实现方法。 1.1 CrawlSpider 在实现通用爬虫之前,我们需要先了解一下CrawlSpider,官方文档: https://scrapy.readthedocs.io/en/latest/topics/spiders.html#crawlspider 。 CrawlSpider是Scrapy提供的一个通用Spider。在Spider里,我们可以指定一些爬取规则来实现页面的提取,这些爬取规则由一个专门的数据结构Rule表示。Rule里包含提取和跟进页面的配置,Spider会根据Rule来确定当前页面中的哪些链接需要继续爬取、哪些页面的爬取结果需要用哪个方法解析等。 CrawlSpider继承自Spider类。除了Spider类的所有方法和属性,它还提供了一个非常重要的属性和方法。 rules,它是爬取规则属性,是包含一个或多个Rule对象的列表

一文了解你是否适合学习python?

扶醉桌前 提交于 2020-01-07 10:02:25
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 编程对于任何一个新手来说都不是一件容易的事情,特别是在中国基本以C语言作为启蒙语言的国家。Python对于任何一个想学习的编程的人来说的确是一个福音,阅读Python代码像是在阅读文章,源于Python语言提供了非常优雅的语法,被称为最优雅的语言之一。 如果你是属于以下这几点的人,强烈推荐你学习python! 编程菜鸟新手 非常喜爱编程,以后想从事相关工作,但是零基础,不知道入门选择什么编程语言的朋友,其实是最适合选择Python编程语言的。 网站前端的开发人员 平常只关注div+css这些页面技术,很多时候其实需要与后端开发人员进行交互的。 SEO人员 很多SEO优化的时候,苦于不会编程,一些程序上面的问题,得不到解决,只能做做简单的页面优化。 现在学会Python之后,你和我一样都可以编写一些查询收录,排名,自动生成网络地图的程序,解决棘手的SEO问题。 在校学生 想有一技之长,或者是自学编程的爱好者,希望快速入门,少走弯路,都可以选择Python语言。 Python学习可以分为几个阶段: 第一步:基础 很简单,只要搭建好环境,然后跟着这个网站敲一敲,熟悉一遍基础,不用花太多时间,大概1~2周。 重点学习:初级教程以及高级教程中的正则表达式、MySQL、多线程。 第二步:巩固 找简单的练手的项目

start_urls in Scrapy

若如初见. 提交于 2020-01-07 08:31:44
问题 I am trying to fetch some information from this website: http://www.go-on.fi/tyopaikat. As you can see, the table has a pagination, so whenever you click second or third page, the link will change too something http://www.go-on.fi/tyopaikat?start=20 (with the "start=" at the end). This is my spider code: allowed_domains = ["go-on.fi"] start_urls = ["http://www.go-on.fi/tyopaikat?start=0"] def parse(self, response): hxs = HtmlXPathSelector(response) items = [] titles = hxs.select("//tr") for

Scrapy image pipeline does not download images

那年仲夏 提交于 2020-01-07 06:28:20
问题 I'm trying to set up image downloading from web pages by using Scrapy Framework and djano-item. I think I have done everything like in doc but after calling scrapy crawl I log looking like this: Scrapy log I can't find there any information on what went wrong but Images field Is empty and directory does not contain any images. This is my model class Event(models.Model): title = models.CharField(max_length=100, blank=False) description = models.TextField(blank=True, null=True) event_location =

Get data from script tag with Scrapy Xpath

吃可爱长大的小学妹 提交于 2020-01-07 05:38:14
问题 I've been trying to extract data from script tag using Scrapy(xpath). My main issue is with identifying the correct div and script tags. I'm new to using xpath and would appreciate any help! <script> (function(e, a) { var t = { "cdn_url": "https://f.vimeocdn.com", "view": 1, "request": { "files": { "dash": { "separate_av": true, "streams": [{ "profile": 164, "quality": "360p", "id": 830872409, "fps": 30 }, { "profile": 175, "quality": "1080p", "id": 830872420, "fps": 30 }] "default_cdn":

SgmlLinkExtractor 'allow' definition not working with Scrapy

懵懂的女人 提交于 2020-01-07 05:09:18
问题 I am using Python.org version 2.7 64 bit on Windows Vista 64 bit. I have the following Scrapy code where the way I have defined SgmlLinkExtractor is not crawling the site correctly: from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor from scrapy.selector import Selector from scrapy.item import Item from scrapy.spider import BaseSpider from scrapy import log from scrapy.cmdline import execute from scrapy.utils.markup import

SgmlLinkExtractor 'allow' definition not working with Scrapy

余生长醉 提交于 2020-01-07 05:08:38
问题 I am using Python.org version 2.7 64 bit on Windows Vista 64 bit. I have the following Scrapy code where the way I have defined SgmlLinkExtractor is not crawling the site correctly: from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor from scrapy.selector import Selector from scrapy.item import Item from scrapy.spider import BaseSpider from scrapy import log from scrapy.cmdline import execute from scrapy.utils.markup import

Crawl and scrap a complete site with scrapy

南笙酒味 提交于 2020-01-07 04:24:17
问题 import scrapy from scrapy import Request #scrapy crawl jobs9 -o jobs9.csv -t csv class JobsSpider(scrapy.Spider): name = "jobs9" allowed_domains = ["vapedonia.com"] start_urls = ["https://www.vapedonia.com/7-principiantes-kit-s-de-inicio-", "https://www.vapedonia.com/10-cigarrillos-electronicos-", "https://www.vapedonia.com/11-mods-potencia-", "https://www.vapedonia.com/12-consumibles", "https://www.vapedonia.com/13-baterias", "https://www.vapedonia.com/23-e-liquidos", "https://www.vapedonia

Scrapy - How to load html string into open_in_browser function

六眼飞鱼酱① 提交于 2020-01-07 03:12:06
问题 I am working on some code which returns an HTML string ( my_html ). I want to see how this looks in a browser using https://doc.scrapy.org/en/latest/topics/debug.html#open-in-browser. To do this I've tried to create a response object with body set to ' my_html '. I've tried a bunch of things including: new_response = TextResponse(body=my_html) open_in_browser(new_response) based on the response class (https://doc.scrapy.org/en/latest/topics/request-response.html#response-objects). I'm getting