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

编程入门 行业动态 更新时间:2024-10-22 13:56:57

Python<a href=https://www.elefans.com/category/jswz/34/1770264.html style=爬虫新手入门教学(四):爬取前程无忧招聘信息"/>

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

前言

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

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

前文内容

 

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

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

 

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

基本开发环境

  • Python 3.6
  • Pycharm

相关模块的使用

  • requests
  • parsel
  • csv
  • re

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

一、明确需求

 


爬取内容:

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

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

 


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

 

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

 

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

 

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

import requests
import redef 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 responsedef 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 = ',000000,0000,00,9,99,python,2,1.html'get_id(url)

简单总结

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

 

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

 


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

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

def get_content(html_url):result = get_id(html_url)for i in result:page_url = f'/{i}.html?s=01&t=0'response = get_response(page_url)# 进行转码response.encoding = response.apparent_encodinghtml_data = response.textselector = parsel.Selector(html_data)# 薪资money = selector.css(' strong::text').get()# 判断如果有薪资继续提取相关内容if money:# 标题title = selector.css(' h1::attr(title)').get()# 公司cname = selector.css('ame 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__':'''第一页地址:,000000,0000,00,9,99,python,2,1.html第二页地址:,000000,0000,00,9,99,python,2,2.html第三页地址:,000000,0000,00,9,99,python,2,3.html'''for page in range(1, 11):url = f',000000,0000,00,9,99,python,2,{page}.html'get_content(url)

实现效果

 

 

 

 

补充代码

正则匹配替换特殊字符

def change_title(title):pattern = repile(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'/{i}.html?s=01&t=0'response = get_response(page_url)response.encoding = response.apparent_encodinghtml_data = response.textselector = parsel.Selector(html_data)# 薪资money = selector.css(' strong::text').get()# 判断如果有薪资继续提取相关内容if money:# 标题title = selector.css(' h1::attr(title)').get()# 公司cname = selector.css('ame 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文本的时候,会出现特殊字符无法保存需要把标题进行正则匹配,替换掉特殊字符;

更多推荐

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

本文发布于:2024-03-15 09:15:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1738562.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:爬虫   招聘信息   新手入门   前程无忧   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!