爬取小说xpath (某币蛆ge)懂得都懂

免责声明:本文章涉及到的应用仅供学习交流使用,不得用于任何商业用途,数据来源于互联网公开内容,没有获取任何私有和有权限的信息(个人信息等)。由此引发的任何法律纠纷与本人无关!禁止将本文技术或者本文所关联的Github项目源码用于任何目的。

大部分小说网站都这样(特别是各种那个网站) 不懂得块可以百度, 代码仅供参考

目标网站 :https://www.xxxbiquge(.com)                                 2022/5/24 目前还在  。

需要的工具: python ;

第三方库 :parsel  request.....(自己下载)

import requests
import re
import parsel
import os

第一步 :获取需要网页源码 拿到爬取小说的源码

# url = input("请输入需要爬取的小说:")
url = 'https://www.xxxbiquge.com/5/5900/'
headers = {
    "User-Agent": # 用自己的  或者挂代理
}
resp = requests.get(url, headers=headers)

二  :拿到源码信息   每个章节的url(需要拼接) 标题(作为文件题目)

# 拿到 小说标题
select0 = parsel.Selector(resp.text)  # 解析网页 
novel_name = select0.xpath("/html/body/div[1]/div[5]/div[1]/div[2]/div[1]/div[1]/h1/text()").get()
# print(novel_name)

# re  xpath scc 去找小说内容

# re拿到小说地址
url_child = re.findall('<a style="" href="(.*?)">(.*?)</a>', resp.text)

for novel in url_child:
    # print(url)
    novel_name_child = novel[1]
    novel_url = "https://www.xxxbiquge.com" + novel[0]

三 :访问每个网页然后拿到小说内容

    resp_child = requests.get(novel_url, headers=headers).text
    # print(resp_child)
    select = parsel.Selector(resp_child)
    novel_title = select.xpath('/html/body/div[1]/div[5]/div/div/div[2]/h1/text()').get()
    # 去除 标题写入的符号
    novel_title = re.sub('[\\\/:?!<>]', '', novel_title)
    novel_comtent_list = select.css('#content::text').getall()
    novel_comtent = '\n'.join(novel_comtent_list)
    # print(novel_comtent)

四:用os创建文件

novel_name = re.sub('[\\\/:?!<>]', '', novel_name)
if not os.path.exists(f"novel\\" + novel_name):
    os.mkdir(f"novel\\" + novel_name)

最后保存: 记得关闭

    with open(f'novel\\{novel_name}\\{novel_title}.txt', mode='a', encoding='utf-8') as f:
        f.write(novel_title)
        f.write("\n")
        f.write(novel_comtent)
        print("开始打印:" + novel_title)

print("打印完成")



resp.close()

全部代码:

import requests
import re
import parsel
import os



# url = input("请输入需要爬取的小说:")
url = 'https://www.xxxbiquge.com/5/5900/'
headers = {
    "User-Agent": 
}
resp = requests.get(url, headers=headers)
# print(resp.text)

# 拿个 小说标题
select0 = parsel.Selector(resp.text)
novel_name = select0.xpath("/html/body/div[1]/div[5]/div[1]/div[2]/div[1]/div[1]/h1/text()").get()
# print(novel_name)


novel_name = re.sub('[\\\/:?!<>]', '', novel_name)
if not os.path.exists(f"novel\\" + novel_name):
    os.mkdir(f"novel\\" + novel_name)


# re  xpath scc 去找小说内容

url_child = re.findall('<a style="" href="(.*?)">(.*?)</a>', resp.text)

for novel in url_child:
    # print(url)
    novel_name_child = novel[1]
    novel_url = "https://www.xxxbiquge.com" + novel[0]
    # print(novel_url, novel_name)
    resp_child = requests.get(novel_url, headers=headers).text
    # print(resp_child)
    select = parsel.Selector(resp_child)
    novel_title = select.xpath('/html/body/div[1]/div[5]/div/div/div[2]/h1/text()').get()
    # 去除 标题写入的符号
    novel_title = re.sub('[\\\/:?!<>]', '', novel_title)
    novel_comtent_list = select.css('#content::text').getall()
    novel_comtent = '\n'.join(novel_comtent_list)
    # print(novel_comtent)


    # 创建存储文件
    with open(f'novel\\{novel_name}\\{novel_title}.txt', mode='a', encoding='utf-8') as f:
        f.write(novel_title)
        f.write("\n")
        f.write(novel_comtent)
        print("开始打印:" + novel_title)

print("打印完成")



resp.close()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值