好友
阅读权限20
听众
最后登录1970-1-1
|
本帖最后由 小心boss 于 2019-12-3 22:10 编辑
# 现在想下本小说还真是不容易....
# 自己动手,丰衣足食!!!
# 脚本没有加入多线程,速度比较慢,开始下载了抽根烟再回来把,哈哈
各位大佬,如果感觉有用的话,给个分,想升级......拜谢!
3楼有打包好的EXE文件,WIN下可以直接运行...懒人福利
[Python] 纯文本查看 复制代码
'''
人生苦短,我用Python
'''
###以此怀念###
import re
import os
import lxml
import random
import requests
from bs4 import BeautifulSoup
###################### 全局变量 ##############################
book_url = ''
ua = ['Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1',
'Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)'
]
Usera = random.choice(ua) #随机一个ua
headers = {'User-Agent':Usera,
'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
'Accept-Encoding': 'gzip',
"Referer": "https://www.bqg3.com/quanbuxiaoshuo/"} # 创建头部信息
##############################################################
def get(url): # 发送网络请求
a = requests.get(url, headers=headers)
a.encoding= 'gb18030'
html = a.text
return html
def main(book_name):
find_book(book_name)
if book_url != '':
print('正在获取小说章节信息,请稍后...')
mulu = BeautifulSoup(get(book_url), 'lxml') # 解析爬取网址
all_zj = mulu.find('dl').find_all('a')
for j in all_zj:
mulu_list = [j.get('href')] # 过滤出章节链接
zj_name = j.contents[0]
zj_name = str(re.findall(r'\w*',zj_name))
zj_name = zj_name.replace(("'"),'')
zj_name = zj_name.replace((" "),'')
zj_name = zj_name.replace((","),'') #过滤掉会导致异常的字符
for k in mulu_list:
zj_url = 'https://www.bqg3.com' + k
a = (zj_name,zj_url)
down(a) #调用下载函数
def down(text_url):
zj_name = text_url[0]
txt_url = text_url[1]
soup = BeautifulSoup(get(txt_url), 'lxml')
zz = r'<div id="content">(.*?)</div>'
text = re.findall(zz, str(soup.contents[1]), re.S)
text = text[0].replace('<br/>', '')
text = text[30:-len(book_url)]
try:
with open(book_name + '.txt', 'a+',encoding='utf-8')as f:
print('下载小说章节%s...' % zj_name, txt_url)
f.write('\n'+zj_name+'\n\n'+text+'\n')
except Exception as e:
print('错误信息: ',e)
def new_dir(name): # 创建文件夹
if os.path.exists(name):
print('"%s" 文件夹已存在'%name)
os.chdir(name)
else:
print('创建文件夹: {}'.format(name))
os.mkdir(name)
os.chdir(name)
def find_book(name):
global book_url
print('正在书库中查找 <%s> 请稍后...'%name)
url1 = 'https://www.bqg3.com/quanbuxiaoshuo/'
soup = BeautifulSoup(get(url1),'lxml')
all_book = soup.find('div', id='main').find_all('a')
for i in all_book:
if i.contents[0] ==name:
book_url = i.get('href')
print(' 小说URL地址: ',book_url)
break
else:
print('''# 该小说未找到或网站未收录!!! #\n# 请确认书名是否输入正确! #\n# 重试或搜索别的小说试试吧! #''')
if __name__ == '__main__':
print("####################################################################")
print("# 现在想下本小说还真是不容易....")
print("# 自己动手,丰衣足食!!!")
print("# 脚本没有加入多线程,速度比较慢,开始下载了抽根烟再回来把,哈哈")
print("# 抓取网址:https://www.bqg3.com/ ")
print("# 以此怀念 2019.12.03 ")
print("####################################################################\n")
book_name = input('请输入要下载的小说名称:')
new_dir(name='D:\DownBook')
main(book_name)
############################## End 2019.12.03 ######################################
|
免费评分
-
查看全部评分
|