经过了几天的摸索,照猫画虎的把爬虫的部分做完了。

但是很多原理性的东西都不是很理解,就是照着抄的,还需要继续学习。

看这个目录结构,只看.py的文件,.pyc的文件是运行的时候生成的不管它。

items.py:定义想要导出的数据

Pipelines.py:用于将数据导出

settings.py:告诉程序数据传输需要的文件

init.py:没用到过还

(以上是我自己暂时的理解)

我最终抓取的是智联招聘的信息,注意点都在注释里

获取职位连接的关键代码,linkspider,其他文件都是生成时默认的

#encoding:utf-8
from scrapy.spider import BaseSpider
from scrapy.http import FormRequest, Request
from scrapy.selector import HtmlXPathSelector
import os
import sys
import datetime
import reclass ZhaoPinSpider(BaseSpider):name = "zhaopin"allowed_domains = ["zhaopin.com"]#url加上pd=1就只显示今天新添加的职位 城市后面都固定为全国zlzp_urlpatten = "http://sou.zhaopin.com/jobs/searchresult.ashx?pd=1&jl={CITY}&kw={KEYWORD}&p={CURR_PAGE}"def __init__(self):self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0',\'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',\'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',\'Connection':'keep-alive'}   self.start_urls = self.set_url()#set_url方法动态设定要抓取的链接列表def set_url(self):url_list = []#从配置文件中取出所有的关键字列表,逐个检索keys = '大数据,hadoop,hive,hbase,spark,storm,sqoop,pig'for keyword in keys.split(','):url = self.zlzp_urlpattenurl = url.format(CITY='全国', KEYWORD=keyword, CURR_PAGE=1)url_list.append(url)return url_listdef start_requests(self): #该方法必须返回一个可迭代对象(iterable)。该对象包含了spider用于爬取的第一个Request。for url in self.start_urls:yield FormRequest(url,\headers = self.headers,\callback = self.parse)#使用回调函数def parse(self, response):hxs = HtmlXPathSelector(response)keyword = hxs.select('//div[@class="search"]//input[@name="KeyWord"]/@value').extract()[0]keyword = keyword.encode('utf-8')url = self.zlzp_urlpatten#找总页数pageInfo = hxs.select('//div[@class="pagesDown"]//button/@onclick').extract()if pageInfo: #注意 只有一页时找不到pageInfopageInfo = pageInfo[0]pattern = re.compile('.*?value,(.*?),.*', re.S)findPageNum = re.search(pattern, pageInfo)pageNum = int(findPageNum.group(1))else:pageNum = 1for curPage in range(1,pageNum + 1):each_url = url.format(CITY='全国', KEYWORD=keyword, CURR_PAGE=curPage)yield Request(each_url,callback=self.get_joburls_bypage)def get_joburls_bypage(self, response):hxs = HtmlXPathSelector(response)links = hxs.select('//td[@class="zwmc"]//a/@href').extract()# 获得的信息都是当天的 直接入库for link in links:if(link != 'http://e.zhaopin.com/products/1/detail.do'): #有的地方会冒出这个链接 去掉open('../output/link_output/link.txt', 'ab').write(link+'\n')

获取具体职位信息的page相关代码:

settings

# Scrapy settings for zhaopin_page project
#
# For simplicity, this file contains only the most important settings by
# default. All the other settings are documented here:
#
#     http://doc.scrapy.org/topics/settings.html
#

BOT_NAME = 'zhaopin_page'
BOT_VERSION = '1.0'SPIDER_MODULES = ['zhaopin_page.spiders']
NEWSPIDER_MODULE = 'zhaopin_page.spiders'
USER_AGENT = '%s/%s' % (BOT_NAME, BOT_VERSION)
ITEM_PIPELINES = {'zhaopin_page.FilePipelines.PagePipeline':5}

FilePipelines

