Python实现的文轩网爬虫完整示例


Posted in Python onMay 16, 2019

本文实例讲述了Python实现的文轩网爬虫。分享给大家供大家参考,具体如下:

encoding=utf8
import pymysql
import time
import sys
import requests
import os
#捕获错误
import traceback
import types
#将html实体化
import cgi
import warnings
reload(sys)
sys.setdefaultencoding('utf-8')
from pyquery import PyQuery as pq
from lxml import etree
sys.setdefaultencoding('utf-8')
#屏蔽错误
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):
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)
#解析详情页面
try:
detail_url=final_url
c=pq(detail_url)
head=c('html').attr('xmlns')
err='http://www.w3.org/1999/xhtml'
err1='http://www.winxuan.com/cms/2016db_sh'
if head == err or head == err1:
return 'back'
except Exception, e:
return 'back'
i=0
while i<12:
  text = c('#page').find('.cont').find('li').eq(i).text()
  text=text.replace(' ','')
  if 'I S B N' in text:
    isbn=text.replace('I S B N:','')
    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 'isbn:' in text :
    isbn=text.replace('isbn:','')
    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 :
    author = text.replace('作者:','')
  if '出版社:' in text :
    press_name=text.replace('出版社:','')
  if '版次:' in text :
    edition=text.replace('版次:','')
  if '印次:' in text :
    impressions=text.replace('印次:','')
  if '装帧:' in text :
    packaging=text.replace('装帧:','')
  if '开本:' in text:
    size=text.replace('开本:','')
  if '出版时间:' in text:
    press_time=text.replace('出版时间:','')
    press_time=press_time.strip()
    if press_time == '无':
      press_time='1970-01-01'
  if '印刷时间:' in text:
    print_time=text.replace('印刷时间:','')
    print_time=print_time.strip()
    if print_time== '无':
      print_time='1970-01-01'
  if '页数:' in text:
    page_num=text.replace('页数:','')
  if '字数:' in text:
    word_num=text.replace('字数:','')
  i+=1
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 = ''
#暂无图片地址
none_img='http://static.winxuancdn.com/goods/sml_blank.jpg'
#获取大小图地址
big_path=c('.info-side').find('.img').find('a').find('img').attr('src')
if big_path is None:
  return 'back'
elif big_path == none_img :
  big_path=''
  small_path=''
else :
  small_path=big_path.replace('_16','_11')
#获取分类
#先获取a标签html
ahtml=c('#page').find('.base-nav').eq(0).html()
#解析a标签html
cate=pq(ahtml)
#获取分类的最后一个分类
category=cate('a:last').text()
#获取书名
name=c('.info-main').find('.name').eq(0).find('h1').eq(0).text()
name=name.strip()
#获取价格
price=c('.info-main').find('.attr').eq(0).find('.price-n').eq(0).find('b').text()
price=price.replace('¥','')
#循环获取内容简介和目录信息
k=5
while k<12:
  title=c('#page').find('.title').eq(k).find('.tab').find('h4').text()
  if '内容简介' in title:
    con=c('#page').find('.title').eq(k).nextAll()
    det=pq(con)
    content=det('.text-words-1').html()
    content=content.encode("utf8", "ignore");
  if '目录' in title:
    con=c('#page').find('.title').eq(k).nextAll()
    dry=pq(con)
    directory=dry('.text-words-1').html()
    directory=directory.encode("utf8", "ignore");
  k+=1
#如果内容简介和目录没有的时候指定为空字符串
if ('content' in locals().keys())== False:
  content = ''
if ('directory' in locals().keys())== False:
  directory = ''
details  = '内容简介<br>'+content+'<br><br>目录<br>'+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 = 3
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,word_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,word_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,%s)"
  aaa=cursor.execute(sql,li)
  if aaa==1:
    print u'插入成功'
  conn.commit()
except Exception, e :
  return 'back'
