找回密码
 注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
发表于 2019-6-8 23:06:07 | 显示全部楼层 |阅读模式
Python + pyspider某小说站的爬虫,入数据库,火车头发布,资源下载到本地,另可写爬虫!& E1 r& ]- x; |3 k  @  G8 A  K
  1. #!/usr/bin/env python3 o5 W- t1 I9 \* y' h
  2. # -*- encoding: utf-8 -*-
    ! N) _, b) ?1 D5 h5 d, d# E
  3. # Created on 2019-05-05 21:43:11( c7 H! x* @; l+ O# b4 {
  4. # Project: XiaoShuo
    1 l: I( G  h: O4 w
  5. ( y, s! m3 d9 M+ W$ k' y  r
  6. from pyspider.libs.base_handler import *
    ' v! c* e! a+ }. t2 Z1 A* x
  7. import pymysql
    ) p+ O8 N0 B: Y& Z0 K7 ]
  8. import random
    * j7 E' h# q0 Y2 r7 A( O, J7 a
  9. import datetime
    " k2 l6 r0 ^( ^" I: F
  10. import urllib2,HTMLParser,re
    7 F" f# k6 S1 q& N- ~
  11. import os9 }7 c9 Q3 |% k% L
  12. import sys9 J. U6 [* X" U# p) A& v' M% e: a
  13. import re
    % Q2 v7 d9 @6 u7 ]
  14. import codecs
    ! @4 B9 i, V2 z! F9 s
  15. import requests
    7 x' x1 |; p- A. f) a* w
  16. import json' }9 Y' L% W, [

  17. 4 p% F/ X+ p6 V% G2 M8 V( b8 Y$ n
  18. class Handler(BaseHandler):
    ; {6 `# }) B2 h; M, X9 s! x/ h
  19.     global Datos1 C. u% B9 R5 {! |) ^& v' I/ z' y
  20.     global P_dir    8 \6 ?' _; k+ B. z' R
  21.     P_dir = '/Tools/Debug/'  #采集时候图片保持到本地的路径* H7 p$ _. i+ \8 \# D& k0 H) [
  22.     global Datos
    ( i; Y/ w& p. F. j  V7 e
  23.     Datos = {}
    8 |: d+ x( Z8 m+ C0 M3 c
  24.     headers= {/ Y% F* P. x! i, _) c9 [" I, b4 G, w, {- H
  25.     'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',% V# J/ }7 L- m' b; V
  26.     'Accept-Encoding':'gzip, deflate, sdch',+ h; T6 \7 ]& V! p! y
  27.     'Accept-Language':'zh-CN,zh;q=0.8',. C" {- \9 h& H2 P$ S
  28.     'Cache-Control':'max-age=0',: r6 I/ Z0 r4 ]' j8 E* S
  29.     'Connection':'keep-alive'," y( o0 \" `" t4 B
  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'
    ! c  U8 q: L: B6 y. B; e+ G' b
  31.     }$ |3 ~& \  p. k+ J/ E+ _
  32.     crawl_config = {
    " x! D! ~" v, T% k
  33.         'headers' : headers,
    5 X* f) ~7 O4 \% y9 n0 {; j
  34.         'timeout' : 300
    $ r- g) T- j! x: s6 ~% Z
  35.     }
    + T4 ~% L/ j% K' k3 p
  36.     def add_BookFile(self,Bookname, BookIDs, img, Locaimg, Book_Dates):
    % _  T5 n0 {9 c
  37.         db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8"): L& W2 P% J9 n4 u
  38.         try:3 z  G6 j" G! a+ c9 i
  39.             cursor = db.cursor()* X* r7 @& ?2 N1 R! q9 Z) ~
  40.             #注意此处字符串的占位符要加双引号"%s"
    ! }; q, k) G4 e
  41.             sql = 'insert into BookFile(Bookname, BookID, img, Locaimg, Book_Date) values ("%s","%s","%s","%s","%s")' % (Bookname, BookIDs, img, Locaimg, Book_Dates);
    2 h+ A5 P6 C  d  x5 s
  42. #            print(sql)
    5 A" B: \3 y+ E. \( ^' v, E
  43.             cursor.execute(sql)  c7 K" ]' ~& {4 n% S: z
  44.             
    ! h' G& K6 v5 h% f% q
  45.             #qid = cursor.lastrowid
    . e" D7 T8 S, Z4 C: Q1 ]# M3 q
  46.             #print(qid)
    . e/ K4 n1 x4 ^+ d
  47.             ) a- z5 F+ }- l/ L7 u  F# o% w8 c
  48.             db.commit()5 L4 \' s, L5 R+ @/ b
  49.         except Exception as err:
    & o) E0 O2 H6 C- a1 k
  50.             print("Error %s for execute sql: %s" % (err, sql))& j- x9 W4 a# l3 q
  51.             db.rollback()6 H: o  C2 V- y
  52.     def add_comment(self,Bookname, Booktitle, BookID, Titleid, Book_Date):2 A; Q1 v" t; k8 d) K
  53.         db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8")3 h4 C/ S. h3 a; P" g
  54.         try:
    5 D7 Y/ n7 {4 _) K3 t6 D
  55.             cursor = db.cursor()
    / Y, ]1 o" p" C' Z4 z
  56.             #注意此处字符串的占位符要加双引号"%s"
    ' y8 U% Y# W- l7 G/ {! n) ?3 \/ P0 Z
  57.             sql = 'insert into BookTitle(Bookname, Booktitle, BookID, Titleid, Book_Date) values ("%s","%s","%s","%s","%s")' % (Bookname, Booktitle, BookID, Titleid, Book_Date);7 b. y0 ]8 M$ q: X
  58. #            print(sql)
    . l, d0 f) e1 P' E% V1 @
  59.             cursor.execute(sql)# A0 n; u% w3 F; v! [9 T# D1 {
  60.             " J' R. D3 }4 Z$ E/ e
  61.             #qid = cursor.lastrowid! @2 n$ j/ M8 {
  62.             #print(qid)- [4 R# I, X$ @6 Q  F
  63.             $ a& w* S2 H; M0 \' G
  64.             db.commit()/ Y4 {  Y; ?- O% V1 |7 t: h! P
  65.         except Exception as err:8 i% u! |2 y- X. r' w- C
  66.             print("Error %s for execute sql: %s" % (err, sql)), ^/ k. _$ ^7 h6 q( p/ N
  67.             db.rollback()6 X3 F- Z+ T$ t# c8 i2 z
  68.     def add_question(self,Bookname,Cater_Name,Book_author,Book_Introduction,Book_Synopsis,Book_Palabras,Bookurl,Booktitle,BookID,BookConte,Titleid,abover,Book_Date):
    2 Z  S( W. W4 t% m
  69.         db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8")
    $ ^, n, S$ i! B% t8 [
  70.         try:0 i$ d8 x; }2 I0 h- k
  71.             cursor = db.cursor()8 S3 _) R+ j1 \; w0 b
  72.             #注意此处字符串的占位符要加双引号"%s"
    6 j; O0 G$ W3 u7 n- M
  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);
    + I( b4 G9 T* m$ ~/ }  u% b1 p; v, P
  74.             print(sql)1 _: s" x5 L+ C" ?
  75.             cursor.execute(sql)
    ) v: G; h+ j/ c& Y& a: o/ i
  76.             print(cursor.lastrowid)
    7 {  C% j9 x1 a  c
  77.             db.commit()8 J9 V/ l1 o) U! g
  78.         except Exception as err:4 Y4 _5 p  J! W; R
  79. #        except:
    " K2 `8 B! k' _+ U% E4 |3 M
  80. #            print('Failed')
    $ T- l; c3 T& x6 K2 J2 Q
  81.             print("Error %s for execute sql: %s" % (err, sql))
    4 H; r' n* B  B. ^( U5 V
  82.             db.rollback()
    9 P6 ~7 }: ?% a& b& r. a
  83.         $ d" b5 ?. X  B7 F
  84.     def add_locoy(self,Bookname,Cater_Name,Book_author,Book_Introduction,Book_Palabras,Book_img,Booktitle,BookConte,abover): - q  Y  ^! v: M, ?
  85.             reload(sys)
    3 ~2 }0 J$ F( w) A
  86.             sys.setdefaultencoding("gbk")
    6 {' v0 v% N$ i  ~4 x2 k( }( l
  87.             locoy_url = 'http://www.******.net/locoy/?my=book'  #697火车头发接口地址, |" s0 R. b( t5 B- K/ x" x
  88.             locoy_data = {
    : e7 V. J: \$ l) N( [( Q( E
  89.             'my_u':'用户名',   #后台用户名
    & W8 }  Y) r" f, j# ~, r
  90.             'my_p':'密码',   #后台密码! ~2 ?, X( D" O
  91.             'subject_669977_net':Bookname.encode('gbk', 'ignore'),) M2 S/ y) A$ }9 l; L/ V
  92.             'caid':Cater_Name.encode('gbk', 'ignore'),2 q# X  W7 Y& T4 z) ~# D
  93.             'title_669977_net':Booktitle.encode('gbk', 'ignore'),
    & V' o( J7 d5 s- s* m/ Y9 I
  94.             'article':BookConte.encode('gbk', 'ignore'),  Y- v8 k* |$ e$ j' E
  95.             'author':Book_author.encode('gbk', 'ignore'),
    + U% n- w- S4 W
  96.             'ready_1':Book_Palabras.encode('gbk', 'ignore'),2 i$ }$ o4 _. h, z) E! d
  97.             'thumb':Book_img,2 X: b7 J0 o5 K# n
  98.             'content':Book_Introduction.encode('gbk', 'ignore'),
    8 o% n* E" R- P7 B, n% `' N
  99.             'abover':abover.encode('gbk', 'ignore')           
    ' r! j! D  Q, w3 @: k% x3 w
  100.                 }
      N# U! I  }1 C; i' Q; s, U
  101.             res = requests.post(locoy_url, data=locoy_data)
    % C  W3 H3 L/ L" o2 l8 r" W! l
  102.             print res.text7 h. g6 K( I' }- [: |7 g8 f
  103.             print res.content
    7 A& _; f" f3 y
  104. #            print Dsd! a9 w' \4 F8 ]+ i
  105.             return res
    , R. l/ X1 r* Q/ j
  106.    
    " ]9 u. m4 V6 [2 P$ |" a% l
  107.     def __init__(self):
    4 H8 C( O, E* W5 b5 O
  108.         self.base_url1 = 'https://www.****.cc/'
    1 @( u. k6 h( O# z9 p$ y& _
  109.         self.base_url2 = '/'
    1 f6 i+ G. \$ \- v* f
  110.         self.CaterId = []- v& E6 L# Q9 {$ N$ J+ x' s
  111.         self.CaterIds = ['xuanhuan', 'wuxia', 'yanqing', 'lishi', 'kehuan', 'kongbu', 'nvsheng']7 G4 z/ n6 b" h1 r9 F
  112.         self.page_num = 13 E; W  P) @6 _8 G4 e+ e% ?- r
  113.         self.total_num = 200   6 U4 i3 [, p2 ]
  114. % W( f0 b- W6 W) X  }
  115.     @every(minutes=8 * 60)
    1 s2 S% N6 h" z$ A# l
  116.     def on_start(self):
    . M% ~1 L1 \: |$ h! S
  117.         global Cater_Name- V( R' l* x6 A/ D! ^8 P- O! i/ @# m
  118.         Cater_Name = []
    0 M/ {: u0 ~; F" F$ P  f/ \
  119.         while self.page_num <= self.total_num:
    ; U9 C4 ]3 ]6 ~" \1 N% Z7 G" I( G7 [  [
  120.             for self.CaterId in self.CaterIds:9 b4 X* k6 k3 A! O: g; ]
  121.                 if self.CaterId  == 'xuanhuan':7 L5 D1 P' _5 ^$ e: [$ V% Q9 v
  122.                      Cater_Name = '玄幻'
    ! q; t- X+ q& u( K- L* f6 B" `
  123.                 if self.CaterId  == 'wuxia':1 O2 }6 t  L9 k! B! [
  124.                     Cater_Name = '武侠'
    - E5 \& B# h: W# E
  125.                 if self.CaterId  == 'lishi':
    % t9 F. z# T4 W% J0 y7 J( _8 g3 b
  126.                     Cater_Name = '历史'            
    . ]8 g" D/ W5 y9 m0 N+ g6 F
  127.                 if self.CaterId  == 'yanqing':" K. Q- j' G2 o5 l& [) U8 e2 W
  128.                     Cater_Name = '都市' 8 g3 r7 c. l' o$ `# Z  a* N
  129.                 if self.CaterId  == 'nvsheng':
    ; V( J# q% h; m5 S: k+ E9 u2 ^& A
  130.                     Cater_Name = '都市' + n# C' N8 v6 p2 D7 S
  131.                 if self.CaterId  == 'kehuan':
    ' B' x+ f" Q7 o3 i8 N
  132.                     Cater_Name = '科幻' # |1 @7 G( c0 y
  133.                 if self.CaterId  == 'kongbu':
      D1 z4 @/ x" X1 O( j4 c2 O' C
  134.                     Cater_Name = '游戏' + V2 M% t) t9 V1 [% {
  135.                 print self.CaterId
    3 `4 k- }+ R/ O# A
  136.                 url = self.base_url1 + str(self.CaterId) + self.base_url2 + str(self.page_num) + "/"          0 i1 n' P) \1 }
  137.                 self.crawl(url, callback=self.list_Caterg,save=Cater_Name)8 E( N  C% |1 r: [6 G
  138.             self.page_num += 1 $ l. n# x$ I0 }  [2 D- _. G
  139.             & K/ n& U- Y! T2 c' r' E) y
  140.     def list_Caterg(self, response):1 s; p: j' g$ }9 T
  141.         Cater_Name = response.save
    $ v4 {# S& L0 M" B% b
  142.         for each in response.doc('.pic-list a[href^="http"]').items():
    $ l  }: A2 Z/ t
  143.             self.crawl(each.attr.href, callback=self.list_Caterg_detail,save=Cater_Name)2 L  s+ e8 b5 Y5 i6 E0 M
  144.             ; O, u! k$ p7 N2 H9 j2 q9 O
  145.     def list_Caterg_detail(self, response):0 [% g1 J) I: X/ B
  146.         Cater_Name = response.save
      r$ A( u' f! R4 o
  147. #        print Cater_Name
    8 ]/ i  S4 Y! T# U2 m) j
  148.         Bookname = response.doc('h1').text()
    ' z; v* l1 l0 S1 ?/ ]
  149.         print Bookname! U1 e& e; S+ ^8 j8 t9 S
  150.         Book_author = response.doc('.authorname > a').text()6 V& i1 I2 \/ O- f7 {
  151. #        print Book_author2 x* X8 A; U  P3 f. C
  152.         Book_Introduction = response.doc('.book-intro > div').text()
    * o* l2 ~/ K  \/ n% \0 i
  153. #        print Book_Introduction
    ' \7 }0 @+ ]0 j5 u4 y+ [$ G6 q" H
  154.         Book_Synopsis = response.doc('b').eq(1).text()
    - {* X0 v9 l7 ~9 K& |
  155. #        print Book_Synopsis
    $ \1 x; D5 @0 I' l
  156.         Book_Palabras = response.doc('.booktitle p').text().split(' ')[1].split('|')[0]
    ( ^! X3 l  R9 I6 G
  157. #        print Book_Palabras
    5 ?& v2 W% T+ G+ G7 g8 J/ C
  158.         BookIDs = response.url.split("xiaoshuo/")[-1].split("/")[0]   #小说ID
    9 K- @$ \! @" z7 y( z
  159. #        print BookIDs
    , `# O8 C/ A% F. @/ ^. t, `
  160.         Book_Dates = str(datetime.datetime.now())         / N5 z8 Y" E9 l5 G3 M& G) h9 p$ c
  161.         for imgs in response.doc('.bigpic > img[src^="http"]').items():
    * V5 z" R5 I1 [/ q9 f
  162.             img = imgs.attr.src
    4 p% G4 O+ s$ Y; _1 H
  163.             print img
    % ~6 I6 p) d. H3 N& g
  164.                 #小说封面下载" X$ y9 ~# f' l4 c1 D; W
  165.             extension = self.getExtension(img)
    7 V! y3 O4 b7 |
  166.             name = self.getname(img)
    $ Q6 i$ A1 N* u! t6 y$ }
  167.             file_name = name + "." + extension$ C& \% b" S& P' Q  {# u
  168.             imgDir = P_dir + name
    / \! I4 \' c" A6 E7 j' T: C
  169.             Locaimg = imgDir + "/" + file_name& Z4 _) g, g; w* r. [
  170.             print Locaimg
    0 a+ [/ t( [" X
  171.             if(self.download(P_dir, imgDir, file_name, img)):   #这2行可注译,图片下载到本地
    : H2 V  C1 P% _2 e; |* V4 Q4 _( J
  172.                 print('attachment url is ' + img)               #
    8 f1 M" q+ n* r# t0 c
  173.             Datos = {
    5 S7 r. ^) A3 J
  174.                     "Cater_Name":Cater_Name,2 [( x1 X  [; ~- N, O% W2 s
  175.                     "Book_author":Book_author,$ f7 h2 q- {  M7 [1 T2 m" [6 G. u
  176.                     "Book_Introduction":Book_Introduction,
    7 I& T; I) B; k
  177.                     "Book_Synopsis":Book_Synopsis,4 s% f1 [/ X& z5 l) t
  178.                     "Book_Palabras":Book_Palabras,$ H6 _  L$ G. A; k4 W1 `
  179.                     "img":img,
    3 P8 |7 E& U! P/ ~" S6 y
  180.                 }# C0 {" M& r/ t6 Q) i8 f
  181.             self.add_BookFile(Bookname, BookIDs, img, Locaimg, Book_Dates)  #这行可注译,数据库发布接口,方便其他系统的发布
    7 F2 M( @+ h. R2 D- A3 Z7 @
  182.         for each in response.doc('div[class="bookbtn-txt"]  a[class="catalogbtn"]').items():
    ! V: @! A8 |$ {9 z
  183.             self.crawl(each.attr.href, callback=self.index_page,save=Datos)7 R3 n; H# o# y1 p2 {
  184.             
    : n+ i6 d4 w2 N: X; s
  185.     @config(age=8 * 60 * 60)   
    4 t5 S/ Y% K- e  T# q2 m
  186.     def index_page(self, response):
    + J) C2 s0 U( u) U9 Z
  187.         Datos = {: g7 O- }6 @" V* c3 }* ?
  188.                   "Cater_Name":response.save['Cater_Name'],; M& a. q" s# V6 j
  189.                    "Book_author":response.save['Book_author'],: y/ k3 ^: S# p
  190.                    "Book_Introduction":response.save['Book_Introduction'],
    ( V$ ]$ b" ^+ U" X3 b
  191.                    "Book_Synopsis":response.save['Book_Synopsis'],
    8 E  u  |6 i0 s! x; h
  192.                    "Book_Palabras":response.save['Book_Palabras'],# {) X7 _5 _& w$ Y7 K3 q# I
  193.                    "img":response.save['img']," v7 F! g- d; V$ S8 z' p
  194.                      }
      ~1 k3 W( s1 @( M' d5 l4 @7 g
  195.         for each in response.doc('.chapter-list li:first-child a[href^="http"]').items():
    ' q7 {7 @* y3 _9 a0 \0 P4 w8 y
  196. #        for each in response.doc('.chapter-list  a[href^="http"]').items():  
    . E' k; N8 T8 Q! W5 f7 }; W
  197.                     self.crawl(each.attr.href, callback=self.detail_page,save=Datos)0 h& a- i  d! }0 K! \  z2 u! E
  198.     @config(priority=2)* L3 O% a4 z0 R5 |& ~
  199.     @catch_status_code_error
    ! ]) S: a2 v! Y9 V" f
  200.     def detail_page(self, response):        # d: ~% `8 E0 p
  201.         NewRe1 = u'哈书'
    3 K# c/ ~. A0 c
  202.         NewRe2 = u'huhjsd.CC'
    9 Z; _7 T0 f3 {: W7 e6 c4 j
  203.         NewRe3 = r'^\\n\\n'
    3 {% i1 M; H3 X3 E0 c6 K
  204.         NewRe5 = u'小说网'
    % t5 V; r! X* |& `7 l- v
  205.         NewRe6 = u'fgdfgf'+ v7 l7 e$ L& ~: |: u9 {: R/ l
  206.         NewRe7 = u'fgfgf'
    ! [7 _0 V! G+ l4 ?
  207.         NewRe8 = u'ffhgf'
    7 F& x" s7 _( ~9 h* |6 |
  208.         NewRe4 = r'[\f\t\v+\.\{\(\)\}\!\/_,$%^*(+"\')]+|[+——()?【】“”!,。?、~@#¥%……&*()]+'
    * S8 q$ q" b7 ?2 }& {3 W5 J
  209.         ReC1 = u'静思'" a  A  \' R9 I/ D. _
  210.         ReC2 = u'aghgf.com'& V7 j) i, c: @3 S4 J& M
  211.         ReC3 = u'aghgfh.com'
    8 ?+ ^  M1 u: _- W
  212.         ReC4 = u''
    + j' K& e$ Y0 @" L8 C" q
  213.         ReC5 = u'文学网') L" u1 U/ U. M
  214.         ReC6 = r'<BR>'
    6 u+ x: e" Y0 d7 z: U, i7 n6 x
  215.         Bookname = response.doc('.readlocation a').eq(2).text()   #小说名称& x% i' x8 F2 c3 u  ]& t
  216.         print Bookname
    3 y6 }! B1 I0 G# R1 P
  217.         Cater_Name = response.save['Cater_Name']   # 小说分类. F2 ^3 f6 T; D, S
  218.         Book_author = response.save['Book_author']   #小说作者6 G  C% m  }3 j7 n* {7 Z7 H
  219.         Book_Introduction1 = response.save['Book_Introduction']   #小说简介
    7 C$ q3 ]* N9 k' J% N
  220.         Book_Synopsis = response.save['Book_Synopsis']   #最近更新
    2 S* M' C4 F' J" e: C1 M9 K. E
  221.         Book_Palabras = response.save['Book_Palabras']   #小说字数
      Y: }& i( G, h1 }# ]9 F
  222.         Bookurl = response.url   #小说网址
    ' E5 \( m6 @' j) b' @9 r
  223.         Booktitle = response.doc('.article-title').text()   #章节名称
    & Z, f3 G5 a7 j% Y9 O
  224.         BookID = response.doc('.readset-r span').text()   #小说ID
    4 p8 K! d) e! F
  225.         BookConte1 = response.doc('.article-con').text()   #小说章节内容
    5 u( [" t/ [3 J1 D# O1 h
  226.         abover = response.doc('.article-title').text() + response.save['Book_Synopsis'] + response.save['Book_Palabras'] + response.save['Book_Introduction']   #小说状态(连载还是完成)3 e2 p0 l' {2 W
  227.         Book_Date = str(datetime.datetime.now())    # 采集时间& l7 W- W  i$ b6 Y! L* d& j5 [
  228.         BookConte2 = BookConte1.replace(NewRe1 , ReC1)- W& i* a$ h% y" W2 ?! |
  229.         BookConte3 = BookConte2.replace(NewRe2 , ReC2)+ G: ^$ a9 @; d% O- d
  230.         BookConte5 = BookConte3.replace(NewRe5 , ReC5)
    1 X+ Y  x' N- C0 m' L8 s1 K0 I+ G
  231.         BookConte6 = BookConte5.replace(NewRe6 , ReC2)/ j0 ^0 j7 z0 m' i; Z. m6 e6 A' H
  232.         BookConte7 = BookConte6.replace(NewRe7 , ReC2)
      z' ^) m$ `3 S8 i9 m" ?
  233.         BookConte8 = BookConte7.replace(NewRe3 , ReC6)+ D& v5 Y. M* Z2 K- P1 P: G
  234.         BookConte4 = re.sub(NewRe4 , ReC4 , BookConte8)- w! z/ M$ W8 `9 A/ m0 t1 D2 G
  235.         BookConte = BookConte4.replace("\n\n","<br>")2 A% L' G, |0 [' O7 z
  236.         print BookConte
    , w1 ]4 F6 Y! ]  L6 v% P
  237.         Book_Introduction2 = Book_Introduction1.replace(NewRe1 , ReC1)
    $ Q5 R+ O' Y4 u& X8 N. w" E
  238.         Book_Introduction3 = Book_Introduction2.replace(NewRe2 , ReC2)+ ?  w0 L  z: ]$ T
  239.         Book_Introduction4 = Book_Introduction3.replace(NewRe3 , ReC3)
      }* M! V; B2 |0 W/ d3 p2 b# m* g
  240.         Book_Introduction = re.sub(NewRe4 , ReC4 , Book_Introduction4)
    7 m! D! C: F8 l
  241.         Titleid = response.url.split(BookID + "/")[-1].split("/")[0]     
    ! T1 F( a- l8 i- o3 E
  242.         Book_img = response.save['img'],  #小说图片7 Z5 e1 v- \. B# s3 p* H4 m) X7 j- W4 V
  243.              * c" D. _. Y( H6 W" X+ D3 X- [! m
  244.         #insert into MySQL 小说入库/ B/ G2 b8 q0 @) i* G
  245.         self.add_question(Bookname,Cater_Name,Book_author,Book_Introduction,Book_Synopsis,Book_Palabras,Bookurl,Booktitle,BookID,BookConte,Titleid,abover,Book_Date)   #这行可注译,数据库发布接口,方便其他系统的发布- v; T5 L3 W: W. P7 c% _
  246.         self.add_comment(Bookname, Booktitle, BookID, Titleid, Book_Date)   #这行可注译,数据库发布接口,方便其他系统的发布
    ! h4 ?3 k. N& K( x! G( Z6 }! K# A
  247.         #post提交发布
    . j. J( v* g" s) Y4 k0 w/ n
  248.         self.add_locoy(Bookname,Cater_Name,Book_author,Book_Introduction,Book_Palabras,Book_img,Booktitle,BookConte,abover)  #这行可注译,火车头发布接口,不需要可取消
    9 t: Z4 w# [9 g+ @
  249.         Datos = {- l' A# R7 B9 m4 c# v/ }" F- o) j5 \
  250.                   "Cater_Name":response.save['Cater_Name'],  ]) o% G- F# m, G) w7 F7 G
  251.                    "Book_author":response.save['Book_author'],
    1 c& f/ ], O; O
  252.                    "Book_Introduction":response.save['Book_Introduction'],! G. G8 V. _# [% }' U( s+ W
  253.                    "Book_Synopsis":response.save['Book_Synopsis'],3 k3 l; j1 l& x/ e  w' q0 _
  254.                    "Book_Palabras":response.save['Book_Palabras'],
    + m% y! @+ C, I3 o
  255.                    "img":response.save['img'],
    7 j- e4 n. @) V: Z) O( o/ h- t
  256.                      }3 k0 A+ E0 e: C* e) N
  257.         for each in response.doc('.articlebtn > a:nth-child(4)[href*="/xiaoshuo"]').items():. Z% G: I1 Y0 Z
  258.             self.crawl(each.attr.href, callback=self.detail_page,save=Datos)
    ' u  l/ k* P2 B# q  \/ [6 K
  259.         return {. G' N' i0 u% }2 e7 U
  260.             "Cater_Name":Cater_Name,
    % m- I) ^2 b! ~/ O; p: h1 B* ~/ \6 n
  261.             "Bookname":Bookname,* s1 k  T0 `) `& k( y" r, `
  262.             "Book_author":Book_author,; g  V3 V9 }8 g) e4 |
  263.             "Book_Introduction":Book_Introduction,# ]2 h8 x2 o! e: a, e
  264.             "Book_Synopsis":Book_Synopsis,6 M. U8 F+ X/ _% v) @8 B. N
  265.             "Book_Palabras":Book_Palabras,) F* i1 O! @% U' m3 E# r
  266.             "Book_img":Book_img,# e, F2 a4 f1 l$ p4 f1 R# E
  267.             "Bookurl": response.url,) l- r4 y( P" q& F" i
  268.             "Booktitle": Booktitle,
    2 K! ?6 h1 e" ~7 P/ M5 Z
  269.             "BookID": BookID,* `9 E% i0 n! W4 s# s
  270.             "BookConte": BookConte,$ @" \% N- m" g8 R; p2 f% v5 E0 @
  271.             "Titleid": Titleid,
    5 t5 ], h. a: e/ [0 W- Y
  272.             "abover":abover,
    - Z$ b) N; v0 j8 E, g' U9 o
  273. #            "Book_Date" = str(datetime.datetime.now()),
    5 ]  H! U3 i0 U0 D# K- d, U  P9 d$ r  n
  274.         }$ l+ ]( a& z& U( d5 r/ c3 J& n
  275.     def download(self, P_dir, imgDir, file_name, Book_img):
    * W2 q# K+ b# ?
  276.         if not os.path.exists(imgDir): 0 z3 R! d4 U! j# M
  277.             os.makedirs(imgDir)
    & V$ E3 D) T: x* W$ q( c
  278.         file = imgDir + "/" + file_name9 e1 W) [* G. d+ z& M" y5 v
  279. #        print file
    $ B& b# g1 c/ Q$ ]
  280.         f = open(file, 'wb+')8 Y" g3 m1 B7 m
  281.         imag = requests.get(Book_img)
    5 l& n* f* c9 t) g5 ^+ [
  282.         f.write(imag.content)
    , j) y6 M; D( {( X0 V" g( W
  283.         f.close()1 y' ^$ X' Y( @  G3 c
  284.         #保存图片前
    ' c$ V1 ?1 ^; T0 t& f3 ^
  285.     def save_imgs(self,response):( X7 \- a& E7 c9 e0 w( O
  286.         content = response.content- b1 v, I/ k  v- l; g; x% }( Z
  287.         file_name = response.save["file_name"]$ E# Q  l" s  ~; @
  288.         imgDir = response.save["imgDir"]
    8 s1 k8 R9 E$ f& Y) \" Q$ k1 f
  289.         file_path = imgDir + file_name( x  y8 a1 N6 d) s9 Q
  290.         self.save_img(content,imgDir,file_path)
    " ~3 I+ `! w9 c) \7 I
  291.     #保存图片5 }- ?5 v# i" M; V* r1 @5 M, x
  292.     def save_img(self,content,imgDir,path):2 ^- u" Z* n! Y: t- U* h( ^
  293.         if not os.path.exists(imgDir):                        
    + U( g( C2 q- g% `' J2 M" m/ W' Y! n
  294.             os.makedirs(imgDir)
    % o% I' x2 s- H# g& \* Z% U
  295.         f = open(path,"wb" )) n2 T# g5 \8 l* B" U+ L0 U- a3 Z' F% a
  296.         f.write(content)
    . T1 Y5 z  S8 U4 L) n+ c! g- d
  297.         f.close()% \( o$ l7 h1 n2 v) k9 y( R- _4 \% c
  298.     #获取url后缀名' }$ {1 C- W* T/ m8 F$ G. S
  299.     def getExtension(self,url):                            " n& ^  S% R! K- O) j
  300.         extension = url.split(".")[-1]  i* r7 |7 Z" v9 o6 R. K# a" ~, f
  301.         return extension
    9 J$ Q2 t2 U' d3 A7 b  L) u. L
  302.    
    ! E. V3 h% L" Q7 }
  303.     #获取图片名5 T8 k" ?! B8 t3 q$ V
  304.     def getname(self,url):9 k9 n% e; @, c9 \
  305.         name=url.split("/")[-1].split(".")[0]
    ( v" X( A  u) K: s0 h1 H) y- S  Q
  306.         return name
复制代码

  b' m, Y$ W0 e3 F! y& W. f
* U7 d: h! ^. M% |5 o; |) a1 Z
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-7-26 12:42

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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