找回密码
 注册

QQ登录

只需一步,快速开始

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

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

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

2 C2 i% Z. n$ {4 y6 A" X2 B9 N! w( [
9 s- g; _% w6 r
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-8-24 06:21

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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