python求职网站_使用Python爬虫爬取招聘网站

# 导入相关的包

import requests

import time

import csv

from urllib.parse import urlencode

from urllib.parse import quote

# 使用面向对象的方式

class Spider:

def __init__(self, page, cityId, search_keywords):

'''初始化方法'''

start = (page - 1) * 90 # 搜索结果每页90个,请求参数的start的值为0,90,180等。

self.headers = {

'referer': 'https://sou.zhaopin.com/?jl=' + str(cityId) + '&kw=' + quote(search_keywords) + '&kt=3',

'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)',

'x-requested-with': 'XMLHttpRequest' # ajax异步请求

}

self.params = {

'start': start,

'pageSize': '90',

'cityId': cityId,

'workExperience': -1,

'education': -1,

'companyType': -1,

'employmentType': -1,

'jobWelfareTag': -1,

'kw': search_keywords,

'kt': 3,

'_v': '0.00882343',

'x-zp-page-request-id': '',

'x-zp-client-id': ''

}

self.url_search = 'https://fe-api.zhaopin.com/c/i/sou?' + urlencode(self.params)

def get_one_page(self):

'''请求网页'''

try:

response = requests.get(self.url_search, headers=self.headers)

if response.status_code == 200:

return response.json()

except requests.ConnectionError:

print('连接错误')

return None

def parse_one_page(self, response_json):

'''解析网页'''

if response_json:

items = response_json.get('data').get('results')

for item in items:

crawlTime = str(time.ctime()) # 抓取时间

businessArea = item.get('businessArea') # 公司所在区域

city = item.get('city').get('items')[0].get('name') # 公司所在城市

companyName = item.get('company').get('name') # 公司名称

companyNumber = item.get('company').get('number') # 公司ID

companySize = item.get('company').get('size').get('name') # 公司人数规模

eduLevel = item.get('eduLevel').get('name') # 职位要求的学历

jobName = item.get('jobName') # 职位名称

jobNumber = item.get('number') # 职位ID

jobType = item.get('jobType').get('items')[0].get('name') # 职位类别

positionURL = item.get('positionURL') # 职位网址

salary = item.get('salary') # 薪资

updateDate = item.get('updateDate') # 职位更新时间

workingExp = item.get('workingExp').get('name') # 工作年限要求

zhilian_results = [crawlTime, businessArea, city, companyName, companyNumber, companySize, eduLevel,

jobName, jobNumber, jobType, positionURL, salary, updateDate, workingExp]

print('zhilian_results:', zhilian_results)

yield zhilian_results

def save_to_csv(self, zhilian_results):

'''保存数据到CSV文件'''

headers = ['crawlTime', 'businessArea', 'city', 'companyName', 'companyNumber',

'companySize', 'eduLevel', 'jobName', 'jobNumber', 'jobType', 'positionURL',

'salary' , 'updateDate', 'workingExp']

with open('zhilian_results.csv', 'a', encoding='utf-8', newline='') as f:

f_csv = csv.writer(f)

f_csv.writerow(headers)

f_csv.writerows(zhilian_results)

def run(self):

'''启动函数'''

response_json = self.get_one_page()

zhilian_search_results = self.parse_one_page(response_json)

self.save_to_csv(zhilian_search_results)

if __name__ == '__main__':

# 抓取搜索相关性较高的前3页

for i in range(1, 4):

time.sleep(1)

s = Spider(i, 763, '数据分析')

s.run()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python爬虫可以用于爬取各种求职信息,以下是一些常见的方法: 1. 使用 requests 库发送 HTTP 请求,获取网页内容。 2. 使用 beautifulsoup4 库或 lxml 库解析 HTML 页面,提取所需信息。 3. 使用 re 正则表达式进行信息提取和筛选。 4. 使用 pandas 库进行数据处理和格式化输出。 5. 使用 scrapy 框架进行高效的爬虫开发和数据爬取。 下面是一个简单的示例,爬取 Boss 直聘上的 Python 工程师职位信息: ```python import requests from bs4 import BeautifulSoup url = 'https://www.zhipin.com/job_detail/?query=Python&city=101020100&industry=&position=' 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 Edge/16.16299', 'Referer': 'https://www.zhipin.com/', } response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'lxml') job_list = [] for job in soup.select('.job-list ul li'): job_dict = {} job_dict['title'] = job.select('div.job-title')[0].text.strip() job_dict['salary'] = job.select('span.red')[0].text.strip() job_dict['company'] = job.select('div.company-text h3.name a')[0].text.strip() job_dict['location'] = job.select('div.job-title small')[0].text.strip() job_dict['experience'] = job.select('div.info-primary p')[0].text.strip() job_dict['education'] = job.select('div.info-primary p')[1].text.strip() job_dict['tags'] = [tag.text.strip() for tag in job.select('div.job-tags span')] job_list.append(job_dict) print(job_list) ``` 这个示例使用 requests 库发送 HTTP 请求,获取 Boss 直聘上 Python 工程师职位的页面内容。然后使用 beautifulsoup4 库解析 HTML 页面,提取职位的标题、薪水、公司、地点、经验要求、学历要求和职位标签等信息。最后将提取到的信息存储在一个列表中,输出到控制台。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值