简要概述
抓取数据代码
import json
import time
import requests
import csv
# 1. 创建文件对象
f = open('lgposition_hz_shenduxuexi_4.7.1.。1.1.csv', 'w', encoding='utf-8', newline='') #文件名记得每爬取一个职业修改一次~
# 2. 基于文件对象构建 csv写入对象
csv_writer = csv.writer(f)
# 3. 构建列表头
csv_writer.writerow(
["公司", "职位名称", "公司简称", "公司规模", "公司行业", "融资", "福利", "职位类型", "第二职位", "第三职位", "技能", "职位发布时间", "城市", "区域",
"薪水", "工作年限", "学历", "职位优势"])
def extractPositionData(results): #提取职位信息
if len(results): #?? 表示什么意思
for result in results:
companyLabelList = result['companyLabelList'] #单个的招聘信息中所包含的公司标签信息(含有多个)提取
companyLabelLists = ''#初始化
if len(companyLabelList): #一条招聘信息中的公司标签有好几个标签
for i in companyLabelList:
companyLabelLists += i + ',' #转换成一个字符串
skillLable = result['skillLables'] #技能需要 同公司标签
skillLables = ''
if len(skillLable):
for i in skillLable:
skillLables += i + ','
# 4. 写入csv文件内容
csv_writer.writerow(
[result['companyFullName'],
result['positionName'],
result['companyShortName'],
result['companySize'],
result['industryField'], #公司行业
result['financeStage'],
companyLabelLists, #福利
result['firstType'], #职位类型
result['secondType'], #第二职位
result['thirdType'], #第三职位
skillLables, #技能
result['createTime'], #职位发布时间
result['city'],
result['district'], #区域
result['salary'],
result['workYear'], #工作年限
result['education'],
result['positionAdvantage']]) #职位优势
def main(pages,position,city):
# 主url
url1 = 'https://www.lagou.com/jobs/list_'+ position+ '?city='+city+'&fromSearch=true&labelWords=&suginput=' #网页链接,不管第几页都是该url【异步加载】
# ajax请求
print (url1)
url = "https://www.lagou.com/jobs/positionAjax.json?city="+city+"&needAddtionalResult=false" #F12里面的请求链接
# 请求头
headers = {
'Connection': 'keep-alive',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'X-Anit-Forge-Code': '0',
'X-Requested-With': 'XMLHttpRequest',
'X-Anit-Forge-Token': 'None',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Origin': 'https://www.lagou.com',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Referer': 'https://www.lagou.com/jobs/list_%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0?city=%E6%9D%AD%E5%B7%9E&fromSearch=true&labelWords=&suginput=',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
}
# 通过data来控制翻页
pages1 = pages+1
for page in range(1, pages1):
data = {
'first': 'false',
'pn': page,
'kd': position
}
s = requests.Session()
s.get(url=url1, headers=headers, timeout=3) #获取html信息 ,从中提取cookie传递给请求链接 respon使用
cookie = s.cookies
respon = s.post(url=url, headers=headers, data=data, cookies=cookie, timeout=3) #f12里发送的请求链接的信息(请求翻页)
#time.sleep(3) #反爬虫,每爬取一次休眠3秒钟
total = respon.text #r.text
results = json.loads(respon.text)['content']['positionResult']['result'] #将r.text文件转换为json格式,再将json内容放入字典中,可以看做是二维列表,每一行代表一个招聘信息,一共有n行。result[0]表示第一个招聘数据。
print ("正在爬取第"+ str(page) +"页...")
extractPositionData(results) #运用定义函数
print ("————已爬取完第"+ str(page) +"页————")
print("已爬取完毕"+city+"这个城市的"+position+"岗位前"+str(pages1)+"页信息")
if __name__ == '__main__':
main(3,'运营',"%E6%9D%AD%E5%B7%9E") #通过F12查看代码 ,%E6%9D%AD%E5%B7%9E表示杭州。传入main函数
# 5. 关闭文件
f.close()
新手操作容易出现的问题
tip1:利用python官方的pip通过cmd进行库的安装速度很慢,这是由于国外的官方源经常被墙,导致安装不上。我们可以更换pip源——使用国内的python镜像源来解决Python安装不上库的问题。
pip install SomePackage -i https://pypi.tuna.tsinghua.edu.cn/simple