scrapy框架爬取小说的正文内容(Scrapy框架爬取Boss直聘网Python职位信息的源码)
类别:脚本大全 浏览量:248
时间:2022-01-25 00:05:34 scrapy框架爬取小说的正文内容
Scrapy框架爬取Boss直聘网Python职位信息的源码分析
使用crawlspider结合linkextractor和rule爬取网页信息
linkextractor用于定义链接提取规则,一般使用allow参数即可
|
linkextractor(allow = (), # 使用正则定义提取规则 deny = (), # 排除规则 allow_domains = (), # 限定域名范围 deny_domains = (), # 排除域名范围 restrict_xpaths = (), # 使用xpath定义提取队则 tags = ( 'a' , 'area' ), attrs = ( 'href' ,), canonicalize = false, unique = true, process_value = none, deny_extensions = none, restrict_css = (), # 使用css选择器定义提取规则 strip = true): |
rule用于定义crawlspider的爬取规则,由spider内部自动识别,提交请求、获取响应,交给callback指定的回调方法处理response
如果指定了callback,参数follow默认为false;如果callback为none,follow默认为true
|
rule(link_extractor, # linkextractor对象,必选参数 callback = none, # 回调方法,可选 cb_kwargs = none, follow = none, # 是否进行深度爬取,true、false process_links = none, # 用于处理链接(有些反爬策略是返回假的url) process_request = identity) |
源码
items.py
|
class bosszhipinitem(scrapy.item): """boss直聘pytho职位爬虫item""" # 职位名称 position = scrapy.field() # 公司名称 company = scrapy.field() # 薪资 salary = scrapy.field() # 工作地点 location = scrapy.field() # 学历要求 education = scrapy.field() # 工作时间 year = scrapy.field() |
spiders/bosszhipin_spider.py
|
# !/usr/bin/env python # -*- coding:utf-8 -*- import scrapy from scrapy.spider import crawlspider,rule from scrapy.linkextractors import linkextractor from myscrapy.items import bosszhipinitem class bosszhipinspider(crawlspider): """ boss直聘python职位爬虫spider 使用crawlspider基类实现 """ name = 'bosszhipin' allowed_domains = [ 'zhipin.com' ,] start_urls = [ 'http://www.zhipin.com/c100010000/h_100010000/?query=python&page=1' ,] # 链接提取器对象(规定链接提取规则) link_extractor = linkextractor(allow = (r 'page=\d+' )) # 链接提取规则对象列表 # 自动调用callback指定的方法,去取爬取由link_extractor指定的链接提取规则匹配到的url # 原理:link_extractor.extract_links(response)返回匹配到的链接 rules = [ rule(link_extractor = link_extractor,callback = 'parse_page' ,follow = true), ] def parse_page( self ,response): """定义回调方法,用于解析每个response对象""" job_list = response.xpath( '//li[@class="job-list"]//li' ) for job in job_list: position = job.xpath( './/li[@class="info-primary"]//h3[@class="name"]/a/text()' )[ 0 ].extract() salary = job.xpath( './/li[@class="info-primary"]//h3[@class="name"]//span/text()' )[ 0 ].extract() company = job.xpath( './/li[@class="company-text"]//a/text()' )[ 0 ].extract() location = job.xpath( './/li[@class="info-primary"]/p/text()[1]' )[ 0 ].extract() year = job.xpath( './/li[@class="info-primary"]/p/text()[2]' )[ 0 ].extract() education = job.xpath( './/li[@class="info-primary"]/p/text()[3]' )[ 0 ].extract() item = bosszhipinitem() item[ 'position' ] = position item[ 'salary' ] = salary item[ 'company' ] = company item[ 'location' ] = location item[ 'year' ] = year item[ 'education' ] = education yield item |
pipelines.py
|
class bosszhipinpipeline( object ): """boss直聘python职位爬虫item pipeline""" def __init__( self ): self .f = open ( 'data/bosszhipin.json' ,mode = 'wb' ) self .f.write(b '[' ) def process_item( self ,item,spider): data = json.dumps( dict (item),ensure_ascii = false,indent = 4 ) self .f.write(data.encode( 'utf-8' )) self .f.write(b ',' ) return item def close_spider( self ,spider): self .f.write(b ']' ) self .f.close() |
settings.py
|
item_pipelines = { 'myscrapy.pipelines.bosszhipinpipeline' : 1 , } |
运行结果
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对开心学习网的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/topleeyap/article/details/78907149
您可能感兴趣
- python编程ai人工智能(AI领域都在用Python即将被淘汰?网友预测未来的编程语言不会是TA)
- python 的常用工具(Python静态类型检查新工具之pyright 使用指南)
- python怎么导入函数pandas(python将pandas datarame保存为txt文件的实例)
- python中怎么连接mysql(python远程连接MySQL数据库)
- python数据分析删除重复值(Python3实现从排序数组中删除重复项算法分析)
- python中的多线程详解(python多线程抽象编程模型详解)
- pythonsocket建立多用户通讯(Python socket实现多对多全双工通信的方法)
- python2.7连接sqlserver数据库(Python使用sqlalchemy模块连接数据库操作示例)
- pythonpandas提取列索引(Python3.5 Pandas模块缺失值处理和层次索引实例详解)
- docker镜像内安装python包(如何使用Docker搭建pypi私有仓库)
- python strip用法(Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析)
- python3的循环怎么用(对Python3 goto 语句的使用方法详解)
- python怎么抽取微信图片(Python 一键制作微信好友图片墙的方法)
- python抽奖代码教程(python实现抽奖小程序)
- python爬虫怎么设置代理ip(python爬虫简单的添加代理进行访问的实现代码)
- python模块都是自己的语法(详解Python_shutil模块)
- 相声转行影帝,被何晴抛弃,甩10年女友闪婚生子,刘威不靠谱情史(相声转行影帝被何晴抛弃)
- 岳云鹏不说相声,改行演员了 网友 快回来说相声(岳云鹏不说相声)
- 乔欣首演古装大女主,颜值演技双在线(乔欣首演古装大女主)
- 于正又推女性古装大剧 杨蓉乔欣演女配,两位女主成 重头戏(于正又推女性古装大剧)
- 乔欣古装女主戏获热度 作为女主,却没吃到红利(乔欣古装女主戏获热度)
- 爱情是什么(爱情是什么最经典的话)
热门推荐
- 真实的codeigniter错误(Codeigniter里的无刷新上传的实现代码)
- dedecms如何重新安装(DEDECMS网站安全 教您更改data目录名称)
- sql server日志备份查看(sql server数据库高可用日志传送的方法)
- 云服务器搭建网站怎么选择合适(访问量过万的网站需要多大云服务器?)
- flashfxp如何设置中文(flashfxp怎么用?flashfxp使用方法)
- nodejs实现websocket服务端(Node.js+express+socket实现在线实时多人聊天室)
- vmware配置桥接(VMware虚拟机桥接方式实现上网互通的方法)
- elasticsearch启动报错(解决Docker启动Elasticsearch7.x报错的问题)
- ASP.NET全角与半角相互转换
- mongodb基本数据类型
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9