日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python爬虫,批量下载小说网站上的小说!

發布時間:2023/12/16 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python爬虫,批量下载小说网站上的小说! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

爬蟲腳本把這個小說網上的幾乎所有小說都下載到了本地,一共27000+本小說,一共40G。

?

完整代碼:

  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
  • ?
#coding=utf-8 2 import urllib 3 import urllib2 4 import re 5 import os 6 7 webroot = 'http://www.xuanshu.com' 8 9 for page in range(20,220):10 print '正在下載第'+str(page)+'頁小說'11 12 url = 'http://www.xuanshu.com/soft/sort02/index_'+str(page)+'.html'13 headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' }14 try:15 request = urllib2.Request(url,headers=headers)16 response = urllib2.urlopen(request,timeout=180)17 #print response.read()18 except urllib2.URLError, e:19 if hasattr(e,"code"):20 print e.code21 if hasattr(e,"reason"):22 print e.reason23 24 html = response.read().decode('utf-8')25 #print html26 pattern = re.compile(u'<li>.*?<div class="s">.*?target="_blank">(.*?)</a><br />大小:(.*?)<br>.*?</em><br>更新:(.*?)</div>.*?<a href="(.*?)"><img.*?>(.*?)</a>.*?<div class="u">(.*?)</div>',re.S)27 items = re.findall(pattern,html)28 #print items29 30 for item in items:31 try:32 book_auther = item[0].encode('gbk')33 book_size = item[1].encode('gbk')34 book_updatetime = item[2].encode('gbk')35 book_link = item[3].encode('gbk')36 book_name = item[4].encode('gbk')37 book_note = item[5].encode('gbk')38 39 book_full_link = webroot + book_link # 構建書的絕對地址40 41 #請求地址42 try:43 request = urllib2.Request(book_full_link,headers=headers)44 response = urllib2.urlopen(request,timeout=180)45 except urllib2.URLError, e:46 if hasattr(e,"code"):47 print e.code48 if hasattr(e,"reason"):49 print e.reason50 html = response.read().decode('utf-8')51 #print html52 pattern = re.compile('<a class="downButton.*?<a class="downButton" href=\'(.*?)\'.*?Txt.*?</a>',re.S)53 down_link = re.findall(pattern,html)54 print book_name55 print down_link56 57 # down txt58 try:59 request = urllib2.Request(down_link[0].encode('utf-8'),headers=headers)60 response = urllib2.urlopen(request,timeout=180)61 except urllib2.URLError, e:62 if hasattr(e,"code"):63 print e.code64 if hasattr(e,"reason"):65 print e.reason66 try:67 fp = open(book_name+'.txt','w')68 except IOError,e:69 pattern = re.compile('<strong>.*?>(.*?)<.*?</strong>',re.S)70 book_name = re.findall(pattern,book_name)71 fp = open(book_name[0]+'.txt','w')72 print 'start download'73 fp.write(response.read())74 print 'down finish\n'75 fp.close()76 except Exception,e:77 print '該條目解析出現錯誤,忽略'78 print e79 print ''80 fp = open('error.log','a')81 fp.write('page:'+str(page)+'\n')82 fp.write(item[4].encode('gbk'))83 #fp.write(e)84 fp.write('\nThere is an error in parsing process.\n\n')85 fp.close()

該腳本只定向抓取“選書網”小說站,“玄幻奇幻”分類下的小說。供網友們參考,可自行修改。

寫得比較粗糙,勿噴……

總結

以上是生活随笔為你收集整理的Python爬虫,批量下载小说网站上的小说!的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。