溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Scrapy框架爬取Boss直聘網(wǎng)Python職位信息的源碼

發(fā)布時(shí)間:2020-09-13 09:24:47 來源:腳本之家 閱讀:525 作者:topleeyap 欄目:開發(fā)技術(shù)

分析

使用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)部自動識別,提交請求、獲取響應(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ī)則對象列表
  # 自動調(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"]//h4[@class="name"]/a/text()')[0].extract()
      salary =job.xpath('.//div[@class="info-primary"]//h4[@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é)果

Scrapy框架爬取Boss直聘網(wǎng)Python職位信息的源碼

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對億速云的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI