找回密码
 注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 1179|回复: 0

Python + pyspider某小说站的爬虫,入数据库,火车头发布,资...

[复制链接]
发表于 2019-6-8 23:06:07 | 显示全部楼层 |阅读模式
Python + pyspider某小说站的爬虫,入数据库,火车头发布,资源下载到本地,另可写爬虫!
# L# Z: j$ c, m8 [5 P1 l4 @' |
  1. #!/usr/bin/env python
    1 b8 s% Z0 k5 P2 @# x
  2. # -*- encoding: utf-8 -*-
    % i+ K- ~: M% j% P* V. v0 p# r% Y
  3. # Created on 2019-05-05 21:43:11
    : O/ B0 a3 `* [  B+ W% y
  4. # Project: XiaoShuo
    # S  ]6 y* X9 K- D9 t0 u, n

  5. % U3 n' Z/ j& d
  6. from pyspider.libs.base_handler import *5 k% |! X) q" G7 P9 ~
  7. import pymysql
    3 l( ]: }9 I7 x# d! {; L4 ~
  8. import random
    * J$ [- J' g( ?' f6 M
  9. import datetime8 Z4 C0 \% E; F' ~% y
  10. import urllib2,HTMLParser,re$ H9 O4 O& H; R% E  t+ {
  11. import os
    2 J0 Z" F% {; w# O* `: r
  12. import sys
    2 p' b+ ~: L3 I8 T# ^1 n- s% M4 c
  13. import re, S* y+ l/ P: k5 s* q% ?0 m
  14. import codecs! l: ~; u/ M2 D8 Y* p, m: S
  15. import requests
    2 M4 ^, B, o3 e2 K7 ^3 e; I
  16. import json1 c# B1 i) R2 Z' I- l* A3 [- s
  17. - z, y7 v9 C1 X
  18. class Handler(BaseHandler):
    , a. k; m, {" U5 V! T2 S
  19.     global Datos2 w1 l5 d1 ^( w, G8 f; K0 F- j6 J
  20.     global P_dir   
    , |1 E" c9 X7 [+ D4 W2 p
  21.     P_dir = '/Tools/Debug/'  #采集时候图片保持到本地的路径% N! v4 |" L0 n: d
  22.     global Datos6 O! \0 A( o7 S; }' M5 A# [8 {5 c
  23.     Datos = {}
    0 U; |( h! @* `) p
  24.     headers= {
    2 k* ?4 E3 _1 F3 z# g) o6 t  m
  25.     'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',7 f. J+ i/ C; G! N. n( T; [
  26.     'Accept-Encoding':'gzip, deflate, sdch',
    ! Y: i: }& ]( \
  27.     'Accept-Language':'zh-CN,zh;q=0.8',2 B& [. d2 B6 e* h2 \" L
  28.     'Cache-Control':'max-age=0',& w  K- a4 ^) H
  29.     'Connection':'keep-alive',
    8 T) \  n7 L* }# q
  30.     'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'
    ; w, U8 y" F6 |8 D
  31.     }: u1 T, ?+ w$ O% M7 G1 i+ n
  32.     crawl_config = {8 M" m  ?1 G. O2 u6 g
  33.         'headers' : headers,: b3 O7 ?% j6 ]! `6 H8 X
  34.         'timeout' : 3006 X# l9 o* }  R! p4 S6 b
  35.     }5 ~" f4 }3 v$ u- F# d
  36.     def add_BookFile(self,Bookname, BookIDs, img, Locaimg, Book_Dates):
    7 V" {& z: ?$ C- L8 `4 u
  37.         db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8")
    - e( m6 N" r0 ~; a8 t" q
  38.         try:
    ' G/ L6 m+ k9 E
  39.             cursor = db.cursor()
    3 ?6 ]. _# @2 }/ F  n. f; P+ L9 E- p
  40.             #注意此处字符串的占位符要加双引号"%s"3 F) X  ?; P3 j9 I/ @" }: `( Y
  41.             sql = 'insert into BookFile(Bookname, BookID, img, Locaimg, Book_Date) values ("%s","%s","%s","%s","%s")' % (Bookname, BookIDs, img, Locaimg, Book_Dates);
    0 q: z2 F0 N* O- {9 \
  42. #            print(sql)+ r& ]  g2 r9 Y4 Y
  43.             cursor.execute(sql)
    $ ?) T3 Z1 L9 |# ]+ O
  44.             " F$ a. _  d2 O5 P4 O
  45.             #qid = cursor.lastrowid
      W) R5 z- Z- z9 ~) ?8 B2 ^
  46.             #print(qid)
    - U: h$ S4 ~( w5 w( ]
  47.             
    ( A' U5 k- Q2 v/ n
  48.             db.commit()
    : H$ Q4 n) @7 c, ?' S' @
  49.         except Exception as err:. b& V2 A, g% |! {# z* |
  50.             print("Error %s for execute sql: %s" % (err, sql))
    ( d8 e. x5 f5 h) ~1 H
  51.             db.rollback()  D; M4 {3 ~$ s. |  @, Q5 _
  52.     def add_comment(self,Bookname, Booktitle, BookID, Titleid, Book_Date):
    9 i3 Y1 H, `& Z, R' J
  53.         db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8")# m9 c" D3 {* f3 P4 c
  54.         try:6 O9 U4 s5 A( [: H
  55.             cursor = db.cursor()  c% i& E2 R! ^3 [5 h! q8 C! w
  56.             #注意此处字符串的占位符要加双引号"%s"
    ! k' O( k6 U5 d- j
  57.             sql = 'insert into BookTitle(Bookname, Booktitle, BookID, Titleid, Book_Date) values ("%s","%s","%s","%s","%s")' % (Bookname, Booktitle, BookID, Titleid, Book_Date);
    / D7 i6 w' R! i% M( U
  58. #            print(sql)2 K3 B& {/ l" R
  59.             cursor.execute(sql): N* U# J; u1 N0 j( o& V6 [" A- |% O1 ~3 n
  60.             " ]* {$ F3 X1 l
  61.             #qid = cursor.lastrowid
    8 C* `6 t! G7 R* S
  62.             #print(qid)# L/ }, m) m5 e. h
  63.             
    " w# g- t" M) m, y- V" L
  64.             db.commit(): c2 p" O* u2 u* I0 t
  65.         except Exception as err:
    ) Q6 Z4 A& l- o$ i5 ^' |! I# i
  66.             print("Error %s for execute sql: %s" % (err, sql))/ U: l/ o; y! o0 O8 m
  67.             db.rollback()
      p5 F1 X0 C! V
  68.     def add_question(self,Bookname,Cater_Name,Book_author,Book_Introduction,Book_Synopsis,Book_Palabras,Bookurl,Booktitle,BookID,BookConte,Titleid,abover,Book_Date):
    % f7 n! r, E0 }- w3 z0 F5 J; |
  69.         db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8")
    ; B% u7 D8 S" g% f
  70.         try:+ p& q' F4 t2 s! j& r4 E; K1 d
  71.             cursor = db.cursor()
    # k  s4 i6 r) |4 K# M& m
  72.             #注意此处字符串的占位符要加双引号"%s"
    , i' v( S. K) \
  73.             sql = ' INSERT INTO BookConte (Bookname,Cater_Name,Book_author,Book_Introduction,Book_Synopsis,Book_Palabras,Bookurl,Booktitle,BookID,BookConte,Titleid,abover,Book_Date) VALUES("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")' % (Bookname,Cater_Name,Book_author,Book_Introduction,Book_Synopsis,Book_Palabras,Bookurl,Booktitle,BookID,BookConte,Titleid,abover,Book_Date);6 a% O- U1 o! u; X2 [
  74.             print(sql)9 z9 {& F: [9 c; f
  75.             cursor.execute(sql)
    9 m& Q$ @! c2 w  V, s
  76.             print(cursor.lastrowid)
    - w7 V9 s7 o* W- n
  77.             db.commit()
    & \9 C: m1 D0 a+ i5 e7 t  z$ i: u
  78.         except Exception as err:
    5 e2 v5 ~/ Q$ Z+ `* P: ^
  79. #        except:
    6 h5 x8 i; Q2 }# w: _
  80. #            print('Failed')
    ; O7 M8 O& `; X& {
  81.             print("Error %s for execute sql: %s" % (err, sql))8 F' l4 x# I1 a+ b" i3 ~
  82.             db.rollback()& v) N" a  t/ I& o
  83.         ( l# k' E5 @, @8 V4 J
  84.     def add_locoy(self,Bookname,Cater_Name,Book_author,Book_Introduction,Book_Palabras,Book_img,Booktitle,BookConte,abover): 4 C+ f8 `0 w5 l, j/ ~/ C5 F: L
  85.             reload(sys)
    $ s5 \# Z' o( k/ M9 D
  86.             sys.setdefaultencoding("gbk")6 t* ]% @/ G( {; Q$ {
  87.             locoy_url = 'http://www.******.net/locoy/?my=book'  #697火车头发接口地址3 n9 T9 N1 J& l1 g+ l" M7 n0 @
  88.             locoy_data = {! O& O; R$ w4 x
  89.             'my_u':'用户名',   #后台用户名
    6 \  }5 Y9 Z* ^! F, ^
  90.             'my_p':'密码',   #后台密码( c# W3 z- y1 @5 G; X- \
  91.             'subject_669977_net':Bookname.encode('gbk', 'ignore'),3 h6 U) H/ _7 W7 R# C6 v1 A
  92.             'caid':Cater_Name.encode('gbk', 'ignore'),
    " v* z- T% j3 m5 S& n
  93.             'title_669977_net':Booktitle.encode('gbk', 'ignore'),8 o; `& K' q8 K* z, v
  94.             'article':BookConte.encode('gbk', 'ignore'),1 H8 P$ n, _9 j$ T3 n% e& I2 \
  95.             'author':Book_author.encode('gbk', 'ignore'),3 N  J, |! h+ {* r8 p
  96.             'ready_1':Book_Palabras.encode('gbk', 'ignore'),
    " H! I5 }& D' W4 M* M/ X5 r1 D
  97.             'thumb':Book_img,
    , ^, C( P; o& s* A
  98.             'content':Book_Introduction.encode('gbk', 'ignore'),. ]) d/ H' w& D, S
  99.             'abover':abover.encode('gbk', 'ignore')           , W0 }# J# E6 W" s( d- p4 g# h7 Q, [
  100.                 }
    2 [; o" p8 [5 s' G
  101.             res = requests.post(locoy_url, data=locoy_data)
    5 s) g! W2 z5 t9 S' p( Z3 ^) l
  102.             print res.text
    4 J1 _7 [" ?  O6 n. V- L
  103.             print res.content
    , p! e7 e; b. C( j& s  N
  104. #            print Dsd( Q& |4 G7 R7 [- {" T- c' y
  105.             return res: P% {1 F4 f- A3 F' p
  106.    
      I8 }3 \5 K0 J+ W! V
  107.     def __init__(self):6 U8 z# g4 D  d' I6 @
  108.         self.base_url1 = 'https://www.****.cc/'
    1 p& k$ m7 R! w$ z
  109.         self.base_url2 = '/'0 Z4 A- t: H2 w* ~$ e9 {7 i# G8 L
  110.         self.CaterId = []+ \6 M. w% k6 v5 Q
  111.         self.CaterIds = ['xuanhuan', 'wuxia', 'yanqing', 'lishi', 'kehuan', 'kongbu', 'nvsheng']
    0 z$ T6 X$ {( x2 t: I8 `9 c; w
  112.         self.page_num = 1
    ' N. K' q* n) o7 h. l- \" [% X! f
  113.         self.total_num = 200   
    2 g$ W( Y1 I& s$ f
  114. 9 c" I  @7 _/ j; Y
  115.     @every(minutes=8 * 60)  Z& n& p7 ^3 _, J$ c
  116.     def on_start(self):
    : C& {  |! L8 V+ M# M7 z0 \
  117.         global Cater_Name
    3 K" ~6 O. c% A% S( j. l
  118.         Cater_Name = []
    " s6 u% K0 e& Z" {' I) _
  119.         while self.page_num <= self.total_num:
    " B" d7 U3 e2 ]& H
  120.             for self.CaterId in self.CaterIds:
    " V- i4 K) d0 F) O- m
  121.                 if self.CaterId  == 'xuanhuan':- n3 _0 f# ^2 d* B% y9 I
  122.                      Cater_Name = '玄幻'( c0 ^# L2 [8 Q& D
  123.                 if self.CaterId  == 'wuxia':
    . q2 {" m! w" Y1 d8 C) d7 E/ e
  124.                     Cater_Name = '武侠'
    5 J1 H* n: t, q/ t1 R! w' S& O
  125.                 if self.CaterId  == 'lishi':% Z0 Y* H  t, K0 v' h3 E# G1 i
  126.                     Cater_Name = '历史'            
    ) x! G; k- x+ Q( Y( q' f0 X- b
  127.                 if self.CaterId  == 'yanqing':( `) E6 L$ y5 D
  128.                     Cater_Name = '都市'
    ( {. |0 }# i! z
  129.                 if self.CaterId  == 'nvsheng':% f7 O1 H4 ]& a# o: M9 E7 W: w- \
  130.                     Cater_Name = '都市'
    + @# K* U( }( }# b7 ]0 t' x1 e
  131.                 if self.CaterId  == 'kehuan':
    1 R; t4 d$ b8 ]3 @
  132.                     Cater_Name = '科幻' / L, M! j9 v. F6 B% }! S7 y+ ]% A
  133.                 if self.CaterId  == 'kongbu':
    5 F% b  `8 q2 W0 |3 n6 \
  134.                     Cater_Name = '游戏' . z8 Y2 f/ H0 e3 k
  135.                 print self.CaterId
    $ k  X( l# @& v
  136.                 url = self.base_url1 + str(self.CaterId) + self.base_url2 + str(self.page_num) + "/"         
    7 z4 J; b/ o5 {+ L8 i, \& C: c! ^, Y
  137.                 self.crawl(url, callback=self.list_Caterg,save=Cater_Name)" T; b! }0 E$ A- x
  138.             self.page_num += 1 % H  P2 a; D2 y/ \9 E
  139.             , D% O6 {* L3 v5 A
  140.     def list_Caterg(self, response):
    1 G/ [! D% P0 b
  141.         Cater_Name = response.save
    6 @" {' _# R) V$ o5 v
  142.         for each in response.doc('.pic-list a[href^="http"]').items():
    9 w* b% f' r& I# e7 e, Z
  143.             self.crawl(each.attr.href, callback=self.list_Caterg_detail,save=Cater_Name)
    ; [) w9 M1 A5 ^2 c1 r
  144.             9 p" l9 G) O# i! u: @% R; {
  145.     def list_Caterg_detail(self, response):5 i0 y+ a2 g5 n7 G% F  I
  146.         Cater_Name = response.save! j& p4 B8 c1 \, N( M3 F- v' ]
  147. #        print Cater_Name1 I- ^0 r" A, p
  148.         Bookname = response.doc('h1').text()
    + k  M% m9 d. @3 P4 ?
  149.         print Bookname7 Q& D) W  B/ Q6 e# g
  150.         Book_author = response.doc('.authorname > a').text()
    & V: h6 V9 D2 G  g  I
  151. #        print Book_author
      k2 K+ t5 [/ n/ v2 T
  152.         Book_Introduction = response.doc('.book-intro > div').text()6 ^4 O. |7 T; S4 G% D
  153. #        print Book_Introduction# d, [7 s  x$ ^/ i, S7 Y! ]$ I9 b( D
  154.         Book_Synopsis = response.doc('b').eq(1).text()8 Y9 f4 {: N* d1 }9 Y2 r- V
  155. #        print Book_Synopsis
    + V% X. z- j' E) a
  156.         Book_Palabras = response.doc('.booktitle p').text().split(' ')[1].split('|')[0]
    . Q. u6 ?& B5 V/ n
  157. #        print Book_Palabras
    & b" A9 `, D( |# H) g; c
  158.         BookIDs = response.url.split("xiaoshuo/")[-1].split("/")[0]   #小说ID
    4 J6 w& L; R/ `0 V
  159. #        print BookIDs
    ) h/ z% g' K8 ^: I7 G3 d( V) s  q
  160.         Book_Dates = str(datetime.datetime.now())         - M9 ]- S* M6 ^5 D9 M- w
  161.         for imgs in response.doc('.bigpic > img[src^="http"]').items():
    : K- P7 a+ v/ `" `7 N5 K' d& e
  162.             img = imgs.attr.src6 X3 n! ?" Y. P6 n$ [$ ?& T
  163.             print img2 X8 G6 \! [* O4 D
  164.                 #小说封面下载
    6 Z" q8 e( ~, l
  165.             extension = self.getExtension(img)" ?7 ]8 X- ?6 D! o- N" U
  166.             name = self.getname(img)
    ) {6 u- _/ s& q9 c0 C4 n: G
  167.             file_name = name + "." + extension" Z& N3 C5 E3 A% c  k
  168.             imgDir = P_dir + name/ ]/ c, l9 A3 S
  169.             Locaimg = imgDir + "/" + file_name
    ; k' S* c8 [3 `- C! |) O' ^
  170.             print Locaimg
    & E( b$ e0 {4 L6 T* `
  171.             if(self.download(P_dir, imgDir, file_name, img)):   #这2行可注译,图片下载到本地& u; Y: b4 p6 s9 s- ?3 T
  172.                 print('attachment url is ' + img)               #
    ! {  F5 {/ c/ x- a
  173.             Datos = {
    6 l$ e. s) e& W$ s; c
  174.                     "Cater_Name":Cater_Name,7 ?# }4 X2 R& ^8 l/ u. i) v
  175.                     "Book_author":Book_author,3 L5 f) O' M% `1 `* T
  176.                     "Book_Introduction":Book_Introduction,
    % T! x& t, m+ F8 ^6 r. e+ x) f
  177.                     "Book_Synopsis":Book_Synopsis,
    * u: D4 N0 W4 z2 G) Y! T* f( K
  178.                     "Book_Palabras":Book_Palabras,
    % e/ h8 [; k2 b0 {; ~: d
  179.                     "img":img,
    % e9 R8 g1 j8 b# x. I- g3 g8 Q
  180.                 }# Q' |& ]: f5 T- H3 A
  181.             self.add_BookFile(Bookname, BookIDs, img, Locaimg, Book_Dates)  #这行可注译,数据库发布接口,方便其他系统的发布4 P6 \  f+ X* M% M: _, e( C9 ?& [
  182.         for each in response.doc('div[class="bookbtn-txt"]  a[class="catalogbtn"]').items():
    + W% M/ x$ ~) P% u, E8 q
  183.             self.crawl(each.attr.href, callback=self.index_page,save=Datos)
      J1 y3 V' x5 ?( m8 B# J
  184.             
    & l- T6 b& o& B. ^& c
  185.     @config(age=8 * 60 * 60)   
    ) d% E( E7 S8 w2 f+ g& P+ ~- @5 m0 V! e
  186.     def index_page(self, response):
    * h9 d  L; h6 {# j6 S
  187.         Datos = {
    ( r. p4 j# }2 ~- B
  188.                   "Cater_Name":response.save['Cater_Name'],. ?; h, r. X1 d2 O8 ]5 s: A
  189.                    "Book_author":response.save['Book_author'],
    4 Y9 F8 `2 e! s+ Y
  190.                    "Book_Introduction":response.save['Book_Introduction'],, P) v0 H9 j" Y) e
  191.                    "Book_Synopsis":response.save['Book_Synopsis'],
    0 _4 h8 r4 y$ Q. F2 T- o7 @" @' k' x
  192.                    "Book_Palabras":response.save['Book_Palabras'],6 a) {+ N0 d6 `( R
  193.                    "img":response.save['img'],
    5 J" T4 x  h6 X2 H, I$ Y
  194.                      }
    4 b2 A# P9 Y/ ?5 i& K4 s) m
  195.         for each in response.doc('.chapter-list li:first-child a[href^="http"]').items():
    ) L2 t4 K- g3 c
  196. #        for each in response.doc('.chapter-list  a[href^="http"]').items():  7 y( ^4 U1 X" x) }' v3 n
  197.                     self.crawl(each.attr.href, callback=self.detail_page,save=Datos)9 _* y5 [2 ~: }2 k+ Y. |
  198.     @config(priority=2)6 k( V/ y# s. G; S" @
  199.     @catch_status_code_error  I4 m0 k+ Q9 [+ T$ ^* K
  200.     def detail_page(self, response):        
    , B3 R" N7 X, c6 e
  201.         NewRe1 = u'哈书'# P7 D$ Y4 `: ~- I: a5 O; z6 a
  202.         NewRe2 = u'huhjsd.CC'
    , h/ v! Q- Q& B' g6 t
  203.         NewRe3 = r'^\\n\\n'0 I# {+ P: [  B% _! |
  204.         NewRe5 = u'小说网'
    0 V/ }% w2 k" V- U0 s: n
  205.         NewRe6 = u'fgdfgf'8 o. [3 [% O: L0 D' L! K
  206.         NewRe7 = u'fgfgf'
    6 F! |  N) S& o' ]& d3 Y
  207.         NewRe8 = u'ffhgf'% ?3 V. i4 C9 d) H* a
  208.         NewRe4 = r'[\f\t\v+\.\{\(\)\}\!\/_,$%^*(+"\')]+|[+——()?【】“”!,。?、~@#¥%……&*()]+'( _# W, g- b# j& ]* Z
  209.         ReC1 = u'静思'- V" q# N, W, H4 C/ f
  210.         ReC2 = u'aghgf.com'3 A# _. n( d4 }; x9 c" Z
  211.         ReC3 = u'aghgfh.com'
    . ^1 f) `2 ?+ n" }$ w* Z
  212.         ReC4 = u''! R4 {( c/ e: l
  213.         ReC5 = u'文学网'. Y! h% V. v! f
  214.         ReC6 = r'<BR>'
    $ \# x! F3 Z4 R
  215.         Bookname = response.doc('.readlocation a').eq(2).text()   #小说名称9 B* `( H7 \3 L9 D. m
  216.         print Bookname
    . e4 }( E7 O6 @' O0 I
  217.         Cater_Name = response.save['Cater_Name']   # 小说分类
    - |& A+ |1 B; [; w$ D
  218.         Book_author = response.save['Book_author']   #小说作者
    , U6 H+ ^+ d- ]* e' t; |' o4 C
  219.         Book_Introduction1 = response.save['Book_Introduction']   #小说简介& }* R# K. i# n8 i, i% v9 d9 ?
  220.         Book_Synopsis = response.save['Book_Synopsis']   #最近更新
    7 _" O9 U6 C+ s( D9 _( S% J0 T9 ^/ d
  221.         Book_Palabras = response.save['Book_Palabras']   #小说字数' i0 @+ d# q7 K
  222.         Bookurl = response.url   #小说网址9 m, c9 I2 y$ L4 b5 w0 `
  223.         Booktitle = response.doc('.article-title').text()   #章节名称# ^, V/ F2 L* j/ y  m5 r8 X: L5 m9 ^" Q3 ?6 l
  224.         BookID = response.doc('.readset-r span').text()   #小说ID. o+ E" T8 u7 }6 M# X5 k
  225.         BookConte1 = response.doc('.article-con').text()   #小说章节内容
    3 J# U' Q' p, ?% p$ ~  g
  226.         abover = response.doc('.article-title').text() + response.save['Book_Synopsis'] + response.save['Book_Palabras'] + response.save['Book_Introduction']   #小说状态(连载还是完成)9 M: z* n% E3 w+ s8 a' m
  227.         Book_Date = str(datetime.datetime.now())    # 采集时间# `& y  H& o& c6 T. f' A/ ?
  228.         BookConte2 = BookConte1.replace(NewRe1 , ReC1)
    & v0 I3 J/ h4 x
  229.         BookConte3 = BookConte2.replace(NewRe2 , ReC2)9 A8 C/ h5 Y) ?" A
  230.         BookConte5 = BookConte3.replace(NewRe5 , ReC5)2 C4 B% b" z: Z1 E; ?. t
  231.         BookConte6 = BookConte5.replace(NewRe6 , ReC2)
    7 D# o4 R. V* y4 q, r, X4 X. J2 x
  232.         BookConte7 = BookConte6.replace(NewRe7 , ReC2)& I$ P1 w; O3 A& N' F0 L2 G
  233.         BookConte8 = BookConte7.replace(NewRe3 , ReC6)5 f/ g& X+ q' \3 [1 [5 t+ d
  234.         BookConte4 = re.sub(NewRe4 , ReC4 , BookConte8)' w; R1 `- z* E1 W. M
  235.         BookConte = BookConte4.replace("\n\n","<br>"). L9 Z8 [1 i4 F
  236.         print BookConte* h, H6 N$ A* H9 D2 F
  237.         Book_Introduction2 = Book_Introduction1.replace(NewRe1 , ReC1)
    ; |* J+ T- j$ P; l9 x' l
  238.         Book_Introduction3 = Book_Introduction2.replace(NewRe2 , ReC2)) @" p  X4 X* p7 `
  239.         Book_Introduction4 = Book_Introduction3.replace(NewRe3 , ReC3), X" r8 A  [, M2 ~/ ~4 t- t
  240.         Book_Introduction = re.sub(NewRe4 , ReC4 , Book_Introduction4)
    ) X: A5 D+ Q. B$ o5 l0 X
  241.         Titleid = response.url.split(BookID + "/")[-1].split("/")[0]     $ z* }- h1 s% u7 J- a
  242.         Book_img = response.save['img'],  #小说图片
    % q/ G1 @1 ?1 d& S; a1 v
  243.             
    2 E( l4 B. Y0 o* I! z* b
  244.         #insert into MySQL 小说入库8 r4 l$ J3 U" o+ {; F$ V
  245.         self.add_question(Bookname,Cater_Name,Book_author,Book_Introduction,Book_Synopsis,Book_Palabras,Bookurl,Booktitle,BookID,BookConte,Titleid,abover,Book_Date)   #这行可注译,数据库发布接口,方便其他系统的发布
    * B, L0 N: n% P8 u! X
  246.         self.add_comment(Bookname, Booktitle, BookID, Titleid, Book_Date)   #这行可注译,数据库发布接口,方便其他系统的发布. c1 E/ `% N. F6 y4 G& v
  247.         #post提交发布
    0 l' x6 u  T+ U) r1 k& x
  248.         self.add_locoy(Bookname,Cater_Name,Book_author,Book_Introduction,Book_Palabras,Book_img,Booktitle,BookConte,abover)  #这行可注译,火车头发布接口,不需要可取消! H* x+ B$ E# g
  249.         Datos = {
      N$ z, a) }# S# z
  250.                   "Cater_Name":response.save['Cater_Name'],
    ' N7 M, I; o$ |4 C
  251.                    "Book_author":response.save['Book_author'],8 l8 Z6 A6 l, S8 ?8 ^
  252.                    "Book_Introduction":response.save['Book_Introduction'],4 S* D& v% I% z9 p1 p. ]$ Z( P
  253.                    "Book_Synopsis":response.save['Book_Synopsis'],# s8 a0 l0 l- P. L! t
  254.                    "Book_Palabras":response.save['Book_Palabras'],
    ; T# c% \5 H: o: X
  255.                    "img":response.save['img'],5 a* i7 v- l9 ^* l" o5 m& ]
  256.                      }, V/ o9 t2 [3 t
  257.         for each in response.doc('.articlebtn > a:nth-child(4)[href*="/xiaoshuo"]').items():
    5 J0 [  Y8 n0 u5 E
  258.             self.crawl(each.attr.href, callback=self.detail_page,save=Datos)
      }1 `- \# W2 X9 G: P; f
  259.         return {
    3 g, p- ^! V$ i; a4 k1 W
  260.             "Cater_Name":Cater_Name,
    + W, O* w0 q: G* t0 o- s
  261.             "Bookname":Bookname,
    / e5 Y2 Y4 m4 \
  262.             "Book_author":Book_author,8 |$ L0 g5 r6 r" D9 o4 g! ]- C6 Y
  263.             "Book_Introduction":Book_Introduction,# @# H* k% ?/ _
  264.             "Book_Synopsis":Book_Synopsis,) L7 W; _; M) I
  265.             "Book_Palabras":Book_Palabras,6 ]1 ?0 U& w/ c9 }4 ^
  266.             "Book_img":Book_img,5 H' d/ O" W  b" ?0 M9 v% U
  267.             "Bookurl": response.url,* x% g1 X+ Y; s4 c
  268.             "Booktitle": Booktitle,5 u  Z6 ^6 w+ Y( ?# _. g
  269.             "BookID": BookID,9 R/ g2 m: S- i( T
  270.             "BookConte": BookConte,) s4 L  b! N3 J
  271.             "Titleid": Titleid,
    # F& ^4 ~/ _; I8 c/ X1 I
  272.             "abover":abover,) }/ C: L, B& Z7 z6 B6 r' s
  273. #            "Book_Date" = str(datetime.datetime.now()),; h8 Q+ z2 J  Z4 D, v" J( N7 _
  274.         }- a, @! J) P! j( d
  275.     def download(self, P_dir, imgDir, file_name, Book_img):( a% }  j4 Z  e
  276.         if not os.path.exists(imgDir): ; A. i4 C) D# T
  277.             os.makedirs(imgDir)
    ! U! O$ K9 @+ B. T
  278.         file = imgDir + "/" + file_name
    # L, b' o+ w: b) }) m: q
  279. #        print file9 {1 \) t; d' e  Y$ \
  280.         f = open(file, 'wb+')
    ; U9 o( ^5 r4 U* g
  281.         imag = requests.get(Book_img) 9 T9 {4 c) t0 H- Y/ W2 i
  282.         f.write(imag.content)( K, ?# U- \* m: L+ u" V) o( b9 V3 t
  283.         f.close()7 C  P8 T5 {+ }0 _
  284.         #保存图片前! `# m! E$ R. s. _7 b# K) v7 N5 {
  285.     def save_imgs(self,response):4 ]0 ~5 C& w- y% C+ ^. Q7 o1 s
  286.         content = response.content8 R( D, i0 K# N  N' x( l( P* O+ B
  287.         file_name = response.save["file_name"]8 O; n4 P. _0 g% k2 U4 b
  288.         imgDir = response.save["imgDir"]
    % `# K) q0 ?, u1 C$ y1 j: }
  289.         file_path = imgDir + file_name+ d6 ~: s) k; P9 h# `+ {
  290.         self.save_img(content,imgDir,file_path)
    * O+ u5 D$ ~" N6 P2 N+ y
  291.     #保存图片
    ! s9 ]2 N  `+ |; y2 j3 p, z
  292.     def save_img(self,content,imgDir,path):
    6 c- l2 a( A9 i2 ^- g+ ~+ A
  293.         if not os.path.exists(imgDir):                        
    4 x4 W- Q8 G; Q0 b, L- q& K7 P
  294.             os.makedirs(imgDir)  f' O2 k& o( X! k
  295.         f = open(path,"wb" ). [- c9 J2 F% A* W/ v6 a
  296.         f.write(content)
    + t( R8 f% s8 s' p, I
  297.         f.close()
    % c$ l) B* M8 z% s
  298.     #获取url后缀名
    , ~  |  q, b3 p! X
  299.     def getExtension(self,url):                           
    / A' i. H1 C) b5 T  |
  300.         extension = url.split(".")[-1]) q! ]# h4 j- U
  301.         return extension $ M% k' @$ C$ k7 n, ?
  302.    
    " |, N% ^- C7 {' l; P3 ^
  303.     #获取图片名( _, Y" ]" |! J7 K5 X
  304.     def getname(self,url):
    ) |$ R7 \; x! a
  305.         name=url.split("/")[-1].split(".")[0]( J* ?- E* D1 [- E
  306.         return name
复制代码

  @& n- g" B( i8 Z/ n! r
' m. {1 j% I3 L3 ?* H1 j+ D
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|中国飞逸网

GMT+8, 2025-7-1 10:05

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表