小生博客:http://xsboke.blog.51cto.com
-------谢谢您的参考,如有疑问,欢迎交流
注意:BOSS应该是做了防爬虫的功能,好像是如果频繁访问,就需要输入一下验证码.
为了节省时间,当前只爬取了热门城市的python相关职位信息
代码如下:
from bs4 import BeautifulSoup
import requests,re,sys
sys.setrecursionlimit(10000)
Init_Url = 'https://www.zhipin.com'
Index_Url = 'https://www.zhipin.com/job_detail/?query=python'
h = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36'}
r = requests.get(Index_Url,headers=h)
r.encoding = 'utf-8'
content = r.text
soup = BeautifulSoup(content,features='html.parser')
so_tag = soup.find('dd',class_='city-wrapper')
Areas_tag = so_tag.find_all(href=re.compile("^/c"),text=re.compile("[^全国]"))
for Area in Areas_tag:
HREF = Area['href']
KA = Area['ka']
AreaName = Area.text
NextPage = Init_Url + '/' + HREF
arg = {'query': 'python','ka':KA}
def Make(NURL,HEAD,PAR):
r2 = requests.get(NURL,headers=HEAD,params=PAR)
r2.encoding = 'utf-8'
content2 = r2.text
soup2 = BeautifulSoup(content2,features='html.parser')
so2 = soup2.find('div',class_='job-list')
so2_tags = so2.find_all('li')
for so2_tag in so2_tags:
List = []
Job_Type = so2_tag.find('div',class_="info-primary").h3.find('div',class_='job-title').get_text() # 招聘职位
Compensation = so2_tag.find('div',class_="info-primary").h3.find('span',class_='red').get_text() # 薪资
CompanyAddress = so2_tag.find('div',class_="info-primary").p.get_text("|").split('|')[0] # 公司地址
WorkTime = so2_tag.find('div',class_="info-primary").p.get_text("|").split('|')[1] # 工作年限
Education = so2_tag.find('div',class_="info-primary").p.get_text("|").split('|')[2] # 学历
CompanyName = so2_tag.find('div',class_="info-company").find('div',class_='company-text').h3.a.get_text() # 公司名称
List.extend([Job_Type,Compensation,CompanyAddress,WorkTime,Education,CompanyName])
CompanyInfo = so2_tag.find('div',class_="info-company").find('div',class_='company-text').p.get_text("|").split('|')
if len(CompanyInfo) == 1:
Trade = so2_tag.find('div', class_="info-company").find('div', class_='company-text').p.get_text("|").split('|')[0] # 公司行业
List.append(Trade)
elif len(CompanyInfo) == 2:
Trade = so2_tag.find('div', class_="info-company").find('div', class_='company-text').p.get_text("|").split('|')[0] # 公司行业
CompanyStage = so2_tag.find('div', class_="info-company").find('div', class_='company-text').p.get_text("|").split('|')[1] # 公司发展阶段
List.extend([Trade,CompanyStage])
elif len(CompanyInfo) == 3:
Trade = so2_tag.find('div', class_="info-company").find('div', class_='company-text').p.get_text("|").split('|')[0] # 公司行业
CompanyStage = so2_tag.find('div', class_="info-company").find('div', class_='company-text').p.get_text("|").split('|')[1] # 公司发展阶段
CompanyScale = so2_tag.find('div', class_="info-company").find('div', class_='company-text').p.get_text("|").split('|')[2] # 公司规模
List.extend([Trade,CompanyStage,CompanyScale])
print('\t'.join(List))
# 判断是否有下一页
IFNextPage = so2.find('a',ka='page-next')
if 'disabled' not in IFNextPage['class']:
NextPage2 = Init_Url + IFNextPage['href']
arg2 = IFNextPage['ka']
print(NextPage2)
Make(NURL=NextPage2, HEAD=h, PAR=arg2)
Make(NURL=NextPage,HEAD=h,PAR=arg)
输出如下:
cto有广告过滤机制,防止文章被删除,这里有些字段用xx覆盖了
pythonxx生 3k-4k 北京 海淀区 知春路 1年以内 学历不限 xxx计算所 计算机软件 未融资 100-499人
Pythonxx开xx程师 15k-30k 北京 海淀区 中关村 3-5年 本科 xxx科技 移动互联网 C轮 1000-9999人
高级pythonxx工程师 20k-35k 北京 朝阳区 高碑店 3-5年 本科 xxx科技 互联网 已上市 500-999人
高级Pythoxx工程师 20k-40k 北京 海淀区 五道口 3-5年 本科 xxxndy 移动互联网 不需要融资 100-499人
Pythonxx工程师 12k-15k 北京 海淀区 五道口 1-3年 本科 阿凡题 移动互联网 B轮 100-499人
高级Pythonxx工程师 30k-50k 北京 朝阳区 望京 5-10年 本科 xxxapp 互联网 D轮及以上 100-499人
SDK后端python工程师 12k-24k 北京 朝阳区 北苑 1-3年 本科 xxx游戏 游戏 B轮 100-499人
..............