找回密码
 注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
发表于 2019-6-8 23:06:07 | 显示全部楼层 |阅读模式
Python + pyspider某小说站的爬虫,入数据库,火车头发布,资源下载到本地,另可写爬虫!
- g; Y1 t) X+ {6 c) C, w0 W
  1. #!/usr/bin/env python6 e( S- y# M$ [" R, ~' ^/ }
  2. # -*- encoding: utf-8 -*-
    7 S6 p6 e8 }: X2 M+ A
  3. # Created on 2019-05-05 21:43:118 N) \3 K6 W; J3 Q/ T) A0 ~/ ~9 a
  4. # Project: XiaoShuo
    , R/ \' S0 `* T2 s
  5. 6 \# r1 ]# \1 |2 C) \) [& V
  6. from pyspider.libs.base_handler import *5 k0 P1 B9 w1 x
  7. import pymysql
    , e9 I; \/ H! e! L( r+ C
  8. import random
      d' r' x/ k; e
  9. import datetime
    2 T3 H5 Y/ x* o/ T" w2 s7 z) U
  10. import urllib2,HTMLParser,re/ d; J5 b- F- Y$ ?3 r7 G
  11. import os# s6 i; @$ ~: E  v
  12. import sys
    & Y& w7 ]% S( f6 c( V
  13. import re
    , w4 y+ K! o# p3 f* u# m
  14. import codecs
    $ `& D& @4 h+ }3 y) J( O/ F3 C
  15. import requests
    - c7 j8 x) h& b) ?2 x# m- g' q
  16. import json
    : c  O. }% W2 r, T& X# T' R- b/ z, r
  17. 7 Y# P" T9 J' H8 C
  18. class Handler(BaseHandler):
    % n; z! |: B9 N5 O6 b/ _$ R5 S
  19.     global Datos
    * n- N+ R. N0 R$ z# X
  20.     global P_dir   
    . ]* G2 S: [; R# F% L
  21.     P_dir = '/Tools/Debug/'  #采集时候图片保持到本地的路径
    ) y( X  G/ \0 ~. n" |# G6 E
  22.     global Datos# [9 O  B* s+ @. t
  23.     Datos = {}
    ; |# g7 P' V/ G% ]8 M0 w2 @
  24.     headers= {, q2 M5 ?0 ~# \/ o
  25.     'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    + y, S) I/ J$ k& ?
  26.     'Accept-Encoding':'gzip, deflate, sdch'," n% l2 K9 \  F  _) R+ z! O3 c
  27.     'Accept-Language':'zh-CN,zh;q=0.8',
    3 m0 X. x' G9 V  {5 s6 N
  28.     'Cache-Control':'max-age=0',% |/ [8 t! t% {6 v2 @+ c
  29.     'Connection':'keep-alive',
    . a- @$ c9 q( X( H
  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'- {1 L9 X/ e+ @
  31.     }0 m1 S  K, _9 B& f7 _
  32.     crawl_config = {
    1 V* _! n" S; K% g2 I  h
  33.         'headers' : headers,
    * C$ \9 {4 z' j9 K" ]( N
  34.         'timeout' : 300* {) l. ^. [: M. Z+ ^9 {
  35.     }! P5 ?! j" A3 r8 j* J# \, _
  36.     def add_BookFile(self,Bookname, BookIDs, img, Locaimg, Book_Dates):
    - U: B  ?. X- z: Y( G" W
  37.         db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8")* i# L* ~& j' [1 |; \9 l
  38.         try:2 D1 |  P+ K  E( {* x. C# \$ {
  39.             cursor = db.cursor()
    2 j( P! N- g" L9 q9 R4 N  t
  40.             #注意此处字符串的占位符要加双引号"%s"; p; A5 x! A& }5 m- i1 h! _( I9 x
  41.             sql = 'insert into BookFile(Bookname, BookID, img, Locaimg, Book_Date) values ("%s","%s","%s","%s","%s")' % (Bookname, BookIDs, img, Locaimg, Book_Dates);
    ) V9 j! o- V3 [+ u! b) ]
  42. #            print(sql)6 d2 b1 a7 ?% X! i9 G% ^
  43.             cursor.execute(sql)$ P% u. ]! |& g# ]
  44.             ! H) _& ^/ E0 i6 }
  45.             #qid = cursor.lastrowid2 m( j/ w" b- q
  46.             #print(qid)
    ) M0 B0 m! ^5 [1 C" D2 q& G8 U( L
  47.             ) d7 q: ]) d- b- e' ~. f. L
  48.             db.commit()
    ; U) O+ f0 k; k
  49.         except Exception as err:
    8 f! m5 D- N9 l- R0 M: M6 k1 v
  50.             print("Error %s for execute sql: %s" % (err, sql))
    ; r. E- o1 l" z  D. S
  51.             db.rollback()
    6 S' o2 r4 Y; C1 j, g6 I7 Q
  52.     def add_comment(self,Bookname, Booktitle, BookID, Titleid, Book_Date):- E6 D/ Y  ]. d
  53.         db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8")- C# L( Z! D* C, u, j
  54.         try:
    ; h. D+ N* |" r: a" Y' t
  55.             cursor = db.cursor()% t4 A+ l! w- m- Y! S
  56.             #注意此处字符串的占位符要加双引号"%s"
    ' d* e' D' `3 {
  57.             sql = 'insert into BookTitle(Bookname, Booktitle, BookID, Titleid, Book_Date) values ("%s","%s","%s","%s","%s")' % (Bookname, Booktitle, BookID, Titleid, Book_Date);9 k- K% Z4 @1 I+ v7 S5 M
  58. #            print(sql)
    , P; b7 p' B5 z4 V/ ?' u
  59.             cursor.execute(sql)9 \* G9 Z# T. |0 b0 p( r
  60.             
    ' o& }; _5 Y* [. Z8 m2 ~
  61.             #qid = cursor.lastrowid, d" X4 r7 V6 q
  62.             #print(qid). P! l7 I& b" w) y8 q" C* w" G, \
  63.             
    ; v% s) g) x. H) w
  64.             db.commit()7 }$ s* d# ~2 S
  65.         except Exception as err:
    6 l+ {" O: g8 ~" X* z1 h
  66.             print("Error %s for execute sql: %s" % (err, sql))' E. c9 A' T+ u) w- H& w3 q
  67.             db.rollback()7 V) l( z5 ?1 P' V  c  R
  68.     def add_question(self,Bookname,Cater_Name,Book_author,Book_Introduction,Book_Synopsis,Book_Palabras,Bookurl,Booktitle,BookID,BookConte,Titleid,abover,Book_Date):
    - G+ m' ~/ M- c; M7 I2 ]9 A
  69.         db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8")
    3 J* S% g9 q( g
  70.         try:
    3 R3 z- V8 Q" l0 d( Z
  71.             cursor = db.cursor()% J; y/ X/ ?# ~1 u
  72.             #注意此处字符串的占位符要加双引号"%s"% h  N$ b% R+ c; J/ Y" O  D
  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);
    + E- p# ]) n3 l( n+ O$ f9 J
  74.             print(sql)
    " n5 F7 E# `$ G% E4 n
  75.             cursor.execute(sql)
    ' b" I4 n1 y: k' [7 h% K: ^4 a# Y
  76.             print(cursor.lastrowid)
    1 x( L1 h0 O. _# I
  77.             db.commit()
    7 A1 Y0 u( Q' K$ ]- d: S5 z
  78.         except Exception as err:
      j) N8 i+ Z# \2 r: ?6 F
  79. #        except:+ i* ^+ d& `5 u" }
  80. #            print('Failed')/ ]* a+ U# Q% b- X' x; ~$ h" s2 j& q
  81.             print("Error %s for execute sql: %s" % (err, sql))
    ) t9 P$ F  C4 G3 d- V$ {
  82.             db.rollback()
    ; c8 |' L8 e0 e/ Y# q' t
  83.         
    5 E/ h* z# }) V5 T' N5 |
  84.     def add_locoy(self,Bookname,Cater_Name,Book_author,Book_Introduction,Book_Palabras,Book_img,Booktitle,BookConte,abover): , Z) d" |0 r! l$ `6 Q8 N4 D
  85.             reload(sys)
    8 ^8 E7 {$ P# f; c& ~
  86.             sys.setdefaultencoding("gbk")6 a! x  m2 q/ y( w6 K6 C: T
  87.             locoy_url = 'http://www.******.net/locoy/?my=book'  #697火车头发接口地址* C* ?. X2 H$ U+ Y
  88.             locoy_data = {
    $ y, G; e: ^9 i3 }+ _
  89.             'my_u':'用户名',   #后台用户名6 ?( l* }! `' c4 u
  90.             'my_p':'密码',   #后台密码
    3 S" C1 }; T* W4 L2 w
  91.             'subject_669977_net':Bookname.encode('gbk', 'ignore'),
    & D& t" @) O0 h( P
  92.             'caid':Cater_Name.encode('gbk', 'ignore'),! d1 t. m. e& z+ a! ^
  93.             'title_669977_net':Booktitle.encode('gbk', 'ignore'),
    6 @& J4 X  x3 D8 Z' S  X9 }
  94.             'article':BookConte.encode('gbk', 'ignore'),- z- Q' F- ^4 ~4 a4 B
  95.             'author':Book_author.encode('gbk', 'ignore'),
    # n) V5 v2 M9 i6 |8 N
  96.             'ready_1':Book_Palabras.encode('gbk', 'ignore'),+ i! J+ @4 X! n, N: `6 Y) h
  97.             'thumb':Book_img," R- F4 `! l& ?$ `( e
  98.             'content':Book_Introduction.encode('gbk', 'ignore'),
    2 I6 T# z+ a2 m/ Z3 {  l& {9 @
  99.             'abover':abover.encode('gbk', 'ignore')           
    / ~* r, H( P8 C
  100.                 }! Q- ?% L! `, r/ f5 F
  101.             res = requests.post(locoy_url, data=locoy_data)  [- F% d! z1 b4 z
  102.             print res.text& ^, R3 _' E& o( l
  103.             print res.content
    ; g! }8 A5 z. u5 [1 D
  104. #            print Dsd/ m$ f2 X7 A% A1 Y/ r: v
  105.             return res
    - H" `+ |/ ^; R) z
  106.    
    7 }6 W5 V9 C  ]4 f8 K! p
  107.     def __init__(self):6 j( S4 C7 _* X- ~6 f' \9 S$ o* ?
  108.         self.base_url1 = 'https://www.****.cc/'4 i+ ~8 r. g" `- }. Q3 d
  109.         self.base_url2 = '/'
    6 h3 A7 N2 O; \/ g5 E- W
  110.         self.CaterId = []
      M0 k% e1 }3 E/ ~
  111.         self.CaterIds = ['xuanhuan', 'wuxia', 'yanqing', 'lishi', 'kehuan', 'kongbu', 'nvsheng']
    7 v! z- u! d1 b
  112.         self.page_num = 1* Z' K: }0 g. M. h
  113.         self.total_num = 200   0 E) G, O4 ^- Q( d7 o
  114. 3 J% b. ]' K9 w' p6 g! V
  115.     @every(minutes=8 * 60)
    + Z4 y- ]0 N8 s
  116.     def on_start(self):" {, @, v/ h+ T8 L; D2 S" Q  ?8 V
  117.         global Cater_Name
    ( Y0 y: N, Z9 W4 O3 h' j2 `
  118.         Cater_Name = []
    0 L9 J; o- x$ z$ z* ]
  119.         while self.page_num <= self.total_num:
    * g+ T. e, d: |$ S9 V$ {
  120.             for self.CaterId in self.CaterIds:/ V; v' N8 Y0 ^* U
  121.                 if self.CaterId  == 'xuanhuan':
    " K" J& ?- N  Y5 O7 u4 R, L; o
  122.                      Cater_Name = '玄幻'* C) M5 W) j, \9 D; U- O4 p& c7 \8 N
  123.                 if self.CaterId  == 'wuxia':
    . G# r  g7 {! L6 c' N
  124.                     Cater_Name = '武侠'
    & Z2 }$ T" y0 Q3 T1 v
  125.                 if self.CaterId  == 'lishi':
    $ P& z* D% f. A) Z5 `+ |& D
  126.                     Cater_Name = '历史'            6 H/ I3 j$ f" y, K7 B& r
  127.                 if self.CaterId  == 'yanqing':0 L  F) B! C; e3 [) g% c
  128.                     Cater_Name = '都市' * g7 I0 u9 L7 i6 g2 f* d
  129.                 if self.CaterId  == 'nvsheng':* \/ m# }/ }3 b! r. U; x
  130.                     Cater_Name = '都市'
    7 @5 Y# W6 a. c* r4 p! h' N7 `
  131.                 if self.CaterId  == 'kehuan':
    ! \  N, l4 H( F7 w% Y4 G
  132.                     Cater_Name = '科幻'
    ' Q# R+ T. f$ O" f& r
  133.                 if self.CaterId  == 'kongbu':
    8 d+ w. r! D( x# P1 N) W
  134.                     Cater_Name = '游戏' " U0 k3 k# [8 b. i9 G
  135.                 print self.CaterId
    7 X/ i- z! Z6 C% L+ @( V1 y
  136.                 url = self.base_url1 + str(self.CaterId) + self.base_url2 + str(self.page_num) + "/"         
    ( h$ Q3 k' m* n; [' f5 U4 Z7 ?
  137.                 self.crawl(url, callback=self.list_Caterg,save=Cater_Name)) e- u! l" \# l8 q  c! y
  138.             self.page_num += 1
    ' X9 H- x6 F& P+ @
  139.             
    8 I* p* L. a. Y2 B1 ~, j! T  e6 y
  140.     def list_Caterg(self, response):
    8 s& V# W" S3 t2 q5 Y
  141.         Cater_Name = response.save
    6 R" ~4 m4 h8 z' o- g" m  k
  142.         for each in response.doc('.pic-list a[href^="http"]').items():( G% N+ u( A2 ?9 H( i
  143.             self.crawl(each.attr.href, callback=self.list_Caterg_detail,save=Cater_Name)
    4 f. O7 M. n% L# s& G4 u
  144.             
    0 n( _2 A2 e- Y
  145.     def list_Caterg_detail(self, response):
    1 @3 |: |! X" Z0 J$ ~& ]: D
  146.         Cater_Name = response.save) b* I/ {4 [$ q. l$ f8 R5 f
  147. #        print Cater_Name; @5 G$ U* p7 t2 m! O2 U: P
  148.         Bookname = response.doc('h1').text()5 O6 D7 n: L2 z0 I
  149.         print Bookname
    8 Z1 I2 o4 j: x: [
  150.         Book_author = response.doc('.authorname > a').text(); I9 X+ i4 ?/ k
  151. #        print Book_author
    9 F% x' |2 H2 r) a6 m6 j0 V
  152.         Book_Introduction = response.doc('.book-intro > div').text()7 m; e( U. @& Y
  153. #        print Book_Introduction
    , G1 x$ X" m: o8 Y( O1 @
  154.         Book_Synopsis = response.doc('b').eq(1).text()
    4 D* M+ U% L5 d$ P$ W, y4 \# I
  155. #        print Book_Synopsis  ^6 G2 c3 G( y+ t: w9 `
  156.         Book_Palabras = response.doc('.booktitle p').text().split(' ')[1].split('|')[0]
    1 S9 [* m) z% [! Z
  157. #        print Book_Palabras) p7 a& b& f+ g" X# f
  158.         BookIDs = response.url.split("xiaoshuo/")[-1].split("/")[0]   #小说ID
    . e$ D  a1 e6 E. t9 _" m
  159. #        print BookIDs0 b& D) |+ [$ ]+ m% D/ ~
  160.         Book_Dates = str(datetime.datetime.now())         ( }% _& M- Y4 S) G3 R
  161.         for imgs in response.doc('.bigpic > img[src^="http"]').items():
    7 {0 t$ D5 m( }4 o( Q2 }! [6 m
  162.             img = imgs.attr.src
      w% B, e0 J7 K7 ^( P  P5 N
  163.             print img
    . y. u' p$ \+ f
  164.                 #小说封面下载
    9 }5 W" Z" P; J; z  s
  165.             extension = self.getExtension(img)
    0 F$ a' ?3 `- ~3 t* G( }
  166.             name = self.getname(img), @0 P+ P4 N2 j' y# ?4 ?
  167.             file_name = name + "." + extension
    3 S# J5 w/ K/ V" H- V
  168.             imgDir = P_dir + name
    ) [  H3 B6 ^% i/ o. R! ~
  169.             Locaimg = imgDir + "/" + file_name
    0 U5 V' g* @5 Z
  170.             print Locaimg
    9 Q2 D8 Y# l% _7 N: N  X' Y
  171.             if(self.download(P_dir, imgDir, file_name, img)):   #这2行可注译,图片下载到本地
    7 V8 C5 V5 \9 P; [0 k& B  M
  172.                 print('attachment url is ' + img)               #
    5 {# E5 c2 I6 R5 `; T3 W% C$ L1 v
  173.             Datos = {
    6 T1 R/ z4 D8 d" c4 q( I
  174.                     "Cater_Name":Cater_Name,& ]& i; t7 x( g/ E8 z6 d! _
  175.                     "Book_author":Book_author,
    ; d$ K+ I9 {; `( w: m
  176.                     "Book_Introduction":Book_Introduction,
    ( k0 _. V6 N1 c/ u* S
  177.                     "Book_Synopsis":Book_Synopsis,4 X# I! w/ O6 U( T" U6 ^
  178.                     "Book_Palabras":Book_Palabras,8 e5 y& I! r0 W1 o: X
  179.                     "img":img,
    ' Q, j; o+ [% N" x# q
  180.                 }
    4 E9 ^6 M/ I; Q/ k, R- t* n
  181.             self.add_BookFile(Bookname, BookIDs, img, Locaimg, Book_Dates)  #这行可注译,数据库发布接口,方便其他系统的发布8 U& f: O- T0 d
  182.         for each in response.doc('div[class="bookbtn-txt"]  a[class="catalogbtn"]').items():
    ( `. W) s7 O, X" F$ M1 j
  183.             self.crawl(each.attr.href, callback=self.index_page,save=Datos)$ t: l+ p8 m+ t6 h8 c. b9 ~0 Y4 O
  184.             
    , x) x1 R7 e! s" k# b
  185.     @config(age=8 * 60 * 60)   
    $ x/ C" ?5 r  F8 d- e
  186.     def index_page(self, response):
    8 E5 |4 W+ _' d5 q2 N9 x$ k
  187.         Datos = {
    / m5 `" @5 M0 f! Q
  188.                   "Cater_Name":response.save['Cater_Name'],
    - M5 R8 E7 c7 V) G9 h" {
  189.                    "Book_author":response.save['Book_author'],7 b* J# `4 ~/ x9 f- J6 O! Y
  190.                    "Book_Introduction":response.save['Book_Introduction'],
    5 V( r1 i) w  N) ]$ q* M" m$ n- _
  191.                    "Book_Synopsis":response.save['Book_Synopsis'],7 j+ h( \" s% O# T3 @
  192.                    "Book_Palabras":response.save['Book_Palabras'],; `$ ]1 y# O8 q4 i* L( A/ h
  193.                    "img":response.save['img'],' K, s) j. d) s& |; O+ a
  194.                      }* k3 q! {( Z# q. ]$ A  ?2 f4 r
  195.         for each in response.doc('.chapter-list li:first-child a[href^="http"]').items():2 z! Y) F% ]$ f: m  p
  196. #        for each in response.doc('.chapter-list  a[href^="http"]').items():  # a- p, K+ R4 N+ S: s& {6 t6 p
  197.                     self.crawl(each.attr.href, callback=self.detail_page,save=Datos)
    9 L" a! l7 b0 K: z/ [- Q. A
  198.     @config(priority=2)# \* J9 Z. P# q
  199.     @catch_status_code_error
    ) l- H; z8 Z( O: T3 r
  200.     def detail_page(self, response):        4 r. J, H9 H$ T& z
  201.         NewRe1 = u'哈书'
    ! T+ A, y" T( N$ f
  202.         NewRe2 = u'huhjsd.CC'% V' ^6 z7 ^0 E. u: {2 U9 V. e
  203.         NewRe3 = r'^\\n\\n'
    ( |9 j" b/ W8 c, I
  204.         NewRe5 = u'小说网'$ L/ L# a% [' U% s5 K- X1 u
  205.         NewRe6 = u'fgdfgf'* h. j1 A8 ?! Q5 B5 o
  206.         NewRe7 = u'fgfgf'9 F1 o" e6 `) x: l5 }; P6 D/ G7 x
  207.         NewRe8 = u'ffhgf'
    6 n0 F( y8 g9 C, t4 v2 R
  208.         NewRe4 = r'[\f\t\v+\.\{\(\)\}\!\/_,$%^*(+"\')]+|[+——()?【】“”!,。?、~@#¥%……&*()]+'3 q. `6 e8 W3 K6 h( X% S, p
  209.         ReC1 = u'静思'
    6 Z- c7 Z: A  \% \9 N
  210.         ReC2 = u'aghgf.com'
    1 F0 r' l# i& j
  211.         ReC3 = u'aghgfh.com'  [# i6 j7 o/ M
  212.         ReC4 = u''4 `; F4 a& k/ k. _3 N
  213.         ReC5 = u'文学网': o! v& w. a: @( d% W
  214.         ReC6 = r'<BR>'
    , J! a, b7 m- T( j
  215.         Bookname = response.doc('.readlocation a').eq(2).text()   #小说名称' [" s  d: ]/ ?, x" e. z) A/ w7 z/ m
  216.         print Bookname( Z# ]$ Q$ J: F* I& s( H
  217.         Cater_Name = response.save['Cater_Name']   # 小说分类
    ( d0 L8 K/ s/ V. Y6 R
  218.         Book_author = response.save['Book_author']   #小说作者* Z' I1 c% Q3 \+ I% W5 d
  219.         Book_Introduction1 = response.save['Book_Introduction']   #小说简介
    0 g- }, M2 I! P0 [5 w
  220.         Book_Synopsis = response.save['Book_Synopsis']   #最近更新$ c2 p( N3 a; k  A" ^/ b
  221.         Book_Palabras = response.save['Book_Palabras']   #小说字数
    0 O& d8 a6 [' Y
  222.         Bookurl = response.url   #小说网址1 T. |6 t4 ?9 V" y  Y
  223.         Booktitle = response.doc('.article-title').text()   #章节名称
    5 T4 J. p8 G8 i! d
  224.         BookID = response.doc('.readset-r span').text()   #小说ID" ~# y$ b  b* |' F* L# ]! `8 G
  225.         BookConte1 = response.doc('.article-con').text()   #小说章节内容
    $ f9 b' a  e# B! p
  226.         abover = response.doc('.article-title').text() + response.save['Book_Synopsis'] + response.save['Book_Palabras'] + response.save['Book_Introduction']   #小说状态(连载还是完成)) q+ X- ^3 ]3 Q" Q6 v
  227.         Book_Date = str(datetime.datetime.now())    # 采集时间/ c, {1 u( Y6 j/ G
  228.         BookConte2 = BookConte1.replace(NewRe1 , ReC1)% O9 R3 M$ y! I' R5 o+ |4 ^
  229.         BookConte3 = BookConte2.replace(NewRe2 , ReC2)& V- U4 Z9 a. f! ~* w8 L4 s
  230.         BookConte5 = BookConte3.replace(NewRe5 , ReC5)1 |- E! q: s. ~: H( u8 Q
  231.         BookConte6 = BookConte5.replace(NewRe6 , ReC2)
    . i7 u7 I1 w1 g: Z- m
  232.         BookConte7 = BookConte6.replace(NewRe7 , ReC2)
    ( A, W! h1 @; H* ~) }
  233.         BookConte8 = BookConte7.replace(NewRe3 , ReC6)
    # u8 R" C8 q8 r+ ]: U0 C
  234.         BookConte4 = re.sub(NewRe4 , ReC4 , BookConte8)
    9 K6 W) o+ ?9 n* _; Z- y
  235.         BookConte = BookConte4.replace("\n\n","<br>")$ m5 N9 k' I4 a6 K
  236.         print BookConte
    % V7 m5 o" `+ s+ t1 V* R5 e
  237.         Book_Introduction2 = Book_Introduction1.replace(NewRe1 , ReC1)
    2 |( l. H& }; D6 v. z
  238.         Book_Introduction3 = Book_Introduction2.replace(NewRe2 , ReC2)8 Q+ Y4 j  m# w6 K, F
  239.         Book_Introduction4 = Book_Introduction3.replace(NewRe3 , ReC3)+ X% Q% n2 b2 O* g: W$ P& k- q
  240.         Book_Introduction = re.sub(NewRe4 , ReC4 , Book_Introduction4)8 a% w5 S0 y# W! \
  241.         Titleid = response.url.split(BookID + "/")[-1].split("/")[0]     : `' ?( P1 B3 j3 P. g
  242.         Book_img = response.save['img'],  #小说图片! V/ J# a- l" v) g6 T! N
  243.             
    # t3 c# {+ P; Z6 K
  244.         #insert into MySQL 小说入库' D: [1 b/ A- n( Y
  245.         self.add_question(Bookname,Cater_Name,Book_author,Book_Introduction,Book_Synopsis,Book_Palabras,Bookurl,Booktitle,BookID,BookConte,Titleid,abover,Book_Date)   #这行可注译,数据库发布接口,方便其他系统的发布2 R+ `) j, B! u: K8 M$ o6 x8 s
  246.         self.add_comment(Bookname, Booktitle, BookID, Titleid, Book_Date)   #这行可注译,数据库发布接口,方便其他系统的发布; r0 U1 f5 F( Z  ?6 ~9 ]0 H' M
  247.         #post提交发布
    6 f5 A" |$ K, y8 X$ W* u1 `6 k
  248.         self.add_locoy(Bookname,Cater_Name,Book_author,Book_Introduction,Book_Palabras,Book_img,Booktitle,BookConte,abover)  #这行可注译,火车头发布接口,不需要可取消% e* c9 e1 ?1 n& S
  249.         Datos = {
    3 a) R3 o/ K" x8 o
  250.                   "Cater_Name":response.save['Cater_Name'],; ^2 A: w: P. c. D. C3 }
  251.                    "Book_author":response.save['Book_author'],
    5 c* h9 f; L6 @, B, G* H/ t# U: @
  252.                    "Book_Introduction":response.save['Book_Introduction'],
    : _: Z$ G; g( o( `+ J
  253.                    "Book_Synopsis":response.save['Book_Synopsis'],
    - {2 P- i, F/ f
  254.                    "Book_Palabras":response.save['Book_Palabras'],2 u/ v6 v* l% l- M, l3 M
  255.                    "img":response.save['img'],
    * ~& u5 L1 @2 \% L! ^; e
  256.                      }$ J( B: H/ m( a5 x' X7 d- |
  257.         for each in response.doc('.articlebtn > a:nth-child(4)[href*="/xiaoshuo"]').items():+ d0 O6 U; O" h  J
  258.             self.crawl(each.attr.href, callback=self.detail_page,save=Datos)
      e. q/ }. a, R+ n3 b$ B1 ^9 F" l4 v/ Q
  259.         return {" f8 Y' i% t9 ~" L) R
  260.             "Cater_Name":Cater_Name,
    $ w2 L3 ~$ H. u/ U- f+ W7 r
  261.             "Bookname":Bookname,5 F. q( t( t( ^% l' j( k7 x
  262.             "Book_author":Book_author,1 {' _2 ?4 p- _- m4 t3 \+ i- ~
  263.             "Book_Introduction":Book_Introduction,8 D6 F" y. N8 F# q  f
  264.             "Book_Synopsis":Book_Synopsis,
    + V  a- x6 U4 ^7 |) c/ r
  265.             "Book_Palabras":Book_Palabras,
    1 M  u/ Z- R( z$ d; ^0 K
  266.             "Book_img":Book_img,4 Y- t+ ?& H- E* S" Z6 P
  267.             "Bookurl": response.url,6 [! F$ }* E3 ?, e! A
  268.             "Booktitle": Booktitle,9 K2 H1 E! P) R7 G
  269.             "BookID": BookID,
    # W, g- d0 M! ]# m; f" W
  270.             "BookConte": BookConte,' j" r6 _2 D5 }' [
  271.             "Titleid": Titleid,8 J' D- P% j; x( u2 f1 P- Y
  272.             "abover":abover,4 N4 S' S3 t- V6 w/ y* |/ D
  273. #            "Book_Date" = str(datetime.datetime.now()),
    * m7 P% N0 @7 i7 r( H
  274.         }
    5 G5 i: z9 ~/ K& s
  275.     def download(self, P_dir, imgDir, file_name, Book_img):+ t- b/ ]1 }; {8 J
  276.         if not os.path.exists(imgDir):   p) n" Q+ V% f$ O4 ?8 y* k
  277.             os.makedirs(imgDir)
    : [1 K& y, `4 y+ _
  278.         file = imgDir + "/" + file_name6 w- \( |0 |1 x) [: d. n
  279. #        print file
    ; w  W% A. p& Y/ [1 n
  280.         f = open(file, 'wb+')
    ' x  M+ _; n% L, G7 h
  281.         imag = requests.get(Book_img)
    / M' }$ R  A0 c$ O& ~7 F0 h
  282.         f.write(imag.content)
    2 x& [  E4 }% @3 G% S- S" Q6 R
  283.         f.close()6 B4 e  A7 u' H5 _" I; W
  284.         #保存图片前4 ]" n" Q4 G. T' {
  285.     def save_imgs(self,response):
    - O9 Z/ T) g# w. D7 {& G* s
  286.         content = response.content
    6 S1 ?+ S+ ?2 f; [( ~5 i6 x. Y! i
  287.         file_name = response.save["file_name"]1 G  e9 h9 V# V% K+ s& g0 N  _0 Y1 t
  288.         imgDir = response.save["imgDir"]
    4 w. d, L  `4 z- V! ~- C8 S6 E
  289.         file_path = imgDir + file_name: y% N% i! }! H2 ~+ O( U2 N9 W
  290.         self.save_img(content,imgDir,file_path)
      l' y0 p+ N( q1 }/ {
  291.     #保存图片+ [/ V/ f$ D" W3 j
  292.     def save_img(self,content,imgDir,path):6 N3 h/ V# F' Q4 a( Y
  293.         if not os.path.exists(imgDir):                        
    6 ?5 p6 l5 ^+ V8 W7 D
  294.             os.makedirs(imgDir)6 [, }; a/ }- _2 ]) T3 x* J
  295.         f = open(path,"wb" )( G( }) k4 s& e. t
  296.         f.write(content)
      B: D4 S9 N8 V$ y, T! M& x/ E$ @
  297.         f.close(): b# ?1 x! \0 c. z& ^% f
  298.     #获取url后缀名
    9 f& C5 B$ R! P) Z; ]% L
  299.     def getExtension(self,url):                           
    + c2 T4 f  e: |& W5 k) z7 {, p
  300.         extension = url.split(".")[-1]  w# n' D4 s5 i) Y
  301.         return extension
    * C% D7 f$ K& u+ p# q: k
  302.    
    7 s9 C* Z# l9 B" X' P: M# M
  303.     #获取图片名1 H8 a5 ]. h! T$ Q* D5 Z: `" N
  304.     def getname(self,url):8 s1 [" `8 E% {& Z7 U1 _
  305.         name=url.split("/")[-1].split(".")[0]
    - A" E7 M/ f3 i) G! c8 k4 V. [
  306.         return name
复制代码

5 Q$ Z8 ^% B9 p1 n: V
/ G( Y9 V' G2 ~% s
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-2-5 23:52

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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