Python + pyspider某小说站的爬虫,入数据库,火车头发布,资源下载到本地,另可写爬虫!1 z/ [: `4 [1 w, x: N% H
- #!/usr/bin/env python& W1 W) i3 d' U+ s7 O v
- # -*- encoding: utf-8 -*-5 F' y% Z9 a. X* w4 U2 C# _
- # Created on 2019-05-05 21:43:11
O7 q$ n# M2 C7 } - # Project: XiaoShuo% |3 T" n" u' j3 p5 W
- / U1 Y# C6 j" T: e9 V' c) J4 @, }8 H7 a
- from pyspider.libs.base_handler import *6 K$ l0 z( Y- ~5 I3 c6 i2 y
- import pymysql: X( w( [3 ], m/ i
- import random
$ @9 H: A" {4 @+ r- o& q - import datetime; r, x3 C% H1 O1 W
- import urllib2,HTMLParser,re) ^; J" C+ V5 B: C. z# ?
- import os
* {+ ?9 u1 g! C" M4 J1 S' ~ - import sys
0 F( X4 v. K9 ~; r5 w' h) E - import re
- o v9 h3 R' D1 \ - import codecs7 }+ V( g1 ?/ S$ W; _
- import requests' u. ~* ]: C4 y, E/ B( G, w5 j5 ]
- import json
/ S; l# v! m9 {7 ?8 z -
* ~. A6 }; E- ?1 o+ i1 Z - class Handler(BaseHandler):
% R' @, C2 K4 I0 `- I# o& K3 y - global Datos3 D7 G( N+ u/ @6 \0 v' t
- global P_dir % |& ^* u( e- T4 }6 t- \$ z0 m
- P_dir = '/Tools/Debug/' #采集时候图片保持到本地的路径3 I$ U8 _. r" c7 B
- global Datos
3 r+ A4 F& V: U4 d: o - Datos = {}9 M) B+ C( v; i3 S% Q
- headers= {1 g1 P+ M, c7 D+ J( A- o' }
- 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',6 }8 Z4 {+ P( L. g
- 'Accept-Encoding':'gzip, deflate, sdch',
* a& Q. \: M( l$ c4 `9 |* G - 'Accept-Language':'zh-CN,zh;q=0.8',8 o% [' h! p1 { K9 o
- 'Cache-Control':'max-age=0',8 m* G% v; }/ y
- 'Connection':'keep-alive',1 x) q" K- I7 z7 B
- 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'( G! _. s7 a8 d3 g
- }: B8 {& v L. L
- crawl_config = {6 m+ a( L6 o) {, G- \- h
- 'headers' : headers,: b$ ^* z- Q0 T2 X, O1 b, `
- 'timeout' : 300
" A! }$ [' Y# Y6 b8 w! K - }
" W- Q! A( V! J: G5 F! J1 L) H - def add_BookFile(self,Bookname, BookIDs, img, Locaimg, Book_Dates):
* p% D. `+ C& m6 }$ n* O* I) } - db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8")
* w1 ^" w) e9 @ - try:# Y- v7 S& G: G# Z
- cursor = db.cursor()6 W, e9 {! g( `& W+ J0 D+ k+ s2 O- @. |
- #注意此处字符串的占位符要加双引号"%s"% {+ _1 ?" r) I9 c! `6 W: @
- sql = 'insert into BookFile(Bookname, BookID, img, Locaimg, Book_Date) values ("%s","%s","%s","%s","%s")' % (Bookname, BookIDs, img, Locaimg, Book_Dates);& T! [5 d. {8 s/ S3 ]% x7 [' E4 j
- # print(sql)1 H. n, O: X* L; x1 n* G
- cursor.execute(sql)3 F, Z" ?+ }" R/ Q
-
8 v/ O8 }/ |8 ?+ i( H1 N/ ^ - #qid = cursor.lastrowid
5 k+ E! B n0 r$ q7 V! G$ y) V - #print(qid)! f4 u# x+ |; n0 t; W" b
- : W: ~" V: W) M' a _0 t1 F; m
- db.commit() G) {: J" Q" a. u6 A' r# l0 N$ w4 G
- except Exception as err:
( Z& [9 F! [0 P; y. I) t - print("Error %s for execute sql: %s" % (err, sql))
* i0 X+ l- D3 q$ }5 H. C% ]' y - db.rollback() J7 \! f- {2 v: d% w
- def add_comment(self,Bookname, Booktitle, BookID, Titleid, Book_Date):3 B8 |8 c3 X' H0 Z1 b- [# B7 S4 R
- db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8")9 m0 D. u& x" r+ b6 ~$ r8 A
- try:
$ }& D3 M7 j1 l& Y% W$ r$ _2 m2 Q& N1 l - cursor = db.cursor()
6 F# d/ Q% t. I9 }7 r - #注意此处字符串的占位符要加双引号"%s"/ g7 J) N$ v! d4 {4 f/ p5 E
- sql = 'insert into BookTitle(Bookname, Booktitle, BookID, Titleid, Book_Date) values ("%s","%s","%s","%s","%s")' % (Bookname, Booktitle, BookID, Titleid, Book_Date);
1 w$ ?6 b0 v0 q6 z7 ~ - # print(sql)/ D. |+ e5 i0 c1 k8 a& ~4 h) P" y
- cursor.execute(sql)
9 L7 _* w: C. k -
3 l/ r C/ |! \! A6 A - #qid = cursor.lastrowid
- [0 _4 ~7 F* v$ E4 T1 I - #print(qid)/ s! D% X2 ]- a5 e" O, a8 o( Y1 G. h
- 5 I( ]" T" h6 Z8 N( ~/ O# @/ D
- db.commit()
8 l/ B( J" J7 }8 m* @ - except Exception as err:
8 h, ~+ t2 G3 H$ _. P* L - print("Error %s for execute sql: %s" % (err, sql))
! G' ^2 q# ]3 @ - db.rollback()
* n- K4 ~$ p+ a - def add_question(self,Bookname,Cater_Name,Book_author,Book_Introduction,Book_Synopsis,Book_Palabras,Bookurl,Booktitle,BookID,BookConte,Titleid,abover,Book_Date):5 S4 r2 A2 d3 e$ o# X
- db = pymysql.connect(host="localhost",user="数据库用户名",password="密码",db="数据库名",charset="utf8")
- Y, R; s6 o. Q v6 w( ? - try:1 m# Y& Z* y5 e5 f" U
- cursor = db.cursor(). {5 |4 b7 z5 M/ {8 r
- #注意此处字符串的占位符要加双引号"%s"- C; Q0 Z% M$ q5 [; B2 U
- 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);
" G) A9 \: F5 j% h9 c. j/ Q - print(sql)
+ ?7 A6 E; l# X9 L$ x* T" F: W8 D - cursor.execute(sql)
) s5 r1 E1 j8 U; V - print(cursor.lastrowid)
' x" H! N2 S! i% C- A3 c5 P- Y - db.commit()
! G K3 T5 ?! Z - except Exception as err:, [+ s: n$ Z( K" g ]
- # except:
% F- j2 ~% k4 x1 u2 [/ ^6 I) p - # print('Failed')8 Y2 ^0 E, }4 C" M* i7 |, p
- print("Error %s for execute sql: %s" % (err, sql))5 n) Q$ b: X) A4 O, K4 J
- db.rollback() U5 y1 j, {! v% o- E' Q# l) h
- * J# u9 @, w- j' u
- def add_locoy(self,Bookname,Cater_Name,Book_author,Book_Introduction,Book_Palabras,Book_img,Booktitle,BookConte,abover):
5 G4 x7 z4 G2 V - reload(sys): @2 |: q# K0 ~6 W z! T) f' [
- sys.setdefaultencoding("gbk")2 P3 h7 P+ G- C4 o \- B
- locoy_url = 'http://www.******.net/locoy/?my=book' #697火车头发接口地址
# }: N" c$ {- D5 o, D0 P& K. g: Q - locoy_data = {3 y- J7 {" P; A: `5 w4 [
- 'my_u':'用户名', #后台用户名" `, o) D, ~' K3 Q, b7 y+ U- c
- 'my_p':'密码', #后台密码8 k2 k8 W, u- e7 {2 I$ o
- 'subject_669977_net':Bookname.encode('gbk', 'ignore'),2 \: c0 r8 b6 c2 B: o0 Q. E
- 'caid':Cater_Name.encode('gbk', 'ignore'),( x' v* C1 Z/ y7 R4 Z
- 'title_669977_net':Booktitle.encode('gbk', 'ignore'),
1 F( x0 V9 ]) U+ w w - 'article':BookConte.encode('gbk', 'ignore'),5 {1 u$ c" f( F4 \% m
- 'author':Book_author.encode('gbk', 'ignore'),
3 |" O1 J+ [( J( g& P$ o0 n/ L - 'ready_1':Book_Palabras.encode('gbk', 'ignore'),( D0 g# E( g+ s: T! |' _; |
- 'thumb':Book_img,. G: F. t+ Y& o- T* m! O
- 'content':Book_Introduction.encode('gbk', 'ignore'),
4 d! G0 w: X0 z N# ] - 'abover':abover.encode('gbk', 'ignore') $ F: j9 t2 s1 j" K7 n+ U! J3 b9 B
- }
* J* h0 q' E. \. r$ ^ A1 B - res = requests.post(locoy_url, data=locoy_data). w/ x2 d. Z) q- R% b* D1 T A
- print res.text4 }, q' h/ Q$ z4 \! x" J+ Y
- print res.content9 Z/ w2 M& r9 A4 J
- # print Dsd; l1 D: Z- V; b; ~3 z) r d" n
- return res
9 Y/ u- k K7 g/ n* C- U+ o -
; Q% x% P& o! C" M7 W - def __init__(self):1 y: k/ q9 N) I6 _- I, K
- self.base_url1 = 'https://www.****.cc/'* O: O- R+ k& S: @% j8 j( c
- self.base_url2 = '/'
9 w5 ?, C( w7 L$ T0 Y! J3 d8 P - self.CaterId = []
4 n5 q% f# r0 \3 m% h: o: K- G( } - self.CaterIds = ['xuanhuan', 'wuxia', 'yanqing', 'lishi', 'kehuan', 'kongbu', 'nvsheng']! L& _8 C. h$ P/ s
- self.page_num = 14 }* X: D4 e! S2 w
- self.total_num = 200 & u% a! j) q/ M, q
-
. W2 ]$ [5 z7 W2 B: ^ - @every(minutes=8 * 60)9 ]6 b5 x8 e. c
- def on_start(self):
/ B2 n1 ~" h0 k6 T7 j - global Cater_Name
' B+ w }# [8 M* H+ z - Cater_Name = []+ f- p, c- i% L+ m; c6 j7 D) e+ F. a; w
- while self.page_num <= self.total_num:
* u9 X. C% K' |* S* o. O( p/ @+ z0 v6 d - for self.CaterId in self.CaterIds:; K5 b# L% V2 t' ]* K* b
- if self.CaterId == 'xuanhuan':
; N4 b4 \- p3 E+ w9 Q - Cater_Name = '玄幻') L# x! Y& n+ ^! Y
- if self.CaterId == 'wuxia':
6 U* Z: [0 k; S) i6 ?! e; j - Cater_Name = '武侠'; a8 z, L, ^4 }5 \3 \8 u# g
- if self.CaterId == 'lishi':
' x9 H) }2 }% z! ?) \/ s - Cater_Name = '历史' & [+ c0 H: d' L: Z u1 y
- if self.CaterId == 'yanqing':
% C% f$ X9 }' C - Cater_Name = '都市'
. a, @' k8 ~: G" x - if self.CaterId == 'nvsheng':
- F8 K1 Z6 d% R8 R v - Cater_Name = '都市' # [0 b4 h/ E* o$ h6 |) p/ J, l
- if self.CaterId == 'kehuan':" L5 O, d. s) g( }5 f& p* ~
- Cater_Name = '科幻' 8 b$ N" v) `- x s/ p o8 Y
- if self.CaterId == 'kongbu':
8 C( r8 S1 Z% D3 y, l - Cater_Name = '游戏' ) Y( M6 r z! W6 T9 c- ?; l, i4 g
- print self.CaterId
1 l: r; m; d8 x" I& e - url = self.base_url1 + str(self.CaterId) + self.base_url2 + str(self.page_num) + "/" ' \2 O2 M# C2 `( w
- self.crawl(url, callback=self.list_Caterg,save=Cater_Name)) S" M' C8 r0 Q" h% `3 L) Y6 r
- self.page_num += 1 + J2 x% o( u- Q: ?) Y
- 1 @ Z* v( D8 g7 k
- def list_Caterg(self, response):
3 Y% @9 V$ H* r3 a9 | - Cater_Name = response.save/ r1 e1 ?1 V( {
- for each in response.doc('.pic-list a[href^="http"]').items():7 L/ M; x4 U$ E( p
- self.crawl(each.attr.href, callback=self.list_Caterg_detail,save=Cater_Name)5 g+ x: b, D0 ^7 x! L7 t: V& `; q
- : P8 X1 o/ n3 _. t8 D, X
- def list_Caterg_detail(self, response):. a8 E- {& V( J( ]) ]/ U( p
- Cater_Name = response.save
: P& a7 R( q" U5 P; A' o8 ^ - # print Cater_Name' m% i- C5 m/ i% D. C1 ^$ O* `
- Bookname = response.doc('h1').text()
% S5 ^( J0 F& X$ J, i" k3 | - print Bookname4 T/ U) a& X+ x4 ]* V: }
- Book_author = response.doc('.authorname > a').text() S! ?9 r" v) o
- # print Book_author3 a9 k/ _! D5 A' N1 `
- Book_Introduction = response.doc('.book-intro > div').text(), r$ U- y* p1 L7 u! ]0 U
- # print Book_Introduction
; ^' m% p$ A8 y V - Book_Synopsis = response.doc('b').eq(1).text()
* x3 r1 S5 d% F% @7 F, A - # print Book_Synopsis2 v( J2 t( }+ S. R: |3 w( \
- Book_Palabras = response.doc('.booktitle p').text().split(' ')[1].split('|')[0]5 ]& s/ \; Q* c* A
- # print Book_Palabras
% w( H! ^6 h+ N( l4 k, ]- [* b- z, P - BookIDs = response.url.split("xiaoshuo/")[-1].split("/")[0] #小说ID
2 m6 ~. W- m3 G6 v* L* Z; ?3 l - # print BookIDs
+ w. g8 _% N& o7 G# D - Book_Dates = str(datetime.datetime.now())
* z7 y- z1 L- Z' A3 w9 |4 E - for imgs in response.doc('.bigpic > img[src^="http"]').items():' u' `- z( Y, W& d# [ v6 a
- img = imgs.attr.src
9 b6 O4 q: q$ U - print img
3 G L; F' J' a' }3 l- ^! T+ | - #小说封面下载
0 L) Z+ @3 J5 O4 p O7 J. ?; q* W - extension = self.getExtension(img)
& v! o) A+ X5 K# r T# E( C - name = self.getname(img)
! F0 x9 T* ~1 m8 }) n' J1 F - file_name = name + "." + extension# F' D. _6 p- L, B7 y4 g1 y+ ^. K" e
- imgDir = P_dir + name: N8 V+ ?# W! O3 a) t, K( H, _9 P
- Locaimg = imgDir + "/" + file_name% @" j9 C y# c& a- o$ f l' d+ [
- print Locaimg- } Y' q2 P1 S! H$ X! G
- if(self.download(P_dir, imgDir, file_name, img)): #这2行可注译,图片下载到本地8 K$ A5 l9 B; w2 \
- print('attachment url is ' + img) #; _, F( c) w8 T- |. p& |/ U1 ~" L
- Datos = {. q; p( v; m7 h1 R8 e6 D
- "Cater_Name":Cater_Name,
$ d' w; D$ ?( u - "Book_author":Book_author,2 T/ g" I9 h. X8 G
- "Book_Introduction":Book_Introduction,
( ?9 E: e( f3 O; D+ S - "Book_Synopsis":Book_Synopsis,
/ m) A: }& w- e/ \ - "Book_Palabras":Book_Palabras,( u9 } C. `7 n% q6 f
- "img":img,4 x$ b! u0 N9 d& h( Z
- }
: t; t" t2 ]: e - self.add_BookFile(Bookname, BookIDs, img, Locaimg, Book_Dates) #这行可注译,数据库发布接口,方便其他系统的发布
; h! G8 d: F/ d3 s1 u R6 |( A - for each in response.doc('div[class="bookbtn-txt"] a[class="catalogbtn"]').items():
2 u8 U5 R1 c) K# B; Z: q N - self.crawl(each.attr.href, callback=self.index_page,save=Datos)' p0 p( V3 E0 |5 o# T6 U
- + k. v9 A e& M5 z
- @config(age=8 * 60 * 60) F) I! t( r8 c) W
- def index_page(self, response): 9 L# [& H, J# |% j0 O
- Datos = {
) ]1 i0 D( g2 _$ [ - "Cater_Name":response.save['Cater_Name'],
6 `+ D3 x Z, \# r - "Book_author":response.save['Book_author'],
! S3 k3 M3 N/ R - "Book_Introduction":response.save['Book_Introduction'],# |: @ o1 J) G" b( r6 ?
- "Book_Synopsis":response.save['Book_Synopsis'],
+ m" m0 k+ K& e6 b' V3 G - "Book_Palabras":response.save['Book_Palabras'],
! d u3 j1 G7 V/ j; ^1 d8 l - "img":response.save['img'], q. J4 \5 L* ~: a% I8 \5 h
- }
0 P; X& t8 w( V8 t' ^ - for each in response.doc('.chapter-list li:first-child a[href^="http"]').items():5 S6 |2 M0 c* X4 Y8 A a
- # for each in response.doc('.chapter-list a[href^="http"]').items(): 0 e' g6 j- R4 l# l9 g: b
- self.crawl(each.attr.href, callback=self.detail_page,save=Datos)5 Q. ]1 j/ F( [; {9 g
- @config(priority=2)9 a6 E6 x2 {+ q5 o" m
- @catch_status_code_error- t* P" k) g, o8 O, z* J7 O8 {) z. ]
- def detail_page(self, response):
/ y% f- h% q# @' c* X( M - NewRe1 = u'哈书'5 y! d0 I6 L' E. t5 f1 {$ W( M
- NewRe2 = u'huhjsd.CC'
+ y6 U( Q" x M - NewRe3 = r'^\\n\\n'
) \5 _, G7 |9 J$ m3 {; i, a8 o - NewRe5 = u'小说网'3 @: Y1 g) J" H% J
- NewRe6 = u'fgdfgf'$ W' u3 t5 w4 g* h# S2 h" }
- NewRe7 = u'fgfgf'
' r$ T" D7 b9 j m; O - NewRe8 = u'ffhgf'
, k& _ o* u( X9 W4 i& r* F - NewRe4 = r'[\f\t\v+\.\{\(\)\}\!\/_,$%^*(+"\')]+|[+——()?【】“”!,。?、~@#¥%……&*()]+'
! j1 W9 i4 O$ y- X5 i - ReC1 = u'静思'- r! g* B @% |1 _- C* q4 e
- ReC2 = u'aghgf.com': e, t1 j( x5 O5 x8 F" Z# x8 Q- D" K1 Y
- ReC3 = u'aghgfh.com'" E# F- v7 o$ Q+ }
- ReC4 = u''8 a! W3 c; g; ]- t3 l, ?( E1 ?& x
- ReC5 = u'文学网'
/ j! L0 `4 A3 y- j. ^ - ReC6 = r'<BR>'
5 D V+ ] ]. x$ @. a* @: j! q - Bookname = response.doc('.readlocation a').eq(2).text() #小说名称- z% X5 z4 M. }+ d! t& v3 o1 M
- print Bookname$ n4 V. a- s: I' e
- Cater_Name = response.save['Cater_Name'] # 小说分类
* z% \0 t0 ^: i2 ~5 ^ - Book_author = response.save['Book_author'] #小说作者* M, {0 F; g) Q* b
- Book_Introduction1 = response.save['Book_Introduction'] #小说简介3 u8 V o W$ Q, }) n. K
- Book_Synopsis = response.save['Book_Synopsis'] #最近更新' B3 G4 u r+ @; h3 o* J" ^
- Book_Palabras = response.save['Book_Palabras'] #小说字数/ Z. f0 I& `+ @$ t; b# j
- Bookurl = response.url #小说网址
. d& H C* m4 K& |1 |) Q. [! S7 l - Booktitle = response.doc('.article-title').text() #章节名称
) a6 b) V% n B4 j, T3 Q } - BookID = response.doc('.readset-r span').text() #小说ID+ Z, `8 M. J, v5 o% Y# G3 W
- BookConte1 = response.doc('.article-con').text() #小说章节内容
4 ]0 w4 O6 W: j0 A2 { - abover = response.doc('.article-title').text() + response.save['Book_Synopsis'] + response.save['Book_Palabras'] + response.save['Book_Introduction'] #小说状态(连载还是完成)' Y L- m3 U0 s, C2 k
- Book_Date = str(datetime.datetime.now()) # 采集时间
6 u: Z$ d, q& E0 G- @( K& Q, B - BookConte2 = BookConte1.replace(NewRe1 , ReC1)
' w" _+ [8 X0 D7 \0 n - BookConte3 = BookConte2.replace(NewRe2 , ReC2)
5 E* G; N% P4 S0 c$ d8 C - BookConte5 = BookConte3.replace(NewRe5 , ReC5)
. O- n2 E5 @- B4 I; v' J% w6 w1 ?9 z/ f - BookConte6 = BookConte5.replace(NewRe6 , ReC2)6 }* y# I! R& B. C
- BookConte7 = BookConte6.replace(NewRe7 , ReC2)/ X0 }# ^. w4 R4 v
- BookConte8 = BookConte7.replace(NewRe3 , ReC6)8 i) R9 G2 }) r: b, Q3 {. Q& \9 |
- BookConte4 = re.sub(NewRe4 , ReC4 , BookConte8)) ?8 t1 f0 T& Q8 Q' X( D' V8 t0 W( C
- BookConte = BookConte4.replace("\n\n","<br>")
1 l' x; x& A* J! @9 ?2 Z) m - print BookConte
( J, R* ~4 m$ S6 e* @9 H3 e7 } - Book_Introduction2 = Book_Introduction1.replace(NewRe1 , ReC1)' N* N" U/ Q, e* C9 V2 R J
- Book_Introduction3 = Book_Introduction2.replace(NewRe2 , ReC2)# u; [1 ]; z' I' d, H
- Book_Introduction4 = Book_Introduction3.replace(NewRe3 , ReC3)3 |$ I' g; k: \ z; B
- Book_Introduction = re.sub(NewRe4 , ReC4 , Book_Introduction4)$ R: \5 m* x: A% p7 n
- Titleid = response.url.split(BookID + "/")[-1].split("/")[0]
+ u& y: b$ ]' ?' x; w - Book_img = response.save['img'], #小说图片- ~2 c8 V0 m% E! r }. \4 u
- ( a6 }: G' ~# t( V
- #insert into MySQL 小说入库
, F" n6 b$ i2 } j: _# B2 { - self.add_question(Bookname,Cater_Name,Book_author,Book_Introduction,Book_Synopsis,Book_Palabras,Bookurl,Booktitle,BookID,BookConte,Titleid,abover,Book_Date) #这行可注译,数据库发布接口,方便其他系统的发布
5 Y$ U: W+ v* s - self.add_comment(Bookname, Booktitle, BookID, Titleid, Book_Date) #这行可注译,数据库发布接口,方便其他系统的发布
: _7 `1 }5 P! ]% n2 W: X# m& | - #post提交发布' Q: @9 D0 w2 M) t
- self.add_locoy(Bookname,Cater_Name,Book_author,Book_Introduction,Book_Palabras,Book_img,Booktitle,BookConte,abover) #这行可注译,火车头发布接口,不需要可取消
; Q K( w! f' F+ N! { - Datos = {
& D% `1 p, {5 {% O9 V - "Cater_Name":response.save['Cater_Name'],( X+ ^7 t& c3 d e+ N4 q6 x! A
- "Book_author":response.save['Book_author'],# O$ _! X0 D# K& F4 x% z
- "Book_Introduction":response.save['Book_Introduction'],
, ^! Y2 T. d4 D K" n6 J - "Book_Synopsis":response.save['Book_Synopsis'],- o* a# J, q8 [9 `6 a
- "Book_Palabras":response.save['Book_Palabras'],
1 h5 i/ M+ a7 A# s, s - "img":response.save['img'],$ S) h% w9 j: H7 }6 `& p
- }
6 W# u5 [" b. S7 f - for each in response.doc('.articlebtn > a:nth-child(4)[href*="/xiaoshuo"]').items():
: c9 D$ o2 v# e6 _+ O5 d - self.crawl(each.attr.href, callback=self.detail_page,save=Datos) 9 f2 }9 g3 @* {: n
- return {* h! R* n$ S; \0 Z, o& M Y/ p" ^
- "Cater_Name":Cater_Name,1 K3 h6 E: k( g2 L/ _; f
- "Bookname":Bookname,' L* j5 z; n' z: q( I4 U
- "Book_author":Book_author,
0 q4 z$ p# n9 @+ _2 ?2 N, j - "Book_Introduction":Book_Introduction,- p. j: R. U7 R/ W% f- Z$ t
- "Book_Synopsis":Book_Synopsis,
: U$ }5 m7 {! W6 Q4 o5 l9 I/ J! e( } - "Book_Palabras":Book_Palabras,7 m$ }6 C( k2 y
- "Book_img":Book_img,' @6 w u% V4 _' x
- "Bookurl": response.url,
# J5 [3 P q9 Y% H8 H - "Booktitle": Booktitle,1 v4 H8 n0 z/ }# B- s
- "BookID": BookID,
8 }; b6 @4 O1 A - "BookConte": BookConte,5 k: S2 w, V! y8 `
- "Titleid": Titleid,
3 V8 T0 \. A1 }7 f - "abover":abover,
& Y" d7 {) v- d+ c0 O - # "Book_Date" = str(datetime.datetime.now()),
/ z: U h2 O( |' m& `6 D J - }8 ]" _& E6 y* L8 F( i& ]
- def download(self, P_dir, imgDir, file_name, Book_img):3 w W" x/ |8 t- N& e" Y
- if not os.path.exists(imgDir):
, }# R0 v+ e( G/ [ - os.makedirs(imgDir)
/ f( N4 t2 q0 o, `0 X e - file = imgDir + "/" + file_name
) y7 Q* P, e& b2 T9 w - # print file0 u: d5 Q5 I; D. \
- f = open(file, 'wb+')0 E& o$ y7 ]6 w+ B" z, f( r- p
- imag = requests.get(Book_img)
7 J9 C: U# E8 ~2 P - f.write(imag.content)5 c5 o) c# ~& k, V1 {2 i0 T& ?$ l( w
- f.close()9 |0 t6 H" I( l
- #保存图片前4 o* @+ i. ^0 A
- def save_imgs(self,response):
, X6 O4 E V. k7 e; h& p. k$ a - content = response.content( H" P. P# J8 S" [1 v1 @
- file_name = response.save["file_name"]+ I0 A. w% A$ }( E- L( O7 V
- imgDir = response.save["imgDir"], H* T4 {+ R" A) y9 U0 e2 ^
- file_path = imgDir + file_name8 L0 [, P8 ]# X" I- K/ ^
- self.save_img(content,imgDir,file_path)' q1 s9 _- z6 L r: l: \/ l) k6 Z
- #保存图片' |# }* E$ M- `5 ?; L/ j! \$ K0 u: ^
- def save_img(self,content,imgDir,path):
; D1 u |% ~' P9 {5 c - if not os.path.exists(imgDir): $ b# I' U9 [6 y5 v! ^8 M
- os.makedirs(imgDir)9 w( J' K) t, ^6 w2 r' T8 R3 z* s
- f = open(path,"wb" )
% s( j, S# y1 Z" D( x. O - f.write(content)
( @; c; F$ V/ [* a - f.close()
" ?8 K8 E; E. } B! m# Z - #获取url后缀名
* z0 Y3 O4 w/ k2 H7 e% V. B% U$ q - def getExtension(self,url):
. H$ F. q+ w& V( w! w) A - extension = url.split(".")[-1]( T# H5 y! Z6 n7 V- b( t
- return extension * D: I/ N7 O, e0 L6 c- s* U9 ^
- # Z# k& f0 I1 C1 A0 g- G2 Z
- #获取图片名
# b, \$ I7 Q1 I$ N$ @/ U3 M - def getname(self,url):
. a/ ~2 X# m: `3 ` T - name=url.split("/")[-1].split(".")[0]
+ z9 H& D! M0 j, [ - return name
复制代码
. H1 S5 Z; O! X$ `- e" \. f % s2 [* Y6 G% P* {$ N' J( u+ P
|