import requests |
import re |
import json # 序列化与反序列化 |
import pprint # 格式化输出模块 |
import csv # 保存csv数据 |
f = open ( '招聘网站信息.csv' , mode = 'a' , encoding = 'utf-8' , newline = '') |
csv_writer = csv.DictWriter(f, fieldnames = [ |
'标题' , |
'公司名字' , |
'城市' , |
'薪资' , |
'招聘信息' , |
'公司属性' , |
'公司规模' , |
'企业性质' , |
'招聘发布日期' , |
'公司详情页' , |
'招聘详情页' , |
]) |
csv_writer.writeheader() # 写入表头数据 |
for page in range ( 1 , 11 ): |
# 1. 发送请求, 把地址html后面删掉,页码字符修改 |
url = f 'https://search.51job.com/list/010000%252C020000%252C030200%252C040000%252C090200,000000,0000,00,9,99,python,2,{page}.html' |
headers = { |
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36' |
} |
response = requests.get(url = url, headers = headers) |
# 2. 获取数据, 获取服务器发给我们返回的数据响应数据 |
# <Response [200]> <> 表示response响应对象 200 状态码 表示请求成功 |
# print(response.text) |
# 3. 解析数据 |
# 解析方法: re正则表达式, css选择器 xpath 根据服务器返回的数据内容, 选择最适合的解析方式;[0] 列表索引取第一个元素 |
html_data = re.findall( 'window.__SEARCH_RESULT__ = (.*?)</script>' , response.text, re.S)[ 0 ] |
json_data = json.loads(html_data) |
# pprint.pprint(json_data['engine_jds']) |
for index in json_data[ 'engine_jds' ]: |
# pprint.pprint(index) |
dit = { |
'标题' : index[ 'job_name' ], |
'公司名字' : index[ 'company_name' ], |
'城市' : index[ 'workarea_text' ], |
'薪资' : index[ 'providesalary_text' ], |
'招聘信息' : '|' .join(index[ 'attribute_text' ]), |
'公司属性' : index[ 'companyind_text' ], |
'公司规模' : index[ 'companysize_text' ], |
'企业性质' : index[ 'companytype_text' ], |
'招聘发布日期' : index[ 'issuedate' ], |
'公司详情页' : index[ 'company_href' ], |
'招聘详情页' : index[ 'job_href' ], |
} |
csv_writer.writerow(dit) |
print (dit) |