使用模板爬拉勾网数据

# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from day12.items import LagouItem

class LagouSpider(CrawlSpider):
    name = 'lagou'
    allowed_domains = ['lagou.com']
    start_urls = ['http://lagou.com/']

    rules = (
        # linkExtractor 是需要过滤出来的url的正则表达式
        # 过滤好的正则表达式会调用callback解析页面的过程
        # follow=True 就表示需要获取这个页面内的链接
        #        False 不再当前页面寻找url继续处理

        Rule(LinkExtractor(allow=r'https://www\.lagou\.com/zhaopin/\w+/'),follow = True),
        Rule(LinkExtractor(allow=r'https://www\.lagou\.com/jobs/\d+.html'), callback='parse_item', follow=False),
    )

    def parse_item(self, response):
        print('当前为列表页')
        # 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

    def parse_detail(self,response):
        print('当前为详情页')
        title = response.xpath('//span[@class="name"]/text()').extract_first()
        print(title)
        salary = response.xpath('//span[@class="salary"]/text()').eatract_first()
        print(salary)
        company = response.xpath('//div[@class="company"]/text()').extract_first()
        print(company)
        advantage = response.xpath('//dd[@class="job-advantage"]//text()').extract()
        advantage = ''.join(advantage)
        print(advantage)
        job_bt = response.xpath('//dd[class="job_bt"]//text()').extract()
        job_bt = ''.join(job_bt)
        print(job_bt)

        item = LagouItem()
        item['title'] = title
        item['salary'] = salary
        item['company'] = company
        item['advantage'] = advantage
        item['job_bt'] = job_bt
        return item








 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值