Python爬虫框架Scrapy实例代码

yipeiwu_com5年前Python爬虫

目标任务:爬取腾讯社招信息,需要爬取的内容为:职位名称,职位的详情链接,职位类别,招聘人数,工作地点,发布时间。

一、创建Scrapy项目

scrapy startproject Tencent

命令执行后,会创建一个Tencent文件夹,结构如下

二、编写item文件,根据需要爬取的内容定义爬取字段

# -*- coding: utf-8 -*-
import scrapy
class TencentItem(scrapy.Item):
  # 职位名
  positionname = scrapy.Field()
  # 详情连接
  positionlink = scrapy.Field()
  # 职位类别
  positionType = scrapy.Field()
  # 招聘人数
  peopleNum = scrapy.Field()
  # 工作地点
  workLocation = scrapy.Field()
  # 发布时间
  publishTime = scrapy.Field()

三、编写spider文件

进入Tencent目录,使用命令创建一个基础爬虫类:

# tencentPostion为爬虫名,tencent.com为爬虫作用范围
scrapy genspider tencentPostion "tencent.com"

执行命令后会在spiders文件夹中创建一个tencentPostion.py的文件,现在开始对其编写:

# -*- coding: utf-8 -*-
import scrapy
from tencent.items import TencentItem
class TencentpositionSpider(scrapy.Spider):
  """
  功能:爬取腾讯社招信息
  """
  # 爬虫名
  name = "tencentPosition"
  # 爬虫作用范围
  allowed_domains = ["tencent.com"]
  url = "http://hr.tencent.com/position.php?&start="
  offset = 0
  # 起始url
  start_urls = [url + str(offset)]
  def parse(self, response):
    for each in response.xpath("//tr[@class='even'] | //tr[@class='odd']"):
      # 初始化模型对象
      item = TencentItem()
      # 职位名称
      item['positionname'] = each.xpath("./td[1]/a/text()").extract()[0]
      # 详情连接
      item['positionlink'] = each.xpath("./td[1]/a/@href").extract()[0]
      # 职位类别
      item['positionType'] = each.xpath("./td[2]/text()").extract()[0]
      # 招聘人数
      item['peopleNum'] = each.xpath("./td[3]/text()").extract()[0]
      # 工作地点
      item['workLocation'] = each.xpath("./td[4]/text()").extract()[0]
      # 发布时间
      item['publishTime'] = each.xpath("./td[5]/text()").extract()[0]
      yield item
    if self.offset < 1680:
      self.offset += 10
    # 每次处理完一页的数据之后,重新发送下一页页面请求
    # self.offset自增10,同时拼接为新的url,并调用回调函数self.parse处理Response
    yield scrapy.Request(self.url + str(self.offset), callback = self.parse)

四、编写pipelines文件

# -*- coding: utf-8 -*-
import json
class TencentPipeline(object):
  """ 
    功能:保存item数据 
  """
  def __init__(self):
    self.filename = open("tencent.json", "w")
  def process_item(self, item, spider):
    text = json.dumps(dict(item), ensure_ascii = False) + ",\n"
    self.filename.write(text.encode("utf-8"))
    return item
  def close_spider(self, spider):
    self.filename.close()

五、settings文件设置(主要设置内容)

# 设置请求头部,添加url
DEFAULT_REQUEST_HEADERS = {
  "User-Agent" : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;",
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
}
# 设置item——pipelines
ITEM_PIPELINES = {
  'tencent.pipelines.TencentPipeline': 300,
}

执行命令,运行程序

# tencentPosition为爬虫名
scrapy crwal tencentPosition

使用CrawlSpider类改写

# 创建项目
scrapy startproject TencentSpider
# 进入项目目录下,创建爬虫文件
scrapy genspider -t crawl tencent tencent.com
item等文件写法不变,主要是爬虫文件的编写
# -*- coding:utf-8 -*-
import scrapy
# 导入CrawlSpider类和Rule
from scrapy.spiders import CrawlSpider, Rule
# 导入链接规则匹配类,用来提取符合规则的连接
from scrapy.linkextractors import LinkExtractor
from TencentSpider.items import TencentItem
class TencentSpider(CrawlSpider):
  name = "tencent"
  allow_domains = ["hr.tencent.com"]
  start_urls = ["http://hr.tencent.com/position.php?&start=0#a"]
  # Response里链接的提取规则,返回的符合匹配规则的链接匹配对象的列表
  pagelink = LinkExtractor(allow=("start=\d+"))
  rules = [
    # 获取这个列表里的链接,依次发送请求,并且继续跟进,调用指定回调函数处理
    Rule(pagelink, callback = "parseTencent", follow = True)
  ]
  # 指定的回调函数
  def parseTencent(self, response):
    for each in response.xpath("//tr[@class='even'] | //tr[@class='odd']"):
      item = TencentItem()
      # 职位名称
      item['positionname'] = each.xpath("./td[1]/a/text()").extract()[0]
      # 详情连接
      item['positionlink'] = each.xpath("./td[1]/a/@href").extract()[0]
      # 职位类别
      item['positionType'] = each.xpath("./td[2]/text()").extract()[0]
      # 招聘人数
      item['peopleNum'] = each.xpath("./td[3]/text()").extract()[0]
      # 工作地点
      item['workLocation'] = each.xpath("./td[4]/text()").extract()[0]
      # 发布时间
      item['publishTime'] = each.xpath("./td[5]/text()").extract()[0]
      yield item

总结

以上所述是小编给大家介绍的Python爬虫框架Scrapy实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

Python爬虫抓取技术的一些经验

Python爬虫抓取技术的一些经验

前言 web是一个开放的平台,这也奠定了web从90年代初诞生直至今日将近30年来蓬勃的发展。然而,正所谓成也萧何败也萧何,开放的特性、搜索引擎以及简单易学的html、css技术使得we...

在scrapy中使用phantomJS实现异步爬取的方法

使用selenium能够非常方便的获取网页的ajax内容,并且能够模拟用户点击和输入文本等诸多操作,这在使用scrapy爬取网页的过程中非常有用。 网上将selenium集成到scrap...

Python3爬取英雄联盟英雄皮肤大图实例代码

Python3爬取英雄联盟英雄皮肤大图实例代码

爬虫思路 初步尝试 我先查看了network,并没有发现有可用的API;然后又用bs4去分析英雄列表页,但是请求到html里面,并没有英雄列表,在英雄列表的节点上,只有“正在加载中”这样...

详解Python网络爬虫功能的基本写法

网络爬虫,即Web Spider,是一个很形象的名字。把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛。 1. 网络爬虫的定义 网络蜘蛛是通过网页的链接地址来寻找网页的...

Python使用Beautiful Soup爬取豆瓣音乐排行榜过程解析

Python使用Beautiful Soup爬取豆瓣音乐排行榜过程解析

前言 要想学好爬虫,必须把基础打扎实,之前发布了两篇文章,分别是使用XPATH和requests爬取网页,今天的文章是学习Beautiful Soup并通过一个例子来实现如何使用Beau...