使用scrapy框架爬取51job的关于python的职位,并且进行分析

example.py 爬虫的主文件,大部分的爬虫逻辑都在这
# -*- coding: utf-8 -*-
import scrapy
# import pyecharts
from ..items import Scrapy3Item
class ExampleSpider(scrapy.Spider):
    name = 'example'
    # allowed_domains = ['example.com']
    start_urls = ['https://search.51job.com/list/000000,000000,0000,00,9,99,python,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=']
    count = 0
    def parse(self, response):
        company_href=response.xpath('//*[@id="resultList"]/div[@class="el"]/p/span/a/@href').extract()
        title_main=response.xpath('//*[@id="resultList"]/div[class="el"]/p/span/a/text').extract()
        for i in company_href:
            yield scrapy.Request(i,callback=self.parse_self,meta={
                title
            })
        next_page=response.xpath('//li[@class="bk"]')[1].xpath('./a/@href').extract_first()
        if  self.count<2:
            yield scrapy.Request(next_page, callback=self.parse)
            self.count+=1
    def parse_self(self,response):
        company_postion=response.xpath('//div[@class="tHeader tHjob"]//div[@class="in"]//div[@class="cn"]//h1/text()').extract()[0].replace('\t','').strip()
        company_salary=response.xpath('//div[@class="tHeader tHjob"]//div[@class="in"]//div[@class="cn"]//strong/text()').extract()[0].strip()
        corporate_name=response.xpath('//div[@class="tHeader tHjob"]//div[@class="in"]//div[@class="cn"]//p[@class="cname"]/a[1]/text()').extract()[0].strip()
        print(company_postion)
        print(corporate_name)
        print(company_salary)
        item=Scrapy3Item()
        item['company_postion']=kong.strip()
        item['corporate_name']=corporate_name
        item['company_salary']=company_salary
        yield item
# /html/body/div[3]/div[2]/div[2]/div/div[1]/h1
# //*[@id="resultList"]/div[4]/p/span/a
item.py
import scrapy


class Scrapy3Item(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    corporate_name=scrapy.Field()
    company_salary=scrapy.Field()
    company_postion=scrapy.Field()
settings.py 配置里面 加上这些
ROBOTSTXT_OBEY = False#不遵守爬虫协议
DOWNLOAD_DELAY = 2 #下载延迟时间 避免 爬取频率太快 被制裁
FEED_FORMAT='csv'
FEED_URI='jobsa.csv'#定义保存为什么文件csv文件,已保存到哪
run.py 运行文件 每次都在命令行运行比较麻烦 定义一个运行文件
from scrapy.cmdline import execute
execute('scrapy crawl example'.split())
其他的文件暂时没有用到
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值