赞
踩
该爬虫一般需要登录提交数据,才能进入页面提取信息。
# 打开网页
r=requests.get(link,headers=headers)
# 通过 BeautifulSoup 的 find_all 方法解析
页面
soup=BeautifulSoup(r.text,"lxml")
job_list=soup.find_all('li',class_="job_
itemclearfix")
# 查找公司名称、职位、薪水等信息
for job in job_list:
comp=job.find('div',class_='comp_name').
text.strip()
name=job.find('span',class_='name').text.
strip()
salary=job.find('p',class_='job_salary').text.
strip()
(4)爬取结果,本地保存。爬虫从网页中提取数据后,需要将数据保存下来, Python提供存储数据的方法:可保存到文件中(包括TXT 文件和 XLS 文件),也可存储在数据库中(包括 MySQL 关系数据库和 MongoDB 数据库)。该爬虫对招聘信息进行本地保存,存储格式为 .XLS 文件。部分代码如下:# 创建 Excel 文档保存将要爬取的数据
book=xlwt.Workbook(encoding='utf-8')
sheet1=book.add_sheet(u'sheet1',cell_
overwrite_ok=True)
# 在创建好的 Excel 文档中写入信息标题
heads=[u' 公司名称 ',u' 职位 ',u' 薪水 ']
ii=0
for head in heads:
sheet1.write(0,ii,head)
ii+=1
# 调 用 write 方 法 将 查 找 到 的 信 息 写 入
Excel 文档中
for data in (comp,name,salary):
sheet1.write(i+1,0,comp)
sheet1.write(i+1,1,name)
sheet1.write(i+1,2,salary)
i+=1
# 保存 Excel 文档
book.save('joblist.xls')
保存结果如图 3 所示。
可以看到和网页中提供的招聘信息是一
致的。
3.3 改进和完善上述爬虫只能爬取网页上的第一页招聘信息,如果需要爬取所有页面信息,可根据分析网页 URL 地址的规律,使用 for 循环爬取。分析比较:
0 d 3 0 3 6 5 5 - 0 0 2 e - 4 9 6 4 - f 9 0 b -
fa2a1cb1f628&ClickID=3
0 d 2 0 2 4 0 8 - 0 0 2 e - 4 6 b c - d 2 a 9 -
36b62fb6175b&ClickID=2
到下一页 ClickID 的值增加 1, 需重新构造
URL。
部分代码如下:
fori in range(0,10):
link1=link+str(i)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。