• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

学100种基本爬虫项目--使用正则表达式来获取整篇小说

武飞扬头像
Volcanoforever
帮助1

我们要爬取的小说为以下小说,结尾附上整体代码以及注意事项

学新通

 在这篇文章中,我将展示如何使用Python和正则表达式来爬取整篇小说的内容。我们的目标是从"bbiquge"网站上获取某本小说的全文内容,并保存为.txt文件。我们将使用requests库进行网络请求,使用re和parsel库来解析页面,并把小说内容保存到本地。

1.导入需要的模块:此段代码首先导入了几个Python的标准库如os.pathre,以及第三方库requestsparsel。这些库分别用于处理文件路径、处理正则表达式、发送HTTP请求和解析HTML文档。

  1.  
    import os.path
  2.  
    import re
  3.  
    import requests
  4.  
    import parsel

2.设置初始参数:设置了小说的URL(list_url),以及发送HTTP请求时使用的请求头(headers)。请求头中的User-Agent是用来伪装成一个正常的浏览器,以防止服务器因为检测到是爬虫而拒绝服务。

  1.  
    list_url='https://www.bbiquge.net/book/133303/'
  2.  
    headers = {
  3.  
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
  4.  
    }

3.发送请求并获取响应:通过requests.get方法发送GET请求到list_url,并使用headers作为请求头。然后获取响应并设置响应的编码为'gbk'。

  1.  
    response=requests.get(url=list_url,headers=headers)
  2.  
    response.encoding='gbk'

4.提取信息:使用re.findall方法和正则表达式从响应的文本中提取所有的章节链接(href)以及小说的名字(name)。

  1.  
    href=re.findall('<dd><a href="(.*?)">(.*?)</a></dd>',response.text)
  2.  
    name=re.findall('<div id="picbox"><div class="img_in"><img src="https://www.biqugevip.net/files/article/image/133/133301/133301s.jpg" alt="(.*?)"',response.text)[0]
  3.  
     

5.遍历所有章节并下载:对每一个提取到的章节链接,发送GET请求获取章节页面的HTML,然后解析HTML以提取章节标题和内容。最后,将提取到的标题和内容写入到本地的txt文件。

  1.  
    for index in href:
  2.  
    index_url='https://www.biqugevip.net/book/133303/' index[0]
  3.  
    selector=parsel.Selector(requests.get(url=index_url,headers=headers).text)
  4.  
    title=selector.css('#main > h1::text').get()
  5.  
    contest_list=selector.css('#content::text').getall()
  6.  
    contest="\n".join(contest_list)
  7.  
    with open(fr'D:\爬取文本存放位置\一百种爬虫方式\{name}.txt',mode='a', encoding='utf-8') as f:
  8.  
    f.write(title)
  9.  
    f.write('\n')
  10.  
    f.write(contest)
  11.  
    f.write('\n')
  12.  
    print('正在保存:',title)

在这段代码中,首先是拼接出每个章节的URL(index_url)。然后发送GET请求获取章节页面的HTML,并使用parsel.Selector对HTML进行解析。然后提取出章节的标题(title)以及内容(contest_list),并将内容的列表通过\n合并成一个字符串(contest)。最后,打开(如果不存在则创建)一个txt文件,并以追加的模式将标题和内容写入文件。在写入每个章节的内容后,会打印一条消息表示该章节已经保存。

运行结果:

学新通

效果展示: 

学新通

 整体代码

  1.  
    # _*_coding :utf-8 _*_
  2.  
    # @time 2023/7/14 20:24
  3.  
    # @Author :volcano
  4.  
    # @File 爬取小说
  5.  
    # @Proect :workspace
  6.  
    #模拟浏览器对服务器发送请求
  7.  
    #导入请求模块
  8.  
    import os.path
  9.  
    import re
  10.  
    import requests
  11.  
    #导入解析模块
  12.  
    import parsel
  13.  
    list_url='https://www.bbiquge.net/book/133303/'#用自定义的变量接收字符串的数据内容
  14.  
    headers = {
  15.  
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
  16.  
    }
  17.  
    response=requests.get(url=list_url,headers=headers)
  18.  
    response.encoding='gbk'
  19.  
    # print(response.text.replace('<br>', '\n')) # 使用replace"<br>"替换为换行符"\n"
  20.  
    href=re.findall('<dd><a href="https://blog.csdn.net/Volcano99/article/details/(.*?)">(.*?)</a></dd>',response.text)
  21.  
    name=re.findall('<div id="picbox"><div class="img_in"><img src="https://www.biqugevip.net/files/article/image/133/133301/133301s.jpg" alt="(.*?)"',response.text)[0]
  22.  
    # print(href)
  23.  
    for index in href:
  24.  
    index_url='https://www.biqugevip.net/book/133303/' index[0]
  25.  
    # print(index_url)
  26.  
     
  27.  
    selector=parsel.Selector(requests.get(url=index_url,headers=headers).text)
  28.  
    #h1::text提取h1标签当中的文本 get的意思是获得的意思
  29.  
    title=selector.css('#main > h1::text').get()
  30.  
    # print(title)
  31.  
    contest_list=selector.css('#content::text').getall()
  32.  
    contest="\n".join(contest_list)
  33.  
    # print(string)
  34.  
    with open(fr'D:\爬取文本存放位置\一百种爬虫方式\{name}.txt',mode='a', encoding='utf-8') as f:
  35.  
    f.write(title)
  36.  
    f.write('\n')
  37.  
    f.write(contest)
  38.  
    f.write('\n')
  39.  
    print('正在保存:',title)
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgffbkf
系列文章
更多 icon
同类精品
更多 icon
继续加载