目前只做了在线阅读部分,其他部分由于时间原因没有写,有兴趣的可以自己补充。
#引入模块
import urllib.request,re,urllib,requests
from bs4 import BeautifulSoup
#变量配置
baseurl='https://www.bbiquge.net/modules/article/search.php?searchkey='#笔趣阁网址
booknames=[]
newpages=[]
writers=[]
nums=[]
lasttimes=[]
statuses=[]
booklinks=[]
newpagelinks=[]
pages=[]
pagelinks=[]
books=[booknames,newpages,writers,nums,lasttimes,statuses,booklinks,newpagelinks]
#文章名称,最新章节,作者,字数,更新时间,更新状态,书籍链接,最新章节链接
'''样式 输入i选定书籍,输入i+进入最新章节,10个一组
序号:i
书名:bookname
最新章节:newpage
作者:writer
字数:num
更新时间:lasttime
更新状态:status
#书籍链接:booklink
#最新章节链接:newpagelink
----------------------------------------------------------------------(70)
'''
#正则表达式配置按住ctrl点击变量可以查看应用地方
fbookname=re.compile(r'<a href="(.*?)">(.*?)</a>')#查找bookname,newpage,booklink
fnewpagelink=re.compile(r'http(.*?)" target')#查找newpagelink
fwriter=re.compile(r'<td align="center" class="odd">(.*?)</td>')#查找writer,lasttime
fnum=re.compile(r'<td align="center" class="even">(.*?)</td>')#查找num,status
fpage=re.compile(r'value="(.*?)">第')
furll=re.compile(r'(.*?)/book')
fupage=re.compile(r'html">(.*?)</a>')
fulink=re.compile(r'<a href="(.*?)">第')
bookanlayse=re.compile(r'<textarea class="T_Input" cols="100" id="con2" name="content2" rows="8">(.*?)</textarea>')
nxurl = re.compile(r'<a href="(.*?)" id="link-next">下一章</a>')
mxurl = re.compile(r'<a href="(.*?)" id="link-index" title=".*?">章节目录</a>')
fxurl = re.compile(r'<a href="(.*?)" id="link-preview">上一章</a>')
atext = re.compile('<br/><br/>(.*?)<br/><br/>')
#访问网站
def askurl(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.52",
}#进行封装
req = urllib.request.Request(url=url, headers=headers,method="POST")#访问方式
response=urllib.request.urlopen(req)#访问
html=response.read()#获取html文件
return html
#汉字转gbk内码(传入汉字book,返回对应gbk内码)
def forgbk(book,url="https://www.23bei.com/tool/54.html#"):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.52",
}#封装表头
data={'content1':book,'ok1': '字转码↓','content2': '','op':1,'op2':1,'op6':1}#封装数据/表单
response=requests.post(url,data=data,headers=headers)#访问方式
html=response.text#获得所需数据
html_soup=BeautifulSoup(html,'html.parser').find_all('textarea')#查找数据
booka=str(html_soup[1])#以下对获取到文本进行处理,转成合适的形式
bookb=str(bookanlayse.findall(booka)[0])
bookc=bookb.split(' ')
if bookc[-1]=='':
bookc.pop()
for i,item in enumerate(bookc):
bookc[i]='%'+item[0]+item[1]+'%'+item[2]+item[3]
bookurl=''.join(bookc)
return bookurl
#查找书名
def findboook(url):
print("无她阅读:正在查找...\n",'-'*70)
html=askurl(url)#调用函数,按住ctrl点击查看
html_soup=BeautifulSoup(html,'html.parser').find_all('tr')#查找数据
for item in html_soup:#遍历列表
item=str(item)#格式转换
try:#批量添加至列表
booknames.append(fbookname.findall(item)[0][1])
booklinks.append(fbookname.findall(item)[0][0])
newpages.append(fbookname.findall(item)[1][1])
newpagelink='http'+fnewpagelink.findall(fbookname.findall(item)[1][0])[0]
newpagelinks.append(newpagelink)
writers.append(fwriter.findall(item)[1])
lasttimes.append(fwriter.findall(item)[2])
nums.append(fnum.findall(item)[1])
statuses.append(fnum.findall(item)[2])
except:
pass
#文字排版书名
def typesetting(books):
n=len(books[0])
for i in range(0,n):
print('序号:',i,'\n书名:',booknames[i],'\n最新章节:',newpages[i],'\n作者:',writers[i],'\n字数:',nums[i],'\n更新时间:',lasttimes[i],'\n更新状态:',statuses[i],'\n','-'*70)
#文字排版章节名
def typesettingpage(pages):
n=len(pages)
for j in range(0,n):
print("序号:",j,pages[j])
#章节处理
def findpage(i):#输入书籍序号或网址
print("无她阅读:正在查找...\n",'-'*70)
try:#序号处理
i = int(i)
html = askurl(booklinks[i])
except:#网址处理
html=askurl(i)
html_soup=BeautifulSoup(html,'html.parser').find_all('option')#查找数据
if len(html_soup)==0:#对应数据不存在
html_soup = BeautifulSoup(html, 'html.parser').find_all('dd')#查找数据
for item in html_soup:
item = str(item)
try:#批量添加至列表
pages.append(fupage.findall(item)[0])
pagelinks.append(books[6][i] + fulink.findall(item)[0])
except:
try:#批量添加至列表
pagelinks.append(i + fulink.findall(item)[0])
except:
pass
pass
else:
for item in html_soup:
item = str(item)
try:
a = furll.search(booklinks[i])[1] + fpage.findall(item)[0]#字符串拼接
except:
pass
try:
html = askurl(a)
html_soup = BeautifulSoup(html, 'html.parser').find_all('dd')
for item in html_soup:
item = str(item)
try:
pages.append(fupage.findall(item)[0])
pagelinks.append(books[6][i] + fulink.findall(item)[0])
except:
try:
pagelinks.append(i + fulink.findall(item)[0])
except:
pass
except:
pass
#解析获取text(传入序号/序号+/网址)
def findtext(x):
print('无她阅读:正在获取...\n','-'*70)
try:#序号
x = int(x)
html = askurl(pagelinks[x])
except:
if '+' in x:#序号+
x = int(x.replace('+', ''))
html = askurl(newpagelinks[x])
else:#网址
html=askurl(x)
html_soup=BeautifulSoup(html,'html.parser')#数据解析
txt = str(html_soup.find_all('div', id="content")[0])
txt = atext.findall(txt)
for i in txt:
i = str(i).replace('<br><br>', '\n')
print(i)#输出text内容
print('-'*20,'1上一章,2章节目录,3下一章,4退出','-'*20)
x=int(input("请输入:"))#更多选项
if x==1:
string=str(html_soup.find_all('a',id='link-preview')[0])
xurl=fxurl.findall(string)[0]
findtext(xurl)
elif x==2:
string=str(html_soup.find_all('a',id='link-index')[0])
xurl=mxurl.findall(string)[0]
findpage(xurl)
typesettingpage(pages)
x = input("请输入章节序号:")
findtext(x)
elif x==3:
string = str(html_soup.find_all('a', id='link-next')[0])
xurl = nxurl.findall(string)[0]
findtext(xurl)
else:
pass
#在线阅读
def readonline():
book=input("请输入需要查询的书籍名称或url:")
if 'www.' in book:#判断是否是网址
if 'book' not in book:#判断是书籍链接还是书名搜索结果链接
try:
findboook(book)
typesetting(books)
print("输入书籍序号跳转书籍,输入‘序号+’跳转对应最新章节")
i = input("请输入书籍序号:")
if '+' in i:
findtext(i)
else:
findpage(i)
typesettingpage(pages)
x = input("请输入章节序号:")
findtext(x)
except:
print("请输入正确的url(仅支持笔趣阁站内小说查找)")
else:
try:
findpage(book)
typesettingpage(pages)
x = input("请输入章节序号:")
findtext(x)
except:
print("请输入正确的url(仅支持笔趣阁站内小说查找)")
else:
try:
bookurl = forgbk(book)
url = baseurl + bookurl
findboook(url)
typesetting(books)
print("输入书籍序号跳转书籍,输入‘序号+’跳转对应最新章节")
i = input("请输入书籍序号:")
if '+' in i:
x=i.replace('+','')
findtext(i)
else:
findpage(i)
typesettingpage(pages)
x = input("请输入章节序号:")
findtext(x)
except:
print("该小说不存在或只能通过url查找")
clears()
#离线阅读
#整书下载
#指定章节下载
#重置变量
def clears():
global booknames,newpages,writers,nums,lasttimes,statuses,booklinks,newpagelinks,pages,pagelinks,books
booknames = []
newpages = []
writers = []
nums = []
lasttimes = []
statuses = []
booklinks = []
newpagelinks = []
pages = []
pagelinks = []
books = [booknames, newpages, writers, nums, lasttimes, statuses, booklinks, newpagelinks]
#主程序
p='神明却无她'
while p!=0:
print("无她阅读\n神明却无她.制作\n数据来源:笔趣阁https://www.bbiquge.net\n请选择操作:0退出程序,1在线阅读,2离线阅读,3整书下载,4指定章节下载")
p=input('请输入:')
try:
p=int(p)
except:
print("输入数据类型错误!")
exit(0)
while p!=0:
if p==1:
readonline()
p=999#修复了一个显示问题,可以去掉看看会发生啥
elif p==2:
print("该功能暂未开放!")
break
elif p==3:
print("该功能暂未开放!")
break
elif p==999:
break
else:
print("请输入指定数字!")
break
else:
break
exit(0)