017/Code

/2018.12.12/20181212-01-02-04-718.py
import json
import requests
import pandas as pd
import time
import os
import re
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
import matplotlib.patches as mpatches
from bs4 import BeautifulSoup
import warnings
warnings.filterwarnings("ignore")

def get_city_name():
city_name = []
for i in range(100):
url = 'https://www.zhipin.com/c'+str(i+1)+'/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
}
response = requests.get(url, headers=headers)
html = response.text
soup = BeautifulSoup(html, 'lxml')
# 获取城市名
for i in soup.find_all('div', class_="col-2"):
city_name.append(i.text.strip())

# 将城市名存入文件中
with open('city_name.txt', 'w', encoding='utf-8') as f:
for city in city_name:
f.write(city + '\n')
return city_name

def get_city_url(city_name):
url_list = []
for i in range(len(city_name)):
url = 'https://www.zhipin.com/c'+str(i+1)+'/job_list/?query=Python&page=0'
url_list.append(url)
return url_list

def get_job_url(url_list):
job_url = []
for url in url_list:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
}
response = requests.get(url, headers=headers)
html = response.text
soup = BeautifulSoup(html, 'lxml')
for i in soup.find_all('div', class_='job-list'):
for j in i.find_all('a'):
job_url.append(j['href'])
return job_url

def get_job_name(job_url):
job_name = []
for url in job_url:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
}
response = requests.get(url, headers=headers)
html = response.text
soup = BeautifulSoup(html, 'lxml')
for i in soup.find_all('div', class_='job-title'):
job_name.append(i.text)
return job_name

def get_job_detail(job_url):
job_detail = []
for url in job_url:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
}
response = requests.get(url, headers=headers)
html = response.text
soup = BeautifulSoup(html, 'lxml')
for i in soup.find_all('div', class_='job-require'):
job_detail.append(i.text)
return job_detail

def save_job_name(job_name):
with open('job_name.txt', 'w', encoding='utf-8') as f:
for job in job_name:
f.write(job + '\n')

def save_job_detail(job_detail):
with open('job_detail.txt', 'w', encoding='utf-8') as f:
for job in job_detail:
f.write(job + '\n')

def save_job_url(job_url):
with open('job_url.txt', 'w', encoding='utf-8') as f:
for job in job_url:
f.write(job + '\n')

def save_city_name(city_name):
with open('city_name.txt', 'w', encoding='utf-8') as f:
for city in city_name:
f.write(city + '\n')

def job_analysis(job_detail):
job_detail = job_detail
job_detail = job_detail.split('\n')
job_detail = [item for item in job_detail if item != '']
job_detail = [item for item in job_detail if item != ' ']
job_detail = [item for item in job_detail if item != '\r']
job_detail = [item for item in job_detail if item != '\t']
job_detail = [item for item in job_detail if item != '\n']
job_detail = [item for item in job_detail if item != ' ']
job_detail = [item for item in job_detail if item != '\r']
job_detail = [item for item in job_detail if item != '\t']
job_detail = [item for item in job_detail if item != '\n']
job_detail = [item for item in job_detail if item != ' ']
job_detail = [item for item in job_detail if item !=