spider爬虫,适合meta传参的爬虫(列表页,详情页都有数据要爬取的时候)

crawlspider爬虫,适合不用meta传参的爬虫

scrapy genspider -t crawl it it.com

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from Sun.items import SunItem class DongguanSpider(CrawlSpider):
name = 'dongguan'
# 修改允许的域
allowed_domains = ['sun0769.com']
# 修改起始的url
start_urls = ['http://wz.sun0769.com/index.php/question/questionType?type=4'] rules = (
# 构建列表url的提取规则
Rule(LinkExtractor(allow=r'questionType'), follow=True),
# 构建详情页面url提取规则
# 'html/question/201711/352271.shtml'
Rule(LinkExtractor(allow=r'html/question/\d+/\d+.shtml'), callback='parse_item'),
) def parse_item(self, response):
# print (response.url,'--------') # 构建item实例
item = SunItem() # 抽取数据,将数据存放到item中
item['number'] = response.xpath('/html/body/div[6]/div/div[1]/div[1]/strong/text()').extract()[0].split(':')[-1].strip()
item['title'] = response.xpath('/html/body/div[6]/div/div[1]/div[1]/strong/text()').extract()[0].split(':')[-1].split(' ')[0]
item['link'] = response.url
data = ''.join(response.xpath('//div[@class="c1 text14_2"]/text()|//div[@class="contentext"]/text()').extract())
item['content'] = data.replace('\xa0','')
# print(item)
# 返回数据
yield item

  

链接提取器的使用

scrapy shell http://hr.tencent.com/position.php

>>> from scrapy.linkextractors import LinkExtractor
>>> le = LinkExtractor(allow=('position_detail.php\?id=\d+&keywords=&tid=0&lid=0'))

或者直接 le = LinkExtractor(allow=('position_detail.php'))   也可以

>>> links=le.extract_links(response)
>>> for link in links:
... print(link)
...

>>> for link in links:
... print(link.url)
...

crawlspider爬虫:定义url规则的更多相关文章

  1. scrapy进阶(CrawlSpider爬虫__爬取整站小说)

    # -*- coding: utf-8 -*- import scrapy,re from scrapy.linkextractors import LinkExtractor from scrapy ...

  2. Django学习(四) Django提供的后台管理系统以及如何定义URL路由

    一旦你建立了模型Models,那么Django就可以为你创建一个专业的,可以提供给生成用的后台管理站点.这个站点可以提供给有权限的人进行已有模型Models数据的增删改查. 将新建的模型Models是 ...

  3. python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。

    本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...

  4. scrapy 中crawlspider 爬虫

    爬取目标网站: http://www.chinanews.com/rss/rss_2.html 获取url后进入另一个页面进行数据提取 检查网页: 爬虫该页数据的逻辑: Crawlspider爬虫类: ...

  5. 《玩转Django2.0》读书笔记-编写URL规则

    <玩转Django2.0>读书笔记-编写URL规则 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. URL(Uniform Resource Locator,统一资源定位 ...

  6. Scrapy - CrawlSpider爬虫

    crawlSpider 爬虫 思路: 从response中提取满足某个条件的url地址,发送给引擎,同时能够指定callback函数. 1. 创建项目 scrapy startproject mysp ...

  7. 创建CrawlSpider爬虫简要步骤

    创建CrawlSpider爬虫简要步骤: 1. 创建项目文件: e.g: scrapy startproject douyu (douyu为项目名自定义) 2. 进入项目文件: e.g: cd dou ...

  8. dt框架自定义url规则

    destoon的列表的地址规则是定义在/api/url.inc.php,然后又是在include/global.func.php中进行的listpages这个函数调用实现 if($page < ...

  9. PHPCMS V9静态化HTML生成设置及URL规则优化

    先讲讲Phpcms V9在后台怎么设置生成静态化HTML,之后再讲解怎么自定义URL规则,进行URL地址优化.在这一篇中,伪静态就不涉及了,大家可以移步到Phpcms V9全站伪静态设置方法. 一.静 ...

随机推荐

  1. C语言中如何计算时间差

    #include <time.h>   #include <stdio.h>   int main()   {       time_t start ,end ;        ...

  2. 【SpringBoot整合Elasticsearch】SpringBoot整合ElasticSearch

    一.Linux下安装ElasticSearch 1.检测是否安装了Elasticsearch ps aux |grep elasticsearch 2.安装JDK 3.下载Elasticsearch ...

  3. 使用import取代require

    首先,Module 语法是 JavaScript 模块的标准写法,坚持使用这种写法.使用import取代require. // bad const moduleA = require('moduleA ...

  4. MySQL优化之SQL耗时瓶颈 SHOW profiles

    1.首先查看是否开启profiling功能 SHOW VARIABLES LIKE '%pro%'; 或者 SELECT @@profiling; 2.开启profiling SET profilin ...

  5. shell中的环境变量:local,global,export

     1.local一般用于局部变量声明,多在在函数内部使用.实例如下:      echo_start() { local STR="$1" echo "...... ${ ...

  6. 状态机FSM

    参考: 百度-有限状态机 博客园-有限状态机FSM详解及其实现 CSDN-状态机FSM代码框架 腾讯开源项目behaviac 占坑,待编辑...

  7. 经典把妹桥段:Flower dance开头对话

    听到一首很赞的钢琴曲,Flower Dance,其开头有一段英文对话,如下: Lucy:"They serve the purpose of changing hydrogen into b ...

  8. thinkphp---用事务处理批量操作

    我们在进行一些业务逻辑的时候,难免会出现批量操作的问题,特别是批量修改操作,如果数据量大,总会考虑到批量修改到一半怎么办?所以如果使用事务来进行批量操作就会好很多,直接看代码: public func ...

  9. Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)

    通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...

  10. 50.TO_NUMBER 将给出的字符转换为数字

    .SYSDATE 用来得到系统的当前日期 SQL> select to_char(sysdate,dd-mm-yyyy day) from dual; TO_CHAR(SYSDATE, ---- ...