首页 > 其他 > 详细

笔趣阁小说爬虫

时间:2021-09-21 13:23:10      阅读:19      评论:0      收藏:0      [点我收藏+]

笔趣阁小说爬虫

2021/09/18

简单的写了一个小爬虫,爬取笔趣阁上面的小说。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 16 14:04:15 2021
笔趣阁小说抓取
@author: fanzhen
"""
import requests
from bs4 import BeautifulSoup
import time
?
def get_html(url):
   headers={
       ‘user-agent‘:‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36‘
      }
   req=requests.get(url,headers=headers)
   if req.status_code == 200:
       req.encoding=req.apparent_encoding #使用网页现在的编码形式,以防乱码
       return req.text
       #这是判断网页返回的状态码,200代表连接成功,大家常见的应该是404和503
   else:
       return
   
   
def get_texts(html):
   soup=BeautifulSoup(html,‘html.parser‘) #使用beautisoup对网页源码进行解析
   title=soup.select("#wrapper > div.content_read > div.box_con > div.bookname > h1")#取得章节名称
   w=‘‘
   w+=title[0].get_text().replace(‘\n‘,‘‘).replace(‘\r‘,‘‘)+‘\n‘
   t=soup.select(‘#content‘)
   w+=t[0].get_text()
   w+=‘\n‘
   print(w)
   return w
   
?
def get_pages(html):
   soup=BeautifulSoup(html,‘html.parser‘) #使用beautisoup对网页源码进行解析
   np=soup.select(‘#wrapper > div.content_read > div.box_con > div.bookname > div > a.next‘)
   return np[-1].get(‘href‘)
?
?
?
?
def get_classes(html):#抓取小说类别
   soup=BeautifulSoup(html,‘html.parser‘)
   classes=soup.select(‘#wrapper > div.nav > ul > li > a‘)
   del classes[:2]#删除无关类别
   del classes[-2]
   c=‘‘
   cc=1
   class_link=[]
   for in classes:#映出小说类别
       c+=str(cc)+‘.‘+i.get_text()+‘ ‘
       cc+=1
       class_link.append(i.get(‘href‘))
   print(c)
   return class_link
?
?
?
?
def select_class(num,class_link):#进入小说类别,查看该类别下的小说
   sub_url=‘https://www.biqugeu.net‘+class_link[num]
   classes_html=get_html(sub_url)
   soup=BeautifulSoup(classes_html,‘html.parser‘)
   title1=soup.select(‘#newscontent > div.l > h2‘)
   print(title1[0].get_text())
   newscontent=soup.select(‘#newscontent > div.l > ul > li > span.s2 > a‘)
   link=[]
   a=‘‘
   aa=0
   for in newscontent:
       if aa%5==0:
           a+=‘\n‘
       a+=str(aa+1)+‘.‘+i.get_text()+‘ ‘
       aa+=1
       link.append(i.get(‘href‘))
   print(a)
   return link
?
def enter_directly(url):#直接进入小说阅读页面,跳过小说首页
   html=get_html(url)
   soup=BeautifulSoup(html,‘html.parser‘)
   enter_link=soup.select(‘#list > dl > dd:nth-child(15)>a‘)
   return enter_link[0].get(‘href‘)
?
?
def main():
   
  
   url=‘https://www.biqugeu.net‘
   html=get_html(url)
   class_link=get_classes(html)
   num=input("选择小说类型:")
   link=select_class(int(num)-1, class_link)
   book_num=input("选择小说:")
   book_num=int(book_num)
   book_html=get_html(‘https://www.biqugeu.net‘+enter_directly(‘https://www.biqugeu.net‘+link[book_num]))
   choice=0
   while choice!=‘-1‘ and get_pages(book_html)!=link[book_num]:#当小说结束或者使用者想推出是退出
       w=get_texts(book_html)
       choice=input("1)下一页;-1)结束:")
       url=‘https://www.biqugeu.net‘+get_pages(book_html)
       book_html=get_html(url)
   
   
   time_start=time.time()
   time_end=time.time()#监视程式运行总时间
   print(‘抓取完毕,用时:‘,time_end-time_start,‘s‘)
   
?
if __name__==‘__main__‘:
   main()
?

可以在线查看小说,并支持在线阅读,练手做。

笔趣阁小说爬虫

原文:https://www.cnblogs.com/fzwd6666/p/15308168.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!