Scrapy 框架 CrawlSpider 全站数据爬取
2024-08-26 11:42:04
CrawlSpider 全站数据爬取
创建 crawlSpider 爬虫文件
scrapy genspider -t crawl chouti www.xxx.com
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule class CrawSpider(CrawlSpider):
name = 'craw'
# allowed_domains = ['www.xxx.com']
start_urls = ['https://dig.***.com/r/scoff/hot/1']
#连接提取器:可以根据指定条件提取连接
link = LinkExtractor(allow=r'/r/scoff/hot/\d+')
# link1 = LinkExtractor(allow=r'/pic/$') 针对于第一页的 url 不同的 页面使用 rules = (
#规则解析器:将连接提取器提取到的连接对应的页面进行指定规则的数据解析
Rule(link, callback='parse_item', follow=True),
#参数follow=True:将连接提取器继续作用到连接提取器提取到的连接所有对应的页面中
# Rule(link1, callback='parse_item', follow=False),
) def parse_item(self, response):
print(response)
对于简介与详情不是一个 item 的存储
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule from tenPro.items import TenproItem, TenproItem_detail class TenSpider(CrawlSpider):
name = 'ten'
# allowed_domains = ['www.ccc.com']
start_urls = ['https://hr.****.com/position.php?&start=#a0']
rules = (
Rule(LinkExtractor(allow=r'&start=\d+#a'), callback='parse_item', follow=True),
Rule(LinkExtractor(allow=r'position_detail.php\?id ='), callback='parse_detail', follow=True),
) def parse_item(self, response):
# 岗位名称和类别
tr_list = response.xpath(
'//table[@class="tablelist"]/tr[@class="odd"] | //table[@class="tablelist"]/tr[@class="even"]')
for tr in tr_list:
title = tr.xpath('./td[1]/a/text()').extract_first()
kind = tr.xpath('./td[2]/text()').extract_first()
item = TenproItem()
item['title'] = title
item['kind'] = kind
yield item def parse_detail(self, response):
desc = response.xpath('//ul[@class="squareli"]//text()').extract()
desc = ''.join(desc)
item = TenproItem_detail()
item['desc'] = desc yield itemimport scrapy class TenproItem(scrapy.Item):
# define the fields for your item here like:
title = scrapy.Field()
kind = scrapy.Field()
# pass
class TenproItem_detail(scrapy.Item):
desc = scrapy.Field()
# 分别进行存储 利用数据库的 多表联查 或数据解析
class TenproPipeline(object):
def process_item(self, item, spider):
desc = None
if item.__class__.__name__ == 'TenproItem_detail':
desc = item['desc']
else:
title = item['title']
kind = item['kind']
print(item)
return item
思路:
基于手动请求发送的形式:对所有页面表示的url发起请求,获取页面数据,进行解析
基于CrawlSpider的形式:使用链接提取器和规则解析器进行所有页面对应页面数据的获取也指定数据的解析
最新文章
- CICS的几个常用命令
- T 泛型转换
- MYSQL性能查看(命中率,慢查询)
- BUG修改纪录
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
- jQuery中DOM操作
- hdu 4119 Isabella's Message
- WCF-001:WCF的发布
- [Swift-2019力扣杯春季决赛]3. 最长重复子串
- <;Numerical Analysis>;(by Timothy Sauer) Notes
- Testing - 软件测试知识梳理 - 基础概念
- Python虚拟环境和包管理工具Pipenv的使用详解--看完这一篇就够了
- gentoo: startx: drmsetmaster failed: permission denied
- android展示注册进度效果源码
- Spring源码解析二:IOC容器初始化过程详解
- Nginx遇上Access Denied提示怎么解决
- docker 集群 笔记
- python算法之选择排序
- php ul li 分类
- lua-遍历集合-ipairs和pairs的区别
热门文章
- Wpf学习20180605
- 对比JavaScript中的Continue和Break
- 如何将字符串格式的对象转换成真正的js对象?
- springboot 开发 Tars
- 开发Spring过程中几个常见异常(二):Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;a&#39; define
- Html富文本编辑器
- C# 利用Log4Net进行日志记录
- Android Studio 无法预览xml布局视图:failed to load AppCompat ActionBar with unkNown error
- svn基本常见操作设置
- CSS回顾(基础知识,元素,选择器,盒子,颜色)