请您扮演一个python开发人员。第一个命令是用python动态获取51招聘网的多页招聘信息,并保存为csv文件。第二个命令是根据保存的csv文件,按照不同岗位和薪资范围进行饼图和柱状图的可视化处理
时间: 2024-05-08 17:15:25 浏览: 35
第一个命令:
```python
import requests
import csv
from bs4 import BeautifulSoup
# 定义函数获取招聘信息
def get_job_info(page_num):
url = 'https://search.51job.com/list/000000,000000,0000,00,9,99,python,2,' + str(page_num) + '.html'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
response = requests.get(url, headers=headers)
response.encoding = 'gbk'
soup = BeautifulSoup(response.text, 'html.parser')
job_list = soup.find_all('div', class_='el')
for job in job_list[1:]:
job_name = job.find('a')['title']
job_company = job.find('span', class_='t2').text.strip()
job_location = job.find('span', class_='t3').text.strip()
job_salary = job.find('span', class_='t4').text.strip()
job_date = job.find('span', class_='t5').text.strip()
yield [job_name, job_company, job_location, job_salary, job_date]
# 保存招聘信息
with open('job_info.csv', 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['职位名称', '公司名称', '工作地点', '薪资', '发布日期'])
for i in range(1, 6):
for job in get_job_info(i):
writer.writerow(job)
```
第二个命令:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取csv文件
df = pd.read_csv('job_info.csv')
# 按薪资范围进行分类
df['salary_range'] = pd.cut(df['薪资'], bins=[0, 3000, 6000, 9000, 12000, 15000, 20000, 30000, 50000], labels=['3k以下', '3k-6k', '6k-9k', '9k-12k', '12k-15k', '15k-20k', '20k-30k', '30k以上'])
# 统计不同岗位数量
job_count = df.groupby('职位名称').size().sort_values(ascending=False)[:10]
# 绘制柱状图
plt.bar(job_count.index, job_count.values)
plt.title('不同岗位数量统计')
plt.xlabel('职位名称')
plt.ylabel('数量')
plt.xticks(rotation=45)
plt.show()
# 统计不同薪资范围数量
salary_count = df.groupby('salary_range').size().sort_values(ascending=False)
# 绘制饼图
plt.pie(salary_count.values, labels=salary_count.index, autopct='%1.1f%%')
plt.title('不同薪资范围数量统计')
plt.show()
```
以上代码仅供参考,实际运行情况可能会有所不同。
阅读全文