Python爬虫新手入门教学(四):爬取前程无忧招聘信息

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

Python爬虫、数据分析、网站开发等案例教程视频免费在线观看

https://space.bilibili.com/523606542

前文内容

Python爬虫新手入门教学(一):爬取豆瓣电影排行信息

Python爬虫新手入门教学(二):爬取小说

Python爬虫新手入门教学(三):爬取链家二手房数据

基本开发环境

  • Python 3.6
  • Pycharm

相关模块的使用

  • requests
  • parsel
  • csv
  • re

安装Python并添加到环境变量,pip安装需要的相关模块即可。

一、明确需求

Python爬虫新手入门教学(四):爬取前程无忧招聘信息


爬取内容:

  • 招聘标题
  • 公司
  • 薪资
  • 城市区域
  • 工作经验要求、学历要求、招聘人数、发布时间、公司福利
  • 岗位职责、任职要求

二、请求网页,先获取所有招聘信息的详情url地址

Python爬虫新手入门教学(四):爬取前程无忧招聘信息


使用开发者工具发现网页加载出来的内容是乱代码的,这也意味着等会再爬取的时候,是需要转码的,这样看是看不出自己想要的内容网页是否有返回数据,可以复制网页中的数据,在网页源代码里面搜索。

Python爬虫新手入门教学(四):爬取前程无忧招聘信息

没有结果,那么我们就可以搜索详情链接的ID

Python爬虫新手入门教学(四):爬取前程无忧招聘信息

里面不仅有ID 还有详情url地址。用正则表达式匹配出ID,然后再拼接url,如果匹配出url地址的话,需要再转一次。

Python爬虫新手入门教学(四):爬取前程无忧招聘信息

特别声明:
因为网站原因,每一个招聘详细页面url地址,仅仅只是ID的变化,如果ID不是唯一变化值的时候,那取url地址更好。

import requests
import re


def get_response(html_url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
    }
    response = requests.get(url=html_url, headers=headers)
    return response


def get_id(html_url):
    response = get_response(html_url)
    result = re.findall('"jobid":"(\d+)"', response.text)
    print(response.text)
    print(result)


if __name__ == '__main__':
    url = 'https://search.51job.com/list/010000%252C020000%252C030200%252C040000%252C090200,000000,0000,00,9,99,python,2,1.html'
    get_id(url)

简单总结

打印 response.text 可以在pycharm里面使用正则匹配规则,可以测试是否有匹配到数据。详情如下图所示

Python爬虫新手入门教学(四):爬取前程无忧招聘信息

三、解析招聘信息数据,提取内容

Python爬虫新手入门教学(四):爬取前程无忧招聘信息


每页第一个招聘信息是没有薪资的,没有薪资待遇的,对于没有薪资的招聘信息,我们就自动跳过就好了,所以需要先判断一下。

其次前面有说过,网页查看内容是有乱码的,需要进行转码。

def get_content(html_url):
    result = get_id(html_url)
    for i in result:
        page_url = f'https://jobs.51job.com/shanghai-xhq/{i}.html?s=01&t=0'
        response = get_response(page_url)
        # 进行转码
        response.encoding = response.apparent_encoding
        html_data = response.text
        selector = parsel.Selector(html_data)
        # 薪资
        money = selector.css('.cn strong::text').get()
        # 判断如果有薪资继续提取相关内容
        if money:
            # 标题
            title = selector.css('.cn h1::attr(title)').get()
            # 公司
            cname = selector.css('.cname a:nth-child(1)::attr(title)').get()
            # 上海-徐汇区  |  5-7年经验  |  本科  |  招1人  |  01-25发布
            info_list = selector.css('p.msg.ltype::attr(title)').get().split('  |  ')
            city = info_list[0]     # 城市
            exp = info_list[1]      # 经验要求
            edu = info_list[2]      # 学历要求
            people = info_list[3]   # 招聘人数
            date = info_list[4]     # 发布时间
            # 福利
            boon_list = selector.css('.t1 span::text').getall()
            boon_str = '|'.join(boon_list)
            # 岗位职责:  任职要求:
            position_list = selector.css('.job_msg p::text').getall()
            position = '\n'.join(position_list)
            dit = {
                '标题': title,
                '公司': cname,
                '城市': city,
                '经验要求': exp,
                '学历要求': edu,
                '薪资': money,
                '福利': boon_str,
                '招聘人数': people,
                '发布时间': date,
                '详情地址': page_url,
            }

四、保存数据(数据持久化)

关于薪资待遇、公司地址这些就用csv保存,岗位职责和任职要求就保存文本格式吧,这样看起来会稍微舒服一些。

保存csv

f = open('python招聘.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['标题', '公司', '城市', '经验要求', '学历要求',
                                           '薪资', '福利', '招聘人数', '发布时间',
                                           '详情地址'])

csv_writer.writeheader()

保存txt

txt_filename = '岗位职责\\' + f'{cname}招聘{title}信息.txt'
with open(txt_filename, mode='a', encoding='utf-8') as f:
    f.write(position)

五、多页数据爬取

    '''
if __name__ == '__main__':
    '''
    第一页地址:
    https://search.51job.com/list/010000%252c020000%252c030200%252c040000%252c090200,000000,0000,00,9,99,python,2,1.html
    第二页地址:
    https://search.51job.com/list/010000%252c020000%252c030200%252c040000%252c090200,000000,0000,00,9,99,python,2,2.html
    第三页地址:
    https://search.51job.com/list/010000%252c020000%252c030200%252c040000%252c090200,000000,0000,00,9,99,python,2,3.html
    '''
    for page in range(1, 11):
        url = f'https://search.51job.com/list/010000%252C020000%252C030200%252C040000%252C090200,000000,0000,00,9,99,python,2,{page}.html'
        get_content(url)

实现效果

Python爬虫新手入门教学(四):爬取前程无忧招聘信息

Python爬虫新手入门教学(四):爬取前程无忧招聘信息

Python爬虫新手入门教学(四):爬取前程无忧招聘信息

Python爬虫新手入门教学(四):爬取前程无忧招聘信息

补充代码

正则匹配替换特殊字符

def change_title(title):
    pattern = re.compile(r"[\/\\\:\*\?\"\<\>\|]")  # '/ \ : * ? " < > |'
    new_title = re.sub(pattern, "_", title)  # 替换为下划线
    return new_title

主函数代码

def main(html_url):
    result = get_id(html_url)
    for i in result:
        page_url = f'https://jobs.51job.com/shanghai-xhq/{i}.html?s=01&t=0'
        response = get_response(page_url)
        response.encoding = response.apparent_encoding
        html_data = response.text
        selector = parsel.Selector(html_data)
        # 薪资
        money = selector.css('.cn strong::text').get()
        # 判断如果有薪资继续提取相关内容
        if money:
            # 标题
            title = selector.css('.cn h1::attr(title)').get()
            # 公司
            cname = selector.css('.cname a:nth-child(1)::attr(title)').get()
            # 上海-徐汇区  |  5-7年经验  |  本科  |  招1人  |  01-25发布
            info_list = selector.css('p.msg.ltype::attr(title)').get().split('  |  ')
            if len(info_list) == 5:
                city = info_list[0]  # 城市
                exp = info_list[1]  # 经验要求
                edu = info_list[2]  # 学历要求
                people = info_list[3]  # 招聘人数
                date = info_list[4]  # 发布时间
                # 福利
                boon_list = selector.css('.t1 span::text').getall()
                boon_str = '|'.join(boon_list)
                # 岗位职责:  任职要求:
                position_list = selector.css('.job_msg p::text').getall()
                position = '\n'.join(position_list)
                dit = {
                    '标题': title,
                    '公司': cname,
                    '城市': city,
                    '经验要求': exp,
                    '学历要求': edu,
                    '薪资': money,
                    '福利': boon_str,
                    '招聘人数': people,
                    '发布时间': date,
                    '详情地址': page_url,
                }
                new_title = change_title(title)
                txt_filename = '岗位职责\\' + f'{cname}招聘{new_title}信息.txt'
                with open(txt_filename, mode='a', encoding='utf-8') as f:
                    f.write(position)
                csv_writer.writerow(dit)
                print(dit)

总结

① 有一些公司招聘并没有写学历要求,直接爬取会进行保存,超出索引范围,要进行判断;
② 保存txt文本的时候,会出现特殊字符无法保存需要把标题进行正则匹配,替换掉特殊字符;

猜你喜欢

转载自blog.csdn.net/m0_48405781/article/details/113179478