# encoding: utf-8
import traceback
import datetime
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
sys.path.append("../../../")class PagePipeline(object):#把解析后的内容放入文件中def process_item(self, item, spider):fname =  '../output/page_output/' + item['file_id'] + '.txt'try:outfile = open(fname, 'wb')outfile.write(item['web_id']+self.getJobFieldSpt()+item['job_url']+self.getJobFieldSpt()+item['job_name']+self.getJobFieldSpt()+item['job_location']+self.getJobFieldSpt()+item['job_desc']+self.getJobFieldSpt()+item['edu']+self.getJobFieldSpt()+item['gender']+self.getJobFieldSpt()+item['language']+self.getJobFieldSpt()+item['major']+self.getJobFieldSpt()+item['work_years']+self.getJobFieldSpt()+item['salary']+self.getJobFieldSpt()+item['company_name']+self.getJobFieldSpt()+item['company_desc']+self.getJobFieldSpt()+item['company_address']+self.getJobFieldSpt()+item['company_worktype']+self.getJobFieldSpt()+item['company_scale']+self.getJobFieldSpt()+item['company_prop']+self.getJobFieldSpt()+item['company_website']+self.getJobFieldSpt()+self.getCurrentTimestamp())except Exception as e:print "ERROR GEN FILE!! >>> " + fnameprint traceback.format_exc()def getCurrentTimestamp(self):# 得到时间戳return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')def getJobFieldSpt(self):#得到生成的职位文件字段间的分隔符。使用ascii码1,和hive中默认的分隔符相同        return chr(1)

items

# encoding: utf-8from scrapy.item import Item, Field#定义存放帖子内容的类
class PageItem(Item):#网站标识web_id = Field()#生成的文件名file_id = Field()#职位来源网址job_url = Field()#工作名称job_name = Field()#工作地点    job_location = Field()#职位描述 job_desc = Field()#学历要求   edu = Field()#性别要求      gender = Field()#语言要求       language = Field()#专业要求        major = Field()#工作年限    work_years = Field()#薪水范围         salary = Field()#职位发布时间job_datetime = Field()#公司名称      company_name = Field()#企业介绍company_desc = Field()#公司地址company_address = Field()#行业company_worktype = Field()#规模company_scale = Field()#性质company_prop = Field()#网址company_website = Field()

spider