def winxuan(n):
#首页解析
home_url='http://www.winxuan.com/'
h=pq(home_url)
#分类导航链接
menu=h('.mod-mainmenu').find('dd').find('a').eq(n).attr('href')
#print menu
#分类书籍首页
try:
mh=pq(menu)
except Exception, e :
return 'backs'
# text=mh('.main').find('a').text()
# text=text.encode("GBK", "ignore");
li=[]
u=0
while u<248 :
detail_urls=mh('.main').find('a').eq(u).attr('href')
#将取到所有地址放入到列表当中
li.append(detail_urls)
u+=1
#进行列表去重
li=list(set(li))
for final_url in li:
try:
result=getData(final_url)
except Exception, e :
continue
if result=='back' :
continue
print 'OK,finished'
n=0
while n<58:
while n<58:
print n
string=str(n)
file_open=open('./number.txt', 'w')
file_open.write(string)
file_open.close()
res=winxuan(n)
n+=1
if res=='backs' :
continue

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python解析html开发库pyquery使用方法
Feb 07 Python
详解使用 pyenv 管理多个版本 python 环境
Oct 19 Python
python实现随机梯度下降法
Mar 24 Python
Python运行不显示DOS窗口的解决方法
Oct 22 Python
python变量的存储原理详解
Jul 10 Python
flask框架配置mysql数据库操作详解
Nov 29 Python
Python&amp;&amp;GDAL实现NDVI的计算方式
Jan 09 Python
python利用opencv实现SIFT特征提取与匹配
Mar 05 Python
anaconda升级sklearn版本的实现方法
Feb 22 Python
python某漫画app逆向
Mar 31 Python
pycharm部署django项目到云服务器的详细流程
Jun 29 Python
关于的python五子棋的算法
May 02 Python
计算机二级python学习教程(2) python语言基本语法元素
May 16 #Python
计算机二级python学习教程(1) 教大家如何学习python
May 16 #Python
详解Python传入参数的几种方法
May 16 #Python
[机器视觉]使用python自动识别验证码详解
May 16 #Python
Python redis操作实例分析【连接、管道、发布和订阅等】
May 16 #Python
Python操作redis实例小结【String、Hash、List、Set等】
May 16 #Python
Python 实现数据结构中的的栈队列
May 16 #Python
You might like
用PHP开发GUI
2006/10/09 PHP
PHP数组交集的优化代码分析
2011/03/06 PHP
实例讲解PHP页面静态化
2018/02/05 PHP
NodeJS框架Express的模板视图机制分析
2011/07/19 NodeJs
用JS实现一个TreeMenu效果分享
2011/08/28 Javascript
jQuery EasyUI API 中文文档 - Documentation 文档
2011/09/29 Javascript
浅谈Javascript事件模拟
2012/06/27 Javascript
JavaScript比较两个对象是否相等的方法
2015/02/06 Javascript
[原创]Javascript 实现广告后加载 可加载百度谷歌联盟广告
2016/05/11 Javascript
带有定位当前位置的百度地图前端web api实例代码
2016/06/21 Javascript
js 声明数组和向数组中添加对象变量的简单实例
2016/07/28 Javascript
详解Jquery Easyui的验证扩展
2017/01/09 Javascript
JavaScript使用简单正则表达式的数据验证功能示例
2017/01/13 Javascript
node.js实现登录注册页面
2017/04/08 Javascript
基于JS对象创建常用方式及原理分析
2017/06/28 Javascript
浅谈从React渲染流程分析Diff算法
2018/09/08 Javascript
ExtJs使用自定义插件动态保存表头配置(隐藏或显示)
2018/09/25 Javascript
jQuery表单校验插件validator使用方法详解
2020/02/18 jQuery
[56:41]iG vs Winstrike 2018国际邀请赛小组赛BO2 第二场
2018/08/17 DOTA
Python使用scrapy采集数据时为每个请求随机分配user-agent的方法
2015/04/08 Python
python开发之tkinter实现图形随鼠标移动的方法
2015/11/11 Python
利用Python获取操作系统信息实例
2016/09/02 Python
The Hut美国/加拿大:英国领先的豪华在线百货商店
2019/03/26 全球购物
宝拉珍选英国官网:Paula’s Choice英国
2019/05/29 全球购物
自我评价怎么写好呢?
2013/12/05 职场文书
护士进修自我鉴定
2014/02/07 职场文书
销售团队激励口号
2014/06/06 职场文书
2013年最新自荐信范文
2014/06/23 职场文书
教师演讲稿开场白
2014/08/25 职场文书
党支部反对四风思想汇报
2014/10/10 职场文书
学生检讨书范文
2014/10/30 职场文书
2014镇党委书记党建工作汇报材料
2014/11/02 职场文书
2014年学校后勤工作总结
2014/12/06 职场文书
为什么中国式养孩子很累?
2019/08/07 职场文书
Python操作CSV格式文件的方法大全
2021/07/15 Python
MySQL 表锁定 LOCK和UNLOCK TABLES的 SQL语法
2022/04/18 MySQL