鱼C论坛

 找回密码
 立即注册
查看: 2185|回复: 1

[技术交流] 小说爬虫 xpath方式解析 没有中断回调

[复制链接]
发表于 2021-6-24 11:51:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

采集小说到本地生成txt文件,采集网站https://www.xbiquge.la,菜鸟写作,欢迎大家交流学习 QQ:343266695
  1. import requests
  2. import os
  3. from lxml import etree
  4. import random
  5. import time
  6. import pymysql

  7. def get_html(url):
  8.     headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"}
  9.     res = requests.get(url,headers=headers,timeout=600)
  10.     res.encoding = res.apparent_encoding
  11.     #res.encoding = 'UTF-8'
  12.     #res.encoding='gb2312'
  13.     htmlmu = res.text
  14.     #print(html)
  15.     return htmlmu
  16. #获取页面
  17. def get_book(url):
  18.     html = get_html(url)
  19.     selector = etree.HTML(html)
  20.     nov_name = selector.xpath('//*[@id="info"]/h1/text()')       #标题
  21.     all_urls = selector.xpath('//*[@id="list"]/dl/dd/a/@href')   #连接
  22.     nov_name = ' '.join(nov_name)  # 去除前后[]字符
  23.     print(nov_name)
  24.     print("匹配到小说共%d章节" % len(all_urls))
  25.     k=0
  26.     for part_url in all_urls:
  27.         k += 1
  28.         part_url = f'{www}{part_url}'
  29.         booktxt=get_html(part_url)
  30.         # print(booktxt.status_code)
  31.         selector = etree.HTML(booktxt)
  32.         title = selector.xpath('//*[@id="wrapper"]//div//h1/text()')  # 标题
  33.         content = selector.xpath('//*[@id="content"]/text()')  # 作者
  34.         content = str(content)
  35.         title = ' '.join(title)  # 去除前后[]字符
  36.         title = title.replace("正文卷", "")
  37.         content = content.replace("\\xa0\\xa0\\xa0\\xa0", "  ")
  38.         content = content.replace("\\r', '\\r', '", "\n\n")
  39.         content = content.replace("['", "\n\n")
  40.         content = content.replace("………………']", "")
  41.         content = content.replace("']", "\n\n")
  42.         content = content.replace("<br/>\\ufeff", "\n\n")
  43.         content = content.replace("新书就是幼苗,需要大家的呵护才能成长,求推荐票,求收藏,求大家的支持!)", "")
  44.         content = content.replace("<br/>【接近6000字的大章,因为里面有不少设定,所以就不断章了,不想看设定可以跳过去,反正也不重要(认真脸)】<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;……<br/>",
  45.                                   "<br/>")
  46.         new = []
  47.         new.append(title)
  48.         new.append(content)
  49.         down = "./txt/"
  50.         if os.path.exists(down) != True:
  51.             os.makedirs(down)

  52.         print("正在写入第%d章" % k)
  53.         with open("./txt/" + nov_name + ".txt", 'a+', encoding='utf-8')as p:
  54.             p.writelines(new)
  55.         m = random.randint(3,6)
  56.         time.sleep(m)


  57. if __name__ == '__main__':   #主函数
  58.     url="https://www.xbiquge.la/28/28564/"
  59.     www="https://www.xbiquge.la"
  60.     #url="http://www.shuquge.com/txt/139403/index.html"
  61.     # url=input("请输入采集目录地址:")
  62.     get_html(url)
  63.     get_book(url)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-22 09:27:42 | 显示全部楼层
能用的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 09:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表