博客
关于我
Python 爬取起点的小说(非vip)
阅读量:357 次
发布时间:2019-03-04

本文共 1978 字,大约阅读时间需要 6 分钟。

Python 爬取小说章节(非VIP)- 以《冒牌大英雄》为例

本文将介绍如何使用Python编写一个爬取小说章节的脚本。以《冒牌大英雄》为例,讲述从非VIP章节爬取小说内容的实现方法。通过本地存储每个章节的内容,生成HTML格式的文件。

技术选型

本次实现使用了以下技术和工具:

  • 第三方库requests,用于发送HTTP请求,抓取网页内容
  • 正则表达式:用于解析网页源代码,提取章节链接
  • 文件存储:将爬取到的内容存储为本地文件
  • 爬取流程

    完整的爬取流程如下:

  • 获取起始网址的网页源代码
  • 从源代码中提取章节链接
  • 循环处理每个章节链接
    • 提取每个章节的标题
    • 提取每个章节的内容
    • 保存为本地文件
  • 文件命名规则
    • 文件名由标题加时间戳组成
    • 保存格式为HTML文件
  • 实现细节

    1. 获取起始网址的网页源代码

    使用requests库发送HTTP GET请求,获取起始网址的网页源代码。同时,设置合理的请求头信息,避免被网站反爬机制拦截。

    import requestsimport reurl = 'https://book.qidian.com/info/131957'headers = {    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',    'accept-encoding': 'gzip, deflate, sdch, br',    'accept-language': 'zh-CN,zh;q=0.8'}response = requests.get(url, headers=headers)response.encoding = 'utf-8'response_text = response.text

    2. 提取章节链接

    网页源代码中通常嵌入了章节链接,可以通过正则表达式提取。常见的提取方式包括:

    • 查找特定的a标签
    • 查找特定的li列表
    • 使用动态加载的内容(如data-rid属性)
    pat = r'
  • 'links = re.findall(pat, response_text)

    3. 循环处理每个章节链接

    对于每个找到的章节链接,重复以下步骤:

  • 发送HTTP GET请求,获取章节网页的内容
  • 提取标题和内容
  • 保存为本地文件
  • for link in links:    # 发送请求获取章节内容    chapter_url = 'https://book.qidian.com' + link    chapter_response = requests.get(chapter_url, headers=headers)    chapter_response.encoding = 'utf-8'    chapter_text = chapter_response.text        # 提取标题和内容    title_pat = r'

    (.*?)

    ' content_pat = r'(?<=
    ).*?(?=<\/content>)' title = re.search(title_pat, chapter_text).group(1) content = re.search(content_pat, chapter_text).group(0) # 保存文件 filename = f'"{title}"_{int(time.time())}.html' with open(filename, 'w', encoding='utf-8') as f: f.write(f'

    {title}

    {content}')

    4. 文件命名规则

    为确保每个文件唯一,可以在标题中添加时间戳。这样即使同一标题的章节多次爬取,也能生成不同的文件名。

    import timefilename = f'"{title}"_{int(time.time())}.html'

    注意事项

  • 反爬机制:部分网站会通过检测代理IP或请求频率来限制爬取行为。可通过设置代理IP池或模拟真实用户行为来规避。
  • 网络限制:部分网站会限制爬虫的并发请求量。可以通过requests库的timeout参数设置请求超时。
  • 内容变更:网站内容可能会定期更新,导致爬取的内容与实际页面有差异。建议定期检查和更新爬虫规则。
  • 通过以上方法,可以轻松实现爬取小说章节的需求。整个流程从获取源代码到存储本地文件,都可以通过Python脚本实现,满足批量处理和自动化的需求。

    转载地址:http://kokr.baihongyu.com/

    你可能感兴趣的文章
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错TypeError: this.getOptions is not a function
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>
    npm的常用配置项---npm工作笔记004
    查看>>
    npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
    查看>>
    npm编译报错You may need an additional loader to handle the result of these loaders
    查看>>
    npm设置淘宝镜像、升级等
    查看>>
    npm设置源地址,npm官方地址
    查看>>
    npm设置镜像如淘宝:http://npm.taobao.org/
    查看>>