Python爬虫框架Scrapy实例(爬取腾讯社招信息并保存为excel)

本文介绍使用Scrapy框架爬取腾讯社会招聘信息的过程,包括创建项目、编写爬虫代码、处理数据并保存至Excel等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:

在学习python爬虫的时候,曾经爬取过腾讯社招的网站,很久很久没有写爬虫, 心血来潮打算爬一个练手,想起之前爬过腾讯社招网站, 打开一看网页变了,行动,重新写一遍。这个网站相对简单,做了简单测试没有设置反爬,比较适合初学者拿来练手。

搜索页面:
腾讯社招主页面
点击列表中的某个职位后,会跳转到下面页面, 我们需要爬取跳转后的页面的数据。
点开列表的详细页面,也就是我们要爬取的信息页面
爬取结果
我们最终将数据写入到excel中。
爬取结果
详细步骤
直接上代码:
开发工具用的PyCharm,自行添加相关依赖。
一、创建scrapy项目。
scrapy startproject Tencent
在Terminal中运行上面命令,scrapy会自动创建项目,目录结构如下(忽略红线内的两个文件,那是手动创建的):
在这里插入图片描述
二、在spiders文件夹下创建careerDesc.py 的爬虫文件, 代码如下:

# -*- coding: utf-8 -*-
import scrapy
import json
import collections
from ..items import TencentItem

class CareerdescSpider(scrapy.Spider,):
    #爬虫名称
    name = 'careerDesc'
    allowed_domains = ['careers.tencent.com']
    offset = 1
    #通过浏览器开发者工具定位出json文件的链接,以下链接是job list的链接,我们需要的到访问职位详情链接里面的postid,这里我们设置每次读取100 条信息
    url = 'https://careers.tencent.com/tencentcareer/api/post/Query?pageSize=100&pageIndex='
    #爬取postid 的url
    start_urls = [url + str(offset)]
    #设置此爬虫的管道文件,这个属于个人习惯, 如果scrapy中只有一个爬虫文件不需要设置, 如果有多个爬虫文件,需要设置一下。
    custom_settings = {'ITEM_PIPELINES': {'Tencent.pipelines.TencentPipeline': 300}}
    #爬取每一条职位详情的url
    url_careerDESC = 'https://careers.tencent.com/tencentcareer/api/post/ByPostId?postId='

    def parse(self, response):
        '''
        我们需要在此爬虫方法中获取到每一条职位特有的postid,并callback一下详情页的爬虫方法
        '''
        #通过json文件获取到所有职位的数量,方便进行页数判断
        countNum = json.loads(response.text)['Data']['Count']
        #通过json文件获取到postid
        postidjson = json.loads(response.text)['Data']['Posts']
        for each in postidjson:
            #循环获取到的postid 并且组合成url 取调用详情页的爬虫desc_parse
            yield scrapy.Request(self.url_careerDESC + str(each['PostId']), callback=self.desc_Parse)
            #页数判断
        if countNum % 100 == 0:
            page = countNum / 100
            if self.offset < page:
                self.offset += 1
        else:
            page = countNum / 100 + 1
            if self.offset < page:
                self.offset += 1
        yield scrapy.Request(self.url + str(self.offset), callback=self.parse)

    def desc_Parse(self, response):
        '''
        爬取详情页的信息并返回item。
        '''
        descjson = json.loads(response.text)['Data']
        #因为item是一个dict,dict是无序的,输出的时候也是无序的,但我们需要按照我们制定的顺序输出, 
        #所以将item转化成orderdict,这样会按照我们设定的顺序输出,但是orderdict占用的内存是普通dict的两倍,暂时没有想到更好的解决方法
        item = collections.OrderedDict(TencentItem())
        item['ARecruitPostName'] = descjson['RecruitPostName']
        item['BLocationName'] = descjson['LocationName']
        item['CategoryName'] = descjson['CategoryName']
        item['DResponsibility'] = descjson['Responsibility']
        item['ERequirement'] = descjson['Requirement']
        item['FLastUpdateTime'] = descjson['LastUpdateTime']
        item['GPostURL'] = descjson['PostURL']
        yield item

三、编写pipeline文件

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import xlrd
from xlutils.copy import copy


class TencentPipeline(object):
    def process_item(self, item, spider):
        workbook = xlrd.open_workbook('tencentposition.xls')
        sheets = workbook.sheet_names()
        worksheet = workbook.sheet_by_name(sheets[0])
        rows_count = worksheet.nrows
        new_workbook = copy(workbook)
        new_worksheet = new_workbook.get_sheet(0)
        cols = 0
        for v in item.values():
            new_worksheet.write(rows_count,cols,v)
            cols += 1
        new_workbook.save('tencentposition.xls')
        return item

