Python爬取笔趣阁小说(新手可入~)

本文介绍了一个使用Python的requests和BeautifulSoup库抓取笔趣阁小说章节标题和内容的简单爬虫程序,通过设置代理服务器并遍历网页链接,实现批量抓取并存储到文本文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个简单的爬取,完整代码放在最后~~~

笔趣阁网址:https://www.biqg.cc/

首先导入需要的库

import requests
import re
from bs4 import BeautifulSoup
import time

点击第一篇文章,按F12进入开发者模式

选择NetWork找到代理服务器

将网址和代理填进去

# 设置代理服务器
    headers = {
        'User_Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
    }
# 请求网址
url = 'https://www.biqg.cc/book/6909/1.html'
response = requests.get(url, headers=headers)

为了防止文字乱码,还要对其进行转化格式

# 转化为utf-8格式,不加这条语句,输出爬取的信息为乱码
response.encoding = 'utf-8'

使用Beautiful Soup解析网页代码

#获取到源码
html = response.text
#使用Beautiful Soup解析网页内容
soup = BeautifulSoup(html, 'html.parser')

找到标题

使用正则表达式提取标题:

# 正则表达式解析小说章节标题
pattern1 = re.compile(r'<h1 class="wap_none">(.*?)</h1>')
title = re.findall(pattern1, html)

找到小说内容

这里我们用Beautiful Soup来提取

#解析小说章节正文内容
text = soup.find('div', id='chaptercontent').text

打印输出

print(title)
print(text)

第一章提取完成!!!

接下来可以多提取几章,注意观察网址的变化:

因此对网址加个for循环就可以提取多章小说了

for page in range(11):  # 爬取10章小说
    # 请求网址
    url = 'https://www.biqg.cc/book/6909/'+str(page)+'.html'
    time.sleep(1)  # 防止操作过快,网站防爬

最后将内容写入文本文件

with open('novel.txt', 'a', encoding='utf-8') as file:
    file.write('\n'.join(title))
    file.write('\n')
    file.write(text)
    file.write('\n\n')

完整代码如下:

import requests
import re
from bs4 import BeautifulSoup
import time

for page in range(11):  # 爬取10章小说
    # 请求网址
    url = 'https://www.biqg.cc/book/6909/'+str(page)+'.html'
    time.sleep(1)  # 防止操作过快,网站防爬
# 设置代理服务器
    headers = {
        'User_Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'
    }
    response = requests.get(url, headers=headers)
    if response.status_code == 200:   #状态码200表示请求成功
        # 转化为utf-8格式,不加这条语句,输出爬取的信息为乱码
        response.encoding = 'utf-8'
        #获取到源码
        html = response.text
        #使用Beautiful Soup解析网页内容
        soup = BeautifulSoup(html, 'html.parser')
        # 正则表达式解析小说章节标题
        pattern1 = re.compile(r'<h1 class="wap_none">(.*?)</h1>')
        title = re.findall(pattern1, html)
        #解析小说章节正文内容
        text = soup.find('div', id='chaptercontent').text
        # 打印输出
        print(title)
        print(text)
# 写入txt文件
        with open('novel.txt', 'a', encoding='utf-8') as file:
            file.write('\n'.join(title))
            file.write('\n')
            file.write(text)
            file.write('\n\n')

Python爬虫用于从网站上抓取数据,包括文本、图片等信息。如果你想爬取这类小说网站的数据,首先需要了解其网页结构,通常这种类型的网站会有分页、章节列表等布局。以下是使用Python爬虫如`requests`库获取HTML内容的大致步骤: 1. **导所需库**: ```python import requests from bs4 import BeautifulSoup ``` 2. **发送GET请求获取HTML**: ```python url = 'https://www.biquge.com.cn/' # 首页或其他章节页面URL response = requests.get(url) if response.status_code == 200: html_content = response.text else: print("请求失败") ``` 3. **解析HTML内容**: ```python soup = BeautifulSoup(html_content, 'html.parser') # 使用BeautifulSoup解析HTML,提取你需要的信息,如章节链接或标题 chapters = soup.find_all('a', class_='chapter') # 这里的类名假设是获取章节链接的部分 ``` 4. **遍历获取数据**: ```python for chapter in chapters: link = chapter['href'] # 获取每个章节的链接 title = chapter.text.strip() # 获取章节标题 # 对于每个链接,你可以进一步发送请求并处理新的HTML内容 ``` 5. **保存或处理数据**: 将爬取到的数据存储到文件、数据库或进行后续分析。 **注意事项**: - 爬虫应遵守目标网站的robots.txt规则,并尊重版权。 - 频繁的高频率请求可能会导致IP被封禁,所以可以设置合适的延迟或使用代理IP。 - 有些网站有反爬机制,可能需要使用更复杂的解决方案,比如设置User-Agent、模拟登录等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值