Пример #1
0
def singleBookSave(subcategory,log,b,itemId):
	"""***单本书函数:单本书籍的抓取、匹配、最后保存到数据库***"""
	
	#--得到单本书的信息,并保存到数据库--#
	imgUrl=getGroup(b,'ImgUrl')																		#取出封面小图地址
	buyUrl=getGroup(b,'Url')																			#取出购买地址
	title=b.group('Title')																				#取出书名
	if title:
		title=title.decode('GBK')																		#如果title存在,进行转码
	author=b.group('Author')																		#取出作者
	if author:
		author=author.decode('GBK')																#如果author存在,进行转码
	publisher=b.group('Publisher')																	#取出出版社
	if publisher:
		publisher=publisher.decode('GBK')															#如果publisher存在,进行转码
	date=b.group('Date')																				#取出出版日期
	if date:
		date=date[10:].decode('GBK')																#如果date存在,去掉“出版日期:” 四个字,并转码为UTF-8
	price=getGroup(b,'Price')																		#取出当当价格,并转码
	oprice=getGroup(b,'OPrice')																		#取出原价,并转码
	state=getGroup(b,'State')																		#取出货存状态,并转码
	
	#--如果得到的state为'link_sale',则有货,赋值为1,否则无货,赋值为0--#
	if state=='link_sale':
		state=1
	else:
		state=0
		
	#--记录当前事件--#
	log.currentEvent='Catch bookDetail_2'
	log.save()
	
	detailHtml=getHtml(buyUrl)																		#得到当前书籍的购买网页数据
	try:
		bookDetail_2=compiledDetailPatterns_2.search(detailHtml)									#抓取书的另一部分信息
		edition=bookDetail_2.group('Edition')[11:].decode('GBK')										#取出版次,去掉“版次:”,并转码
		totalPage=bookDetail_2.group('TPage')[11:].decode('GBK')									#取出总页数,去掉“页数:”,并转码
		format=bookDetail_2.group('Format')[11:].decode('GBK')										#取出开本,去掉“开本:”,并转码
		isbn=bookDetail_2.group('ISBN')[13:].decode('GBK')											#取出ISBN,去掉“ISBN:”,并转码
		pack=bookDetail_2.group('Pack')[11:].decode('GBK')											#取出装帧,去掉“装帧:”,并转码
		bigImgUrl=bookDetail_2.group('BigImgUrl').decode('GBK')										#得到封面大图地址,并转码
		captionData=compiledCaptionPatterns.search(detailHtml)                      					#抓取内容简介,并转码
		if captionData:                                                                            						#如果匹配到简介,则取出
			try:
				caption=captionData.group('Caption').decode('GBK')									#对内容简介进行编码转换
			except UnicodeDecodeError:                                                   						#如果转换编码的过程中捕捉到UnicodeDecodeError,将caption赋值为None
				caption=None
		else: 
			caption=None                                                                        						#如果没有匹配到简介,则赋值为None
	except AttributeError:
		edition='暂无'
		totalPage='暂无'
		format='暂无'
		isbn='暂无'
		pack='暂无'
		bigImgUrl='暂无'
		caption='暂无'
	
	#--保存数据到数据库--#
	bdetail=BookDetail(title=title,buyUrl=buyUrl,price=price,oprice=oprice,
						state=state,caption=caption,imgUrl=imgUrl,bigImgUrl=bigImgUrl,
						author=author,publisher=publisher,date=date,edition=edition,
						totalPage=totalPage,format=format,isbn=isbn,pack=pack,
						subCategory=subcategory.title,topCategory=subcategory.topcategory.title,siteId=subcategory.siteId)
	try:					
		bdetail.save()                                                                              					#保存到数据库
	except DataError:
		pass																							#如果捕捉到DataError,将其忽略,继续抓取
	log.breakItemId=itemId                                                                						#在Log里记录下当前序号
	log.errorInfo='No Exception Raise'                                                 						#书籍保存成功,无异常发生
	log.currentEvent='Save Books Success'                                          						#当前事件为保存书籍成功
	log.save()                                                                                   						#保存Log
