侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   220

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

encoding=utf8import pymysqlimport timeimport sysimport requestsimport os#捕获错误import tracebackimport types#将html实体化import cgiimport warningsreload(sys)sys.setdefaultencoding('utf-8')from pyquery import PyQuery as pqfrom lxml import etreesys.setdefaultencoding('utf-8')#屏蔽错误warnings.filterwarnings("ignore")#下载图片def dowloadPic(imageUrl,filePath):r = requests.get(imageUrl,timeout=60)status=r.status_codeif status == 404:return 404with 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_urlc=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=0while 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+=1if ('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标签htmlahtml=c('#page').find('.base-nav').eq(0).html()#解析a标签htmlcate=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=5while 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>'+directorydetails=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/'+isbnif 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 = 3try :  #要插入的列表  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=0while 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 :continueif result=='back' :continueprint 'OK,finished'n=0while n<58:while n<58:print nstring=str(n)file_open=open('./number.txt', 'w')file_open.write(string)file_open.close()res=winxuan(n)n+=1if res=='backs' :continue

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

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


  • 上一条:
    Python实现爬取亚马逊数据并打印出Excel文件操作示例
    下一条:
    计算机二级python学习教程(2) python语言基本语法元素
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客