Python爬虫实战 | (2) 爬取网络小说

 在本篇博客中,我们将使用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)

发送请求&

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值