拉勾网爬虫是异步加载方式,先访问初始页面得到cookie,再用cookie去爬取职位详情页面。
这里参考的是另外一篇文字的做法,开始自己走了很多弯路。原文链接暂时找不到了,后面看到会再贴上来。
爬取过程:
1、创建获取cookie的函数
2、main主程序
根据页面地址封装url,让其可以输入“城市”和“岗位”进行爬取。
3、解析页面
返回的是一个json格式,而且是post方法,但是在post的时候,页面的翻页地址实际上也会发生变化,只是并不会显示出来,所以我们通过改变这个pn实现翻页。
4、解析详情页的地址
详情页地址解析这里也有个坑,就是也需要带上第一步的cookies,否则只能爬取5条详情页,后面的地址就会不一样,导致无法爬取。
另外,详情页的地址里面还带有一个sid,是在解析职位列表的时候附带的一个showid,至于是否一定要这个,还不是很清楚,但是我爬取详情页的时候把它也附带上了,所以详情页的地址如下。
一个是构造的url,另一个是response.url,通过对比两个url的地址不同才发现这个cookies的问题。
5、保存到csv
这一步就没什么好说的了,通过追加的方式逐条保存到csv文件中。这样有个问题就是每一次都有一个标题行,在csv文件中需要手动删除才行。
当然也可以先保存到字典列表,然后一次性,然后用pandas.to_excel的方法一次性写入到excel中,就不会出现上面的问题。
完整代码如下:
#coding:utf-8
import requests
import csv,time
from lxml import etree
def GetCookie():
url = 'https://www.lagou.com/jobs/list_%E8%BF%90%E8%90%A5/p-city_213?&cl=false&fromSearch=true&labelWords=&suginput='
# 注意如果url中有中文,需要把中文字符编码后才可以正常运行
headers = {
'User-Agent': 'ozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3704.400 QQBrowser/10.4.3587.400'
}
response = requests.get(url=url,headers=headers,allow_redirects=False)
# cookies = requests.utils.dict_from_cookiejar(response.cookies)
return response.cookies
def GetData(page,kd,url):
headers = {
'Host': 'www.lagou.com',
'Origin': 'https://www.lagou.com',
'Referer': 'https://www.lagou.com/jobs/list_%E8%BF%90%E8%90%A5?labelWords=&fromSearch=true&suginput=',
'User-Agent': 'ozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3704.400 QQBrowser/10.4.3587.400'
}
data = {
'first': 'true',
'pn': str(page),
'kd': str(kd),
}
s = requests.Session()
response = s.post(url = url,data=data,headers = headers,cookies = GetCookie())
# 这里的请求是post且获取的内容是json格式,因此使用json=data的方式才能获取到数据
response.encoding = response.apparent_encoding # 根据网页内容分析出的编码方式。
html = response.json()['content']['positionResult']['result']
showid = response.json()['content']['showId']
savedata(html,kd,showid,s)
def savedata(html,kd,showid,s):
f = open(kd + '.csv', mode="a+",newline='',encoding='utf-8-sig')
csv_write = csv.writer(f)
csv_write.writerow(['职位名称', '公司名称', '公司规模', '薪资待遇', '工作经验', '是否全职', '学历要求', '公司福利', '发布时间','职位详情'])
for i in range(len(html)):
positionid = html[i]['positionId'] # 职位id,用于爬取下一页内容
job_detail = detail_parse(positionid, showid,s)
positionName = html[i]['positionName'] # 职位名称
companyFullName = html[i]['companyFullName'] # 公司名称
companySize = html[i]['companySize'] # 公司规模
salary = html[i]['salary'] # 薪资待遇
workYear = html[i]['workYear'] # 工作经验
jobNature = html[i]['jobNature'] # 是否全职
education = html[i]['education'] # 学历要求
positionAdvantage = html[i]['positionAdvantage'] # 公司福利
lastLogin = html[i]['lastLogin'] # 发布时间
csv_write.writerow([positionName, companyFullName, companySize, salary, workYear, jobNature, education, positionAdvantage,lastLogin,job_detail])
f.close()
def detail_parse(positionid,showid,s):
# 解析详情页数据
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language':'zh-CN,zh;q=0.9',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Host': 'www.lagou.com',
'Upgrade-Insecure-Requests':'1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3704.400 QQBrowser/10.4.3587.400'
}
url = 'https://www.lagou.com/jobs/{}.html?show={}'.format(positionid,showid)
response = s.get(url,headers = headers,cookies = GetCookie())
print(url)
print(response.url)
tree = etree.HTML(response.content)
job_detail = tree.xpath('//div[@class="job-detail"]/p/text()')
job_detail = ''.join(job_detail)
return job_detail
def main():
print('【说明文档】')
print('1.输入要查询的岗位和城市后,目前只能爬取30页数据')
print('2.为了防止拉勾反爬虫机制,每页爬取时间间隔1秒,无需手动设置')
print('3.爬取完毕后,会在原文件夹生成一个csv文件,跟excel一样打开即可查看数据')
print('*'*30)
# 发送请求,注意是post方法
kd = input('请输入你要查询的岗位:')
city = input('请输入你要查询的城市:')
url = 'https://www.lagou.com/jobs/positionAjax.json?city={}&needAddtionalResult=false'.format(city)
for page in range(2,3):
print('第%i页正在爬取' % (page - 1))
GetData(page,kd,url)
time.sleep(2)
print('*' * 30)
print('所有数据爬取完毕,可以关闭当前界面,前往查看爬取下来的数据了~')
if __name__ == '__main__':
main()