四、编写setting文件

ITEM_PIPELINES = {
   'Tencent.pipelines.TencentPipeline': 300,
}

五、为了方便, 我单独编写了一个文件创建Excel,方便修改。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#-------------------------------------------------------------------------------
# Name:         createExcel
#-------------------------------------------------------------------------------

import xlwt
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

def CreateExcel():
    file = xlwt.Workbook(encoding='utf-8')
    table = file.add_sheet('TencentPosition',cell_overwrite_ok=True)
    table_head = ['职位名称','工作地点','职位类型','岗位职责','工作要求','LastUpdate','PostURL']
    for i in range(len(table_head)):
        table.write(0,i,table_head[i])
    file.save('tencentposition.xls')
    print 'created successful'

if __name__ == '__main__':
    CreateExcel()

六、设置一个程序按照先创建Excel再执行爬虫的顺序执行。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#-------------------------------------------------------------------------------
# Name:         entrypoint
#-------------------------------------------------------------------------------

import os
import time

os.system('python createExcel.py')
time.sleep(5)
os.system('scrapy crawl careerDesc')

我们直接运行entryPoint.py 即可运行程序
至此,一个简单的scrapy爬虫就完成了。
才疏学浅,共同交流。

### Scrapy爬取Boss直聘数据保存Excel 要实现通过Scrapy爬取Boss直聘的数据将结果保存Excel文件中,可以按照以下方法操作: #### 1. 定义Scrapy Item类 定义一个`Item`类用于存储所需字段。这些字段应包括岗位名称、薪资范围、工作地点等信息。 ```python import scrapy class BossZhipinItem(scrapy.Item): jobTitle = scrapy.Field() # 岗位名称 salary = scrapy.Field() # 薪资范围 jobLocation = scrapy.Field() # 工作地点 workExperience = scrapy.Field() # 工作经验要求 educationRequirement = scrapy.Field() # 学历要求 companySize = scrapy.Field() # 公司规模 releaseDate = scrapy.Field() # 发布时间 companyName = scrapy.Field() # 公司名称 ``` 此部分代码基于提供的引用[^2]进行了扩展和调整,以适应实际需求。 --- #### 2. 编写Spider逻辑 编写具体的爬虫逻辑,访问目标网站提取所需的字段信息。 ```python import scrapy from ..items import BossZhipinItem class ZhipinSpider(scrapy.Spider): name = 'zhipin' allowed_domains = ['www.zhipin.com'] start_urls = ['https://www.zhipin.com/job_detail/?query=Python&city=101010100'] def parse(self, response): items = [] jobs = response.css('div.job-primary') # 提取职位列表 for job in jobs: item = BossZhipinItem() item['jobTitle'] = job.css('div.info-primary h3.name a::text').get().strip() item['salary'] = job.css('span.red::text').get().strip() item['jobLocation'] = job.css('p::text').re(r'.*?([\u4e00-\u9fa5]+).*')[0].strip() item['workExperience'] = job.css('p::text').extract()[1].split('/')[0].strip() item['educationRequirement'] = job.css('p::text').extract()[1].split('/')[-1].strip() item['companyName'] = job.css('div.company-info a::attr(title)').get().strip() yield item ``` 这段代码实现了对Boss直聘面的解析,提取了关键字段。注意需要根据实际情况修改CSS选择器路径[^2]。 --- #### 3. 数据导出为Excel 利用`pandas`库将抓取的结果转换成DataFrame对象,最终保存Excel文件。 ```python import pandas as pd def save_to_excel(items, filename='output.xlsx'): data = { '岗位': [item['jobTitle'] for item in items], '薪资': [item['salary'] for item in items], '工作地点': [item['jobLocation'] for item in items], '工作经验': [item['workExperience'] for item in items], '学历要求': [item['educationRequirement'] for item in items], '公司名称': [item['companyName'] for item in items] } df = pd.DataFrame(data) df.to_excel(filename, index=False) # 在管道中调用该函数 save_to_excel(items) # `items` 是从 Spider 中获取的所有数据项 ``` 这部分代码参考了站内引用的内容[^1],通过`pandas`完成数据整理与输出功能。 --- #### 4. 配置Pipeline处理数据 为了自动执行上述保存过程,在项目的`pipelines.py`文件中配置如下内容: ```python import json from .utils import save_to_excel class SaveToExcelPipeline(object): def __init__(self): self.items = [] def process_item(self, item, spider): self.items.append(item) return item def close_spider(self, spider): save_to_excel(self.items) ``` 最后在`settings.py`中启用该Pipeline: ```python ITEM_PIPELINES = {'your_project.pipelines.SaveToExcelPipeline': 300} ``` 以上步骤确保了所有采集的数据能够被正确地保存到指定的Excel文件中。 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值