encoding=utf8
import pymysql
import time
import sys
import requests
import os
import types
将html实体化
import cgi
import urllib2
import warnings
正则
import re
reload(sys)
sys.setdefaultencoding('utf-8')
from pyquery import PyQuery as pq
from lxml import etree
屏蔽错误
warnings.filterwarnings("ignore")
下载图片
def dowloadPic(imageUrl,filePath):
r = requests.get(imageUrl,timeout=60)
status=r.status_code
if status == 404:
return 404
with open(filePath, "wb") as code:
code.write(r.content)
def getData(final_url):
print final_url
file_open=open('./url.txt', 'w')
file_open.write(final_url)
file_open.close()
#链接数据库
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='bookinfo', charset='utf8')
#设置浮标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
#插入数据库来源地址
detail_url=final_url
r=requests.get(final_url)
d=pq(r.content.decode('gbk',"ignore"))
#获取大图地址
none_img='http://images.zxhsd.com/photo/book_m//nologo.gif'
#大图地址前缀
bigtop='http://images.zxhsd.com/photo/book_m'
#小图地址前缀
smalltop='http://www.zxhsd.com/photo/book_m'
big_path=d('.pic').find('a').attr('href')
if big_path is None:
return 'back'
elif big_path == none_img:
big_path=''
small_path=''
else:
small_path=big_path.replace(bigtop,smalltop)
#获取a标签html
ahtml=d('.top').html()
#解析a标签HTML
cate=pq(ahtml)
#获取分类的最后一个分类
#获取最后一个标签必须先获取a标签的HTML解析后才能这样写
category=cate('a:last').text()
#获取书名
name=d('.topbarcon').find('h3').eq(0).text()
i=0
while i<3:
k=0
while k<4:
text=d('.ProductsInfo').find('ul').eq(i).find('li').eq(k).text()
#将获取的内容转换成字符串类型
text=str(text)
#获取isbn
if 'ISBN:' in text:
isbn=text.replace('ISBN:','')
if isbn == None or isbn=='':
return 'back'
isbn=isbn.strip()
sel='select count() from bi_book where isbn ='+isbn
cursor.execute(sel)
result=cursor.fetchone()
count=result['count()']
if count != 0 :
print u'已存在'
return 'back'
#获取价格
if '定价:' in text:
price=text.replace('定价: ¥','')
price=price.replace(' ','').encode('utf8')
if '作者:' in text:
author=text.replace('作者:','')
author=author.strip().encode('utf8')
#print author
if '出版社:' in text:
press_name=text.replace('出版社:','')
press_name=press_name.strip().decode('utf8')
if '页数:' in text:
page_num=text.replace('页数:','')
page_num=page_num.strip().decode('utf8')
if '开 本:' in text:
#开本和包装在一起获取总共数据
cont=text.replace('开 本:','')
cont=cont.replace(' ','')
#获取开本
size=cont[0:5]
#获取包装
packaging=cont.replace(size,'').decode('utf8')
size=size.decode('utf8')
#获取出版时间
#正则匹配 search冲全部字符串中找 match只找第一个
press_res=re.search(r'第\d版', text)
if press_res:
#获取版次
edition=press_res.group()
#要解析成utf8不然乱码和真是字符串不一样替换不了
edition=edition.decode('utf8')
text=text.decode('utf8')
press_time=text.replace(edition,'')
press_time=press_time.strip()
#获取印刷时间
#正则匹配 search冲全部字符串中找 match只找第一个
print_res=re.search(r'第\d次印刷', text)
if print_res:
#获取印次
impressions=print_res.group()
#要解析成utf8不然乱码和真是字符串不一样替换不了
impressions=impressions.decode('utf8')
text=text.decode('utf8')
print_time=text.replace(impressions,'')
#截取掉首尾空格
print_time=print_time.strip()
k+=1
i+=1
if ('isbn' in locals().keys()) == False:
return 'back'
if ('author' in locals().keys()) == False:
author = ''
if ('press_time' in locals().keys()) == False:
press_time = '1970-01-01'
if ('print_time' in locals().keys()) == False:
print_time = '1970-01-01'
if ('impressions' in locals().keys()) == False:
impressions = ''
if ('edition' in locals().keys())== False:
edition = ''
if ('page_num' in locals().keys())== False:
page_num = ''
# if ('word_num' in locals().keys())== False:
# word_num = ''
if ('packaging' in locals().keys())== False:
packaging = ''
if ('size' in locals().keys())== False:
size = ''
if ('press_name' in locals().keys())== False:
press_name = ''
y=0
while y<8:
title=d('.t').find('.title').eq(y).text()
text=str(title)
if '目录' in text:
directory=d('.t').find('p').eq(y).html()
if directory==None:
directory=''
else:
directory=directory.encode("utf8", "ignore")
if '内容提要' in text:
content=d('.t').find('p').eq(y).html()
if content==None:
content=''
else:
#截取掉首尾空格
content=content.strip()
content=content.encode("utf8", "ignore")
y+=1
#如果内容简介和目录没有的时候指定为空字符串
if ('content' in locals().keys())== False:
content = ''
if ('directory' in locals().keys())== False:
directory = ''
if ('content' in locals().keys())== False and ('directory' in locals().keys())== False:
details=''
else:
a='内容简介<br>'.encode("utf8", "ignore")
b='<br><br>目录<br>'.encode("utf8", "ignore")
details=a+content+b+directory
details=cgi.escape(details)
#录入时间
add_time = time.strftime('%Y-%m-%d',time.localtime(time.time()))
#下载小图
#文件根目录
root_path=sys.path[0]
#创建isbn文件夹路径
root_path=root_path.replace('\\','/')
isbn_path=root_path+'/download/'+isbn
if big_path != '' and small_path !='' :
#创建isbn目录
if os.path.isdir(isbn_path) ==False :
os.mkdir(isbn_path)
#组合下载后图片保存路径
down_img_small = isbn_path+"/small"+isbn+".jpg"
down_img_big = isbn_path+'/big'+isbn+".jpg"
#调用下载图片方法
small_res=dowloadPic(small_path,down_img_small)
#大图保存数据库路径
big_res=dowloadPic(big_path,down_img_big)
#小图保存数据库路径
if small_res==404 :
img_small = 'none-picture/none-small.jpg'
else :
img_small = 'download/'+isbn+'/small'+isbn+'.jpg'
if big_res==404 :
img_big = 'none-picture/none-big.jpg'
else :
img_big = 'download/'+isbn+'/big'+isbn+'.jpg'
else :
#组合保存数据库中的图片路径
img_small = 'download/'+isbn+'/small'+isbn+'.jpg'
img_big = 'download/'+isbn+'/big'+isbn+'.jpg'
else :
img_big = 'none-picture/none-big.jpg'
img_small = 'none-picture/none-small.jpg'
source_type = 5
try :
#要插入的列表
li=[0,source_type,category,details,detail_url,price,add_time,packaging,print_time,impressions,name,author,press_name,isbn,edition,size,press_time,page_num,img_big,img_small]
#执行sql
sql="insert into bi_book (book_id,source_type,category,details,detail_url,price,add_time,packaging,print_time,impressions,name,author,press_name,isbn,edition,size,press_time,page_num,img_big,img_small) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
aaa=cursor.execute(sql,li)
if aaa==1:
print u'插入成功'
conn.commit()
except Exception, e :
return 'back'
p=3757000
while p<3758429:
p=0
while p<1:
#string=str(p)
home_url="http://www.zxhsd.com/kgsm/ts/2017/03/04/3760868.shtml"
# file_open=open('./number.txt', 'w')
# file_open.write(string)
# file_open.close()
getData(home_url)
p+=1