使用Python看网络小说

本文所述内容及其低级,仅为入门水平,高手勿阅。

时值疫情期间,女友来家竟然惨遭滞留。本人因看网络小说,经常遭遇女友与老妈的双重监管(他们两个商量好了要督促我学习)

被逼无奈,使用Python书写基础功能的看小说工具(仅使用控制台打印),可自动换页与换章。

详情如下:

使用PyCharm+Anaconda3 环境,包缺啥装啥,直接在PyCharm里装,极其简单粗暴。

import time

import requests
import urllib3

from bs4 import BeautifulSoup


#目标网址
url = "http://m.vipzw.com"              # 主连接地址
child_url = "/wapbook-7327-30323063/"   # 子链接地址

AutoNextPage = True    # 自动获取下一页
NextPageWait = 0       # 自动获取间隔,单位秒

AutoNextUnit = False   # 自动获取下一章
NextUnitWait = 10      # 自动获取间隔,单位秒

CharNumEveryLine = 20  # 一行显示多少字

if not AutoNextPage or not AutoNextUnit:
    inp=input()        # 如果并非全自动获取,需要在控制台敲击回车获取接下来的内容
else:
    inp = ''

while(inp!='exit'):    # 如果并非全自动获取,在控制台输入exit退出程序

    temp = 0
    Text = ''
    #使用request去get目标网址
    res = requests.get(url + child_url)
    res.encoding="UTF8"
    soup = BeautifulSoup(res.text, "html.parser")
    for descendant in list(soup.body.descendants):
        if descendant.name!=None:
            if not descendant.name == 'a':
                if descendant.name == 'p':
                    if not descendant.text in Text:
                        Text += descendant.text
                elif descendant.name == 'div':
                    if not descendant.text in Text:
                        Text += descendant.text
            else:
                if descendant.text == '下一页':
                    temp = 1
                    child_url = descendant.attrs['href']
                elif descendant.text == '下一章':
                    temp = 2
                    child_url = descendant.attrs['href']

    Text = Text.replace('&nbsp;',' ').replace('<br />','\n').replace('\n\n','\n')\
        .replace('\n\n','\n').replace('\n\n','\n').replace('    ','')


    i = 0
    for c in Text:
        print(c,end='')
        i+=1
        if c=='\n':
            i=0
        if i>CharNumEveryLine:
            print('\n',end='')
            i=0

    if temp == 1 and AutoNextPage:
        time.sleep(NextPageWait)
    elif temp == 2 and AutoNextUnit:
        time.sleep(NextUnitWait)
    else:
        print('\nNextUrlTo:',url + child_url)
        inp = input()

运行结果如下:


返回
487【空白市场】
首页
报错
关灯
护眼
字:大 中 小
codfish1(); 
上一章
回目录
下一页
进书架
最新网址:m.vipzw.com     
487【空白市场】 (第1/3页)    
说起连锁超市,第一反应大概会想起沃尔玛、家
乐福之类。
    但在2000年左右,这些洋超市虽然
已经进入中国,其影响力却远远不如中国本土的

......

贴的太多可能会侵权,毕竟不是自己写的小说呜......(叫《重生野性时代》)

还有优化空间,我只想做个最简单的逃脱监管,毕竟用PyCharm看他们不知道我在干啥,以为我在调试程序哈哈哈

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值