# encoding: utf-8from scrapy.spider import BaseSpider
from scrapy.http import FormRequest, Request
from scrapy.selector import HtmlXPathSelector
from zhaopin_page import items
import traceback
import sys
import datetime
import re#定义要抓取页面的爬虫类
class ZhaoPinPageSpider(BaseSpider):name = "page"    start_urls = []def __init__(self):        self.start_urls = self.set_url()#从jobs_task表中读出要抓取的链接列表,放入数组中def set_url(self):url_list = []link_file = open('../output/link_output/link.txt', 'r')loops = 0for each_link in link_file:each_link = each_link.replace('\r','')each_link = each_link.replace('\n','')url_list.append(each_link)loops+=1if (loops == 100):breaklink_file.close()return url_listdef parse(self, response):try:#url中后面的数字串file_id = response.url.split("/")[-1].split(".")[0]hxs = HtmlXPathSelector(response)#获取最上面一栏的内容title = ''  #职位名companyName = '' #公司名basicInfo = hxs.select('//div[@class="fixed-inner-box"]').extract()[0] #会有两个 后面有个clone的pattern = re.compile('.*?<h1>(.*?)</h1>.*?<a.*?>(.*?)</a>.*?', re.S)findBasicInfo = re.search(pattern, basicInfo)if findBasicInfo:title = findBasicInfo.group(1).strip()  #职位名companyName = findBasicInfo.group(2).strip() #公司名#获取左侧基本公司信息 不能用正则表达式,因为有信息不全的情况 如http://jobs.zhaopin.com/297851037250005.htmcompanySize = ''  #公司规模companyType = ''  #公司性质 companyLine = ''  #公司行业companyHost = ''  #公司主页companyAddress = ''  #公司地companyInfo = hxs.select('//div[@class="company-box"]').extract()[0].encode('utf-8') #尽管只有一个,但是是列表形式,还是需要取出来if(companyInfo.find('公司规模:')>-1):companySize = companyInfo.split('公司规模:</span>')[1]companySize = companySize.split('<strong>')[1]companySize = companySize.split('</strong>')[0].strip()if(companyInfo.find('公司性质:')>-1):companyType = companyInfo.split('公司性质:</span>')[1]companyType = companyType.split('<strong>')[1]companyType = companyType.split('</strong>')[0].strip()if(companyInfo.find('公司行业:')>-1):companyLine = companyInfo.split('公司行业:</span>')[1]companyLine = companyLine.split('<strong>')[1]companyLine = companyLine.split('</a>')[0]companyLine = companyLine.split('>')[1].strip()if(companyInfo.find('公司主页:')>-1):companyHost = companyInfo.split('公司主页:</span>')[1]companyHost = companyHost.split('<strong>')[1]companyHost = companyHost.split('</a>')[0]companyHost = companyHost.split('>')[1].strip()if(companyInfo.find('公司地址:')>-1):companyAddress = companyInfo.split('公司地址:</span>')[1]companyAddress = companyAddress.split('<strong>')[1]companyAddress = companyAddress.split('</strong>')[0].strip()#获取中部工作要求信息salary = '' #职位月薪  必须先声明变量 否则会出错address = '' #工作地点jobDateTime = '' #发布日期jobCategory = '' #工作性质experience = '' #工作经验education = '' #最低学历numberInNeed = '' #招聘人数jobType = '' #职位类别jobRequirementInfo = hxs.select('/html/body/div[4]/div[1]/ul').extract()[0]pattern = re.compile('.*?<strong>(.*?)</strong>\
.*?<strong>.*?<a.*?>(.*?)</a>\
.*?<strong>.*?<span.*?>(.*?)</span>\
.*?<strong>(.*?)</strong>\
.*?<strong>(.*?)</strong>\
.*?<strong>(.*?)</strong>\
.*?<strong>(.*?)</strong>\
.*?<strong>.*?target.*?>(.*?)</a>',re.S) #前面不能有空格或者TAB 否则匹配不上findJobRequirementInfo = re.search(pattern, jobRequirementInfo)if findJobRequirementInfo:salary = findJobRequirementInfo.group(1).strip() #职位月薪address = findJobRequirementInfo.group(2).strip() #工作地点jobDateTime = findJobRequirementInfo.group(3).strip() #发布日期jobCategory = findJobRequirementInfo.group(4).strip() #工作性质experience = findJobRequirementInfo.group(5).strip() #工作经验education = findJobRequirementInfo.group(6).strip() #最低学历numberInNeed = findJobRequirementInfo.group(7).strip() #招聘人数jobType = findJobRequirementInfo.group(8).strip() #职位类别#获取描述信息detailInfo = hxs.select('//div[@class="tab-inner-cont"]').extract()jobDescribe = detailInfo[0]companyDescribe = detailInfo[1]pattern = re.compile('<.*?>|&nbsp',re.S)  #删除无用的信息jobDescribe = re.sub(pattern,'',jobDescribe).strip()  #职位描述companyDescribe = re.sub(pattern,'',companyDescribe).strip()  #公司介绍companySize = re.sub(pattern,'',companySize).strip()companyType = re.sub(pattern,'',companyType).strip()companyLine = re.sub(pattern,'',companyLine).strip()companyHost = re.sub(pattern,'',companyHost).strip()companyAddress = re.sub(pattern,'',companyAddress).strip() salary = re.sub(pattern,'',salary).strip()address = re.sub(pattern,'',address).strip()jobDateTime = re.sub(pattern,'',jobDateTime).strip()jobCategory = re.sub(pattern,'',jobCategory).strip()experience = re.sub(pattern,'',experience).strip()education = re.sub(pattern,'',education).strip()numberInNeed = re.sub(pattern,'',numberInNeed).strip()jobType = re.sub(pattern,'',jobType).strip()title = re.sub(pattern,'',title).strip()companyName = re.sub(pattern,'',companyName).strip()data = items.PageItem()data['web_id'] = "zhaopin"data['file_id'] = file_iddata['job_url'] = response.urldata['job_name'] = titledata['job_desc'] = jobDescribedata['gender'] = ""data['major'] = ""data['company_name'] = companyNamedata['job_datetime'] = jobDateTimedata['job_location'] = addressdata['work_years'] = experiencedata['edu'] = educationdata['salary'] = salarydata['company_desc'] = companyDescribedata['company_address'] = companyAddressdata['company_website'] = companyHostdata['language'] = ""data['company_worktype'] = companyLinedata['company_prop'] = companyTypedata['company_scale'] = companySize#更新任务表中抓取状态#self.jobsTool.updateCrulInfo(ConfigPropObj.liepin_webid, response.url, 1, "")return dataexcept Exception as e:print "ERROR PARSE"print response.urlprint traceback.format_exc()

转载于:https://www.cnblogs.com/dplearning/p/4925542.html

