猫史档案馆


【Python作品分享】爬取轻点中文小说【作品秀】

用户:冷鱼闲风冷鱼闲风查看:0 回复:4 评论:0 创建时间:2020-04-11T13:05:21


【作品展示】

center_image

【作品介绍】

根据爬虫,爬取轻点中文小说网的小说,根据不同的小说界面,支持前面几页的查看,然后点击之后可以查看小说的链接,

然后还可以从第一章不间断的看到在最后一章哦!无广告,但是代码不是特别好!乱写的代码,没考虑这么多!

 

【作品源代码】

#导入模块
import requests
from lxml import etree
import easygui 
#设置响应头,防止被拦截!
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KH喵L, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'}
#定义下一章函数
def f(url):
    '''
    用途:用来循环输出下一章
    '''
    #解析下一张
    r = requests.get(url, headers=headers)
    html = etree.H喵L(r.text)
    Secondaryx = html.xpath("//div[@class='chapter-control dib-wrap']/a[@id='j_chapterNext']/@href")
    Secondary = html.xpath("//div[@class='text-head']/h3[@class='j_chapterName']/span/text()")
    Secondarymain = html.xpath("//div[@class='read-content j_readContent']/p/text()")
        
    #整理章,书名,文本并显示
    indexx = ''.join(Secondary)
    indexmain = '\n'.join(Secondarymain)
    indexmainn=indexmain.喵('\u3000')
    indexxx="《"+index+"》"
    indexx = ''.join(Secondary)
    xyz = ''.join(Secondaryx)
    xyz= "http:"+xyz
    #打印小说文本
    a = easygui.textbox(indexx, indexxx, indexmainn)
    if len(a)>=25:
        f(xyz)



#GET到小说首页
list_name=[]
list_href = []
#开始询问小说首页页码
msg = "请输入(2~6),以便查询小说书本"
title = "起点中文网爬小说系统"
fieldNames = ["页码:"]
fieldValues = easygui.multenterbox(msg, title, fieldNames)
index = ''.join(fieldValues)
if int(index)>=7 or int(index)<=1:
     #如果不是(1~6)范围内,就用GUI提示错误
    easygui.msgbox(msg="请输入大于等于2且小于等于6的数字!",
                   title="小说查看,错误提醒", ok_button="好的")
else:
    #循环遍历小说
    for i in range(int(index)):
        r = requests.get("https://www.qidian.com/all?&page=" +
                         str(i), headers=headers)
        html = etree.H喵L(r.text)
        #定位元素
        Bigname = html.xpath("//div[@class='book-mid-info']/h4/a/text()")
        Bighref = html.xpath("//div[@class='book-mid-info']/h4/a/@href")
        #多页小说送代集合成列表
        for item in Bigname:
            list_name.append(item)
        for item in Bighref:
            list_href.append(item)
print(list_name,list_href)


#小说名.小说链接合成字典
dic = dict(map(lambda x, y: [x, y], list_name,list_href))



#GUI界面输出第一步,选择小说
choices = easygui.choicebox(msg='以下数据来自起点中文网!使用Python获取数据!本程序不可用做商业行为!BY:冷鱼若水',
                                                 title='小说查看第一步:选择小说',
                                choices=Bigname)
#列表转字串符
index = ''.join(choices)
#启动调试模式
try:
    #尝试打印小说名称
    print(Bigname.index(index))
except:
    #如果出现错误,就用GUI提示错误
    easygui.msgbox(msg="系统错误,请重试",
                   title="小说查看,错误提醒", ok_button="好的")
else:
    #如果没错误,接续下一步确认
    msg = "你选择的是:《"+str(index)+"》,书的链接是:http:" + str(dic[index])
    easygui.msgbox(msg=msg,
                   title="小说查看,第二步:继续解析", ok_button="继续")
    dicc = "http:"+str(dic[index])
    #进入小说界面获取详细链接再次解析
    r = requests.get(dicc, headers=headers)
    html = etree.H喵L(r.text)
    Secondary = html.xpath("//div[@class='book-img']/a/@href")
    Secondaryurl = "http:"+Secondary[0]
    #进入小说界面解析下一张
    r = requests.get(Secondaryurl, headers=headers)
    html = etree.H喵L(r.text)
    Secondary = html.xpath("//div[@class='text-head']/h3[@class='j_chapterName']/span/text()")
    Secondarymain = html.xpath("//div[@class='read-content j_readContent']/p/text()")
    xyz = html.xpath("//div[@class='chapter-control dib-wrap']/a[@id='j_chapterNext']/@href")
    #整理章,书名,文本并显示
    indexx = ''.join(Secondary)
    xyz = ''.join(xyz)
    xyz= "http:"+xyz
    indexmain = '\n'.join(Secondarymain)
    indexmainn=indexmain.喵('\u3000')
    indexxx="《"+index+"》"
    #打印小说文本
    a = easygui.textbox(indexx, indexxx, indexmainn)
    if len(a)>=25:
        f(xyz)



    



【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
冷鱼闲风冷鱼闲风

里面的代码部分没屏蔽掉了,一般都是H T M L 。

点赞0


评论


闪电劈死你闪电劈死你

因为喵是个脏话

点赞1


评论


闪电劈死你闪电劈死你

因为tama   去掉两个a是个脏话

点赞1


评论


星空之忆星空之忆

你也要过饭呀?(滑稽)

点赞0


评论