Пример #2
0
def singleBookSave(subcategory,log,bd1,itemId):
	"""***单本书函数:单本书籍的抓取、匹配、最后保存到数据库***"""
	
	#--预先赋值需要的基本信息,用于后面的if判断--#
	P='出版社:'
	E='版本:'
	D='出版日期:'
	I='ISBN:'
	Z='装帧:'
	F='开本:'
	TP='页码:'
	
	#--获取书籍部分信息--#
	imgUrl=bd1.group('ImgUrl')																			#取出封面小图地址
	buyUrl=bd1.group('BuyUrl') 																			#取出购买地址
	title=bd1.group('Title') 																				#取出图书名称
	state=bd1.group('State') 																			#取出货存状态
	#--如果得到的state为‘现在有货。’,则给state赋值1,否则为0--#
	if state=='现在有货。':
		state=1
	else:
		state=0
	#--记录Log--#
	log.currentEvent='Catch bookDetail_2'																#记录当前事件
	log.save()
	
	detailHtml=getHtml(buyUrl)																			#得到当前书籍购买地址对应页面的HTML内容
	bookDetail_2=compiledDetailPatterns_2.search(detailHtml) 										#对当前书籍信息2进行匹配
	if bookDetail_2:
		author=bookDetail_2.group('Author') 															#取出作者
		bigImgUrl=bookDetail_2.group('BigImgUrl') 														#取出封面大图地址
	else:
		author=None
		bigImgUrl='nothing'
	opricedata=compiledOpricePatterns.search(detailHtml)												#匹配原价
	if opricedata:																							#如果匹配到
		oprice=opricedata.group('OPrice')  																#取出原价
	else:																									#如果没有匹配到
		oprice='无'																						#赋值为‘无’
	pricedata=compiledPricePatterns.search(detailHtml)												#匹配卓越价(同原价)
	if pricedata:
		price=pricedata.group('Price')
	else:
		price='无'
	bookDetail_3=compiledInfoPatterns.finditer(detailHtml) 											#对书籍基本信息进行匹配,得到所有基本信息的迭代器
	dic={}  																								#创建一个字典
	for bd3 in bookDetail_3:																				#对基本信息迭代器进行循环
		infoTitle=bd3.group('InfoTitle')
		info=bd3.group('Info')
		dic[infoTitle]=info																					#得到一个包含所有基本信息的字典,后面将取出我们需要的基本信息
	publisher=getInfo(P,dic)																				#调用getInfo函数得到出版社
	date=getInfo(D,dic)																					#调用getInfo函数得到出版日期
	edition=getInfo(E,dic)																				#调用getInfo函数得到版次
	isbn=getInfo(I,dic)																					#调用getInfo函数得到ISBN
	format=getInfo(F,dic)																				#调用getInfo函数得到开本
	pack=getInfo(Z,dic)																					#调用getInfo函数得到装帧
	totalPage=getInfo(TP,dic)																			#调用getInfo函数得到总页数
	captionData=compiledCaptionPatterns.search(detailHtml)  										#匹配内容简介
	if captionData:																							#如果匹配成功
		caption=captionData.group('Caption')  															#取出内容简介
	else:																									#如果匹配不成功
		caption=None  																					#如果内容简介不存在,caption为None
		
	#--保存数据到数据库--#
	bdetail=BookDetail(title=title,buyUrl=buyUrl,price=price,oprice=oprice,
						state=state,caption=caption,imgUrl=imgUrl,bigImgUrl=bigImgUrl,
						author=author,publisher=publisher,date=date,edition=edition,
						totalPage=totalPage,format=format,isbn=isbn,pack=pack,
						subCategory=subcategory.title,topCategory=subcategory.topcategory.title,siteId=subcategory.siteId)
	try:
		bdetail.save()
	except (DataError,Warning):
		pass
	#---记录Log-#
	log.breakItemId=itemId                                                                							#在Log里记录下当前序号
	log.errorInfo='No Exception Raise'                                                 							#书籍保存成功,无异常发生
	log.currentEvent='Save Books Success'                                          							#当前事件为保存书籍成功
	log.save()                                                                                   							#保存Log