【飞谷六期】爬虫项目4相关推荐

  1. 【飞谷六期】爬虫项目1

    报名了飞谷六期的爬虫项目,但是自己相关的基础还是较弱,每天都有种无所事事的感觉.决定还是记录一下每天学习到的知识,自己看看也知道学习了些什么. 1.XShell连接阿里云,Xftp传输文件 2.把例子 ...

  2. 蒙牛六期的高度自动化物流系统

    蒙牛乳业集团成立于1999年,总部设在内蒙古呼和浩特市和林格尔县盛乐经济园区,企业总资产达76亿元,乳制品年生产能力500万吨.随着生产规模的不断扩大,自2002年起,蒙牛乳业集团开始采用自动化立体仓 ...

  3. 飞谷云六期第三组——基于Spark的机器学习

    项目正式开始时间:2015.10.15. 随笔内容:本次项目的主题是基于Spark的ML.对于ML的学习有大概半年了,正好在网上关注到了由上海交通大学所主办的这个飞谷云的大数据项目,我所报名的这期已经 ...

  4. bootstrap4 左侧导航栏 优秀 大气_志愿服务嘉年华|“持志隽永 赴愿奔行优秀志愿服务项目展示第六期...

    原标题:志愿服务嘉年华|"持志隽永 赴愿奔行"优秀志愿服务项目展示第六期 近日烟雨环绕 小楼几度听风语 可听见十一月的脚步正悄然靠近 小海狮们期盼已久的 第五届志愿服务嘉年华即将拉 ...

  5. 第三十六期:学 Java 网络爬虫,需要哪些基础知识?

    说起网络爬虫,大家想起的估计都是 Python ,诚然爬虫已经是 Python 的代名词之一,相比 Java 来说就要逊色不少.有不少人都不知道 Java 可以做网络爬虫,其实 Java 也能做网络爬 ...

  6. 【陈鹏老师精益项目实战】华东区电机企业精益生产项目第六期总结

    2021年下半年,华东区电机企业精益生产项目第六期总结启动,企业精益管理推进委员会成员.班组长及以上骨干成员,和项目运营总监陈鹏导师共同参与会议. 自2020年下半年以陈鹏导师为首的精益项目团队进驻企 ...

  7. 爬虫项目六:用Python爬下链家新房所有城市近三万条数据

    文章目录 前言 一.分析url 二.拼接url 1.实例化chrome 2.获取首字符.page 3.拼接url 三.获取房源数据 前言 本文全面解析了链家新房源数据,爬取了全部城市的房源信息,共两万 ...

  8. android仿秒拍源码,你所不知道的程序员 程序员其实真的很…【Bus Weekly】三十六期...

    原标题:你所不知道的程序员 程序员其实真的很-[Bus Weekly]三十六期 快,点击蓝色"字体"关注这个公众号,一起涨姿势- 现如今,程序员在中国的科技 圈可以说已经达到了举足 ...

  9. 《大咖说开源第二季》五、六期

    点击上方"开源社"关注我们 | 作者:叶雨秋.何莹 | 编辑:钱英宇 | 设计:宋传琪 | 责编:王玥敏 开源供应链点亮计划 - 暑期系列是由中科院软件所和 openEuler 社 ...

最新文章

  1. 【放置奇兵】算法 PVE总结
  2. 30岁学python编程_朋友问我,你都30岁了学编程来得及吗
  3. 河北移动引入物联网4G专网APN提升交警执法效率
  4. 图像处理 花屏_滴滴开源的 AoE:工程实践中的图像处理
  5. 4-1k近邻算法(k-Nearest Neighbors)
  6. Android 系统(167)----OTA升级常见问题
  7. DELPHI存储过程调用
  8. java hashset 实现_HashSet实现原理分析(Java源码剖析)
  9. Node的textContent属性
  10. 折线图后面无数据_老板让数据师分析二八法则,此图表完美解决,项目管理师专用图表...
  11. H3C交换机创建用户
  12. ITSM群(48132184)讨论精选
  13. 苹果手机测距离_3D传感市场要变天!苹果力推之下,dToF将成新风口!
  14. visio添加箭头图标
  15. java文本文档统计字数,行走目录时字数统计PDF文件
  16. centos7下安装airflow
  17. 八行代码一键照片转漫画
  18. LaTex绘制跨行跨列的三线表
  19. 计算机专业可以谈手机吗,手机真能取代电脑吗?谈手机/PC系统的大一统
  20. 【笔记】ARM架构和ARM芯片(三)

热门文章

  1. 汇编语言写c51延时程序,单片机延时程序实例讲解
  2. linuxpython拍照_linux下python抓屏实现方法 -电脑资料
  3. Codeforces #471
  4. Atitit.java swing打印功能 api  attilax总结
  5. sublime text3下BracketHighlighter的配置方法
  6. TinyFrame升级之五:全局缓存的设计及实现
  7. 几种常用的数字滤波器
  8. linux/unix下setuid/seteuid/setreuid/setresuid
  9. 查看linux的用户
  10. Android 串口开发,发送串口命令,读卡,反扫码,USB通讯,实现demo。——持续更新