今天小編就爲大家分享一篇關於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('//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,
}
運行結果
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值