Scrapy框架爬取Boss直聘網(wǎng)Python職位信息的源碼
分析
使用CrawlSpider結(jié)合LinkExtractor和Rule爬取網(wǎng)頁信息
LinkExtractor用于定義鏈接提取規(guī)則,一般使用allow參數(shù)即可
LinkExtractor(allow=(), # 使用正則定義提取規(guī)則
deny=(), # 排除規(guī)則
allow_domains=(), # 限定域名范圍
deny_domains=(), # 排除域名范圍
restrict_xpaths=(), # 使用xpath定義提取隊(duì)則
tags=('a', 'area'),
attrs=('href',),
canonicalize=False,
unique=True,
process_value=None,
deny_extensions=None,
restrict_css=(), # 使用css選擇器定義提取規(guī)則
strip=True):
Rule用于定義CrawlSpider的爬取規(guī)則,由Spider內(nèi)部自動(dòng)識(shí)別,提交請求、獲取響應(yīng),交給callback指定的回調(diào)方法處理response
如果指定了callback,參數(shù)follow默認(rèn)為False;如果callback為None,follow默認(rèn)為True
Rule(link_extractor, # LinkExtractor對象,必選參數(shù) callback=None, # 回調(diào)方法,可選 cb_kwargs=None, follow=None, # 是否進(jìn)行深度爬取,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() # 工作地點(diǎn) location=scrapy.Field() # 學(xué)歷要求 education=scrapy.Field() # 工作時(shí)間 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基類實(shí)現(xiàn)
"""
name = 'bosszhipin'
allowed_domains=['zhipin.com',]
start_urls=['http://www.zhipin.com/c100010000/h_100010000/?query=Python&page=1',]
# 鏈接提取器對象(規(guī)定鏈接提取規(guī)則)
link_extractor=LinkExtractor(allow=(r'page=\d+'))
# 鏈接提取規(guī)則對象列表
# 自動(dòng)調(diào)用callback指定的方法,去取爬取由link_extractor指定的鏈接提取規(guī)則匹配到的url
# 原理:link_extractor.extract_links(response)返回匹配到的鏈接
rules = [
Rule(link_extractor=link_extractor,callback='parse_page',follow=True),
]
def parse_page(self,response):
"""定義回調(diào)方法,用于解析每個(gè)response對象"""
job_list=response.xpath('//div[@class="job-list"]//li')
for job in job_list:
position = job.xpath('.//div[@class="info-primary"]//h3[@class="name"]/a/text()')[0].extract()
salary =job.xpath('.//div[@class="info-primary"]//h3[@class="name"]//span/text()')[0].extract()
company =job.xpath('.//div[@class="company-text"]//a/text()')[0].extract()
location =job.xpath('.//div[@class="info-primary"]/p/text()[1]')[0].extract()
year =job.xpath('.//div[@class="info-primary"]/p/text()[2]')[0].extract()
education =job.xpath('.//div[@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,
}
運(yùn)行結(jié)果

總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- Python爬蟲Scrapy框架CrawlSpider原理及使用案例
- 簡述python Scrapy框架
- 詳解Python的爬蟲框架 Scrapy
- Python爬蟲實(shí)例——scrapy框架爬取拉勾網(wǎng)招聘信息
- Python Scrapy框架:通用爬蟲之CrawlSpider用法簡單示例
- Python Scrapy框架第一個(gè)入門程序示例
- python3 Scrapy爬蟲框架ip代理配置的方法
- Python利用Scrapy框架爬取豆瓣電影示例
- 詳解Python網(wǎng)絡(luò)框架Django和Scrapy安裝指南
- Python3爬蟲爬取英雄聯(lián)盟高清桌面壁紙功能示例【基于Scrapy框架】
- python Scrapy框架原理解析
相關(guān)文章
CentOS安裝pillow報(bào)錯(cuò)的解決方法
本文給大家分享的是作者在centos下為Python安裝pillow的時(shí)候報(bào)錯(cuò)的解決方法,希望對大家能夠有所幫助。2016-01-01
在pytorch中實(shí)現(xiàn)只讓指定變量向后傳播梯度
今天小編就為大家分享一篇在pytorch中實(shí)現(xiàn)只讓指定變量向后傳播梯度,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02

