CrawlSpider下的腾讯招聘网站内容爬取

1.首先是scrapy项目的建立:

scrapy startproject TencentSpider

 

2.打开项目文件,在对应的spider文件夹内输入:

scrapy genspider -t Crawl tencent tencent.com

进行tencent.py爬虫文件的建立。

3.爬虫程序的设置

4.tencent.py程序的编写:

源代码如下:

# -*- coding: utf-8 -*-
import scrapy
#导入链接规则匹配类,用来提取符合规则的链接
from scrapy.linkextractors import LinkExtractor
#导入Crawlspider类和Rule
from scrapy.spiders import CrawlSpider, Rule
from TencentSpider.items import TencentspiderItem

class TencentSpider(CrawlSpider):
    name = 'tencent'
    allowed_domains = ['hr.tencent.com']          #确定爬取域
    start_urls = ['http://hr.tencent.com/position.php?&start=0#a']     #初始url
    #获取列表里的链接,依次发送请求,并且继续跟进,并调用回退函数处理
    rules = (
        Rule(LinkExtractor(allow=r'start=\d+'), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        # i = {}
        #i['domain_id'] = response.xpath('//input[@id="sid"]/@value').extract()
        #i['name'] = response.xpath('//div[@id="name"]').extract()
        #i['description'] = response.xpath('//div[@id="description"]').extract()
        # return i
        for each in response.xpath("//tr[@class='even'] | //tr[@class='odd']"):
            item = TencentspiderItem()
            # 职位名称
            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]
            r = each.xpath("./td[2]/text()").extract()
            item['positionType'] = r[0] if r else None
            # 招聘人数
            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

对应的设置文件和scrapy的爬虫项目基本一样,在此省略。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值