成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

如何使用Python實(shí)現(xiàn)的文軒網(wǎng)爬蟲-創(chuàng)新互聯(lián)

這篇文章主要介紹了如何使用Python實(shí)現(xiàn)的文軒網(wǎng)爬蟲,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括天津網(wǎng)站建設(shè)、天津網(wǎng)站制作、天津網(wǎng)頁制作以及天津網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,天津網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到天津省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

具體如下:

encoding=utf8
import pymysql
import time
import sys
import requests
import os
#捕獲錯(cuò)誤
import traceback
import types
#將html實(shí)體化
import cgi
import warnings
reload(sys)
sys.setdefaultencoding('utf-8')
from pyquery import PyQuery as pq
from lxml import etree
sys.setdefaultencoding('utf-8')
#屏蔽錯(cuò)誤
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)
#根據(jù)詳情頁地址抓取數(shù)據(jù)并插入數(shù)據(jù)庫(kù)
def getData(final_url):
file_open=open('./url.txt', 'w')
file_open.write(final_url)
file_open.close()
#鏈接數(shù)據(jù)庫(kù)
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='bookinfo', charset='utf8')
#設(shè)置浮標(biāo)
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 '出版時(shí)間:' in text:
    press_time=text.replace('出版時(shí)間:','')
    press_time=press_time.strip()
    if press_time == '無':
      press_time='1970-01-01'
  if '印刷時(shí)間:' in text:
    print_time=text.replace('印刷時(shí)間:','')
    print_time=print_time.strip()
    if print_time== '無':
      print_time='1970-01-01'
  if '頁數(shù):' in text:
    page_num=text.replace('頁數(shù):','')
  if '字?jǐn)?shù):' in text:
    word_num=text.replace('字?jǐn)?shù):','')
  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='https://cache.yisu.com/upload/information/20200622/113/35179.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標(biāo)簽html
ahtml=c('#page').find('.base-nav').eq(0).html()
#解析a標(biāo)簽html
cate=pq(ahtml)
#獲取分類的最后一個(gè)分類
category=cate('a:last').text()
#獲取書名
name=c('.info-main').find('.name').eq(0).find('h2').eq(0).text()
name=name.strip()
#獲取價(jià)格
price=c('.info-main').find('.attr').eq(0).find('.price-n').eq(0).find('b').text()
price=price.replace('&yen;','')
#循環(huán)獲取內(nèi)容簡(jiǎn)介和目錄信息
k=5
while k<12:
  title=c('#page').find('.title').eq(k).find('.tab').find('h5').text()
  if '內(nèi)容簡(jiǎn)介' 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
#如果內(nèi)容簡(jiǎn)介和目錄沒有的時(shí)候指定為空字符串
if ('content' in locals().keys())== False:
  content = ''
if ('directory' in locals().keys())== False:
  directory = ''
details  = '內(nèi)容簡(jiǎn)介<br>'+content+'<br><br>目錄<br>'+directory
details=cgi.escape(details)
#錄入時(shí)間
add_time = time.strftime('%Y-%m-%d',time.localtime(time.time()))
#下載小圖
#文件根目錄
root_path=sys.path[0]
#創(chuàng)建isbn文件夾路徑
root_path=root_path.replace('\\','/')
isbn_path=root_path+'/download/'+isbn
if big_path != '' and small_path !='' :
  #創(chuàng)建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"
    #調(diào)用下載圖片方法
    small_res=dowloadPic(small_path,down_img_small)
    #大圖保存數(shù)據(jù)庫(kù)路徑
    big_res=dowloadPic(big_path,down_img_big)
    #小圖保存數(shù)據(jù)庫(kù)路徑
    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 :
    #組合保存數(shù)據(jù)庫(kù)中的圖片路徑
    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]
  #執(zhí)行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)
#分類導(dǎo)航鏈接
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')
#將取到所有地址放入到列表當(dāng)中
li.append(detail_urls)
u+=1
#進(jìn)行列表去重
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

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何使用Python實(shí)現(xiàn)的文軒網(wǎng)爬蟲”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司,關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、網(wǎng)站設(shè)計(jì)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

當(dāng)前題目:如何使用Python實(shí)現(xiàn)的文軒網(wǎng)爬蟲-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://jinyejixie.com/article36/dcgdpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、營(yíng)銷型網(wǎng)站建設(shè)軟件開發(fā)、商城網(wǎng)站、定制網(wǎng)站、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管
建湖县| 象山县| 包头市| 盐边县| 慈溪市| 图们市| 宿松县| 顺平县| 阿图什市| 榆社县| 慈溪市| 图们市| 和硕县| 西乡县| 仙居县| 尼勒克县| 东莞市| 辽阳市| 亚东县| 霍州市| 濮阳市| 宁南县| 闻喜县| 英德市| 隆林| 根河市| 固镇县| 兴文县| 石泉县| 比如县| 乌兰浩特市| 波密县| 噶尔县| 安康市| 眉山市| 周口市| 舞阳县| 太谷县| 彩票| 河西区| 仁布县|