学习笔记(42):21天通关Python(仅视频课)-Scrapy开发爬虫的步骤(上)

立即学习:https://edu.csdn.net/course/play/24797/282223?utm_source=blogtoedu

生成一个蜘蛛:

scrapy genspider job_position 'zhipin.com'

job_position 创建的类名称

zhipin.com 爬取的域名

 

# -*- coding: utf-8 -*-
import scrapy
from ZhipinSpider.items import ZhipinspiderItem


# view-source:https://www.zhipin.com/job_detail/?query=&city=101120200&industry=&position=110101

class TestScrapySpider(scrapy.Spider):
    # 蜘蛛的名字
    name = 'test_scrapy'
    # 定义蜘蛛只爬取哪写域名
    allowed_domains = ['zhipin.com']
    # 从哪个页面开始爬
    start_urls = ['https://www.zhipin.com/job_detail/?query=&city=101120200&industry=&position=110101']

    # 该response就代表Scrapy下载器所获取的目标响应
    def parse(self, response):
        # 每个job_primary元素包含一个工作信息
        for job_primary in response.xpath('//div[@class="job-primary"]'):
            item = ZhipinspiderItem()
            # 获取工作信息的内容DIV
            info_primary = job_primary.xpath('./div[@class="info-primary"]')
            work_primary = info_primary.xpath('./div[@class="primary-wrapper"]/div [@class="primary-box"]')
            # 工作名称
            item['title'] = work_primary.xpath('./div[@class="job-title"]/span[@class="job-name"]/a/text()').extract()[
                0]
            # # 工资
            item['salary'] = work_primary.xpath(
                './div[@class="job-limit clearfix"]/span[@class="red"]/text()').extract_first()
            # # 工作的连接
            item['url'] = work_primary.xpath('./div[@class="job-title"]/span[@class="job-name"]/a/@href').extract()[0]
            # # 工作地点
            item['work_addr'] = work_primary.xpath(
                './div[@class="job-title"]/span[@class="job-area-wrapper"]/span[@class="job-area"]/text()').extract_first()

            # # 招聘公司
            company_primary = job_primary.xpath('./div[@class="info-company"]/div[@class="company-text"]')
            item['company'] = company_primary.xpath('./h3/a/text()').extract_first()
            company_info=company_primary.xpath('./p/text()').extract_first()
            if company_info and len(company_info)>0:
                item['industry']=company_info[0]
            if company_info and len(company_info)>1:
                item['company_size'] =company_info[2]
            # # 行业
            item['industry'] = company_primary.xpath('./p/a/text()')
            # # 公司规模
            # item['company_size'] = scrapy.Field()
            # # 招聘人
            # item['recruiter'] = scrapy.Field()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hello-Rock

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值