在本篇博客中,我们将使用requests+正则表达式爬取笔趣阁的小说,获取小说的名字、文本等内容。
http://www.xbiquge.la/xiaoshuodaquan/
首先打开上面的网址,我们会发现是小说列表,选择其中一部小说,打开会是章节列表,打开某一章后才是文本。所以,我们要首先获取小说列表,然后打开某一部小说后,再获取章节列表,最后在爬取对应的内容。依旧是四部曲:
首先搭建起程序主体框架:
import os
import re
import time
import requests
from requests import RequestException
def get_page(url):
pass
def get_list(page):
pass
def get_chapter(novel_url):
pass
def get_content(chapter,name):
pass
def write_tofile(chapter_content,chapter_name,name):
pass
if __name__=='__main__':
#首页url
url = 'http://www.xbiquge.la/xiaoshuodaquan/'
#发送请求,获取响应
page = get_page(url)
#获取小说列表 解析响应
novel_list = get_list(page)
print(novel_list)
#查找某部小说 进行爬取
name = '全职法师'
for item in novel_list:
if item[1] == name:
# 如果在列表中有这部小说,就返回该小说的章节列表
novel_chapter = get_chapter(item[0])
print(novel_chapter)
#按小说章节 分别保存到文本文件
for chapter in novel_chapter:
get_content(chapter,name)
发送请求&