from urllib.request import quote
import urllib.request
from bs4 import BeautifulSoup
import re
import multiprocessing
import os
import time
def start():
for txt in range(0, 999):
start = int(input("请输入开始章节(从1开始):"))-1
if start < 0 or start > len(chapter_link)-1:
print("开始章节错误,请重新输入")
else:
break
return start
def end():
for txt in range(0, 999):
end = int(input("请输入最后章节(最大为总章节数):"))-1
if end < 0 or end > len(chapter_link)-1:
print("结束章节错误,请重新输入")
else:
break
return end
def all():
filter_chapter_link = r'<a href="(.+?)">.+?</a>'
book_txt = str(soup.find_all(name="a", attrs={"href": re.compile(r"/\w+/\w+.html")}))
chapter_link_1 = re.findall(filter_chapter_link, book_txt) # 链接
chapter_link_2 = "http://www.x23us.us" + " http://www.x23us.us".join(chapter_link_1)
chapter_link = chapter_link_2.split(' ')
name=soup.h1.string
return chapter_link,name
def chapter():
links=[]
i=0
for link_chapter in range(start,end+1):
links.append(chapter_link[start+i])
i=i+1
return links
def mkdir(path):
floder=os.path.exists(path)
if not floder:
os.makedirs(path)
print("创建成功")
else:
print("文件已存在")
def remadir():
img_path2 = "E:/txt/" + name
if not os.path.exists(img_path2):
os.rename(img_path, img_path2)
print("已全部下载完成!")
else:
downloadtime = time.strftime("%Y%m%d%I%M%S", time.localtime())
os.rename(img_path, img_path2 + downloadtime)
print("已全部下载完成!\n"+"文件名:" +name + " 已存在,重命名为:" + name+ downloadtime + "\n" + "请勿重复操作")
def download(url):
req = urllib.request.Request(url) # 请求链接
req.add_header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0'} # 添加数据头
page = urllib.request.urlopen(req).read()
html = page.decode('gbk')
soup = BeautifulSoup(html, "html.parser")
book_txt = soup.find_all(name="div", attrs={"id": "content"})
txt = soup.find_all(name="h1")
name=re.sub(r'<h1>|</h1>|\|/|<|>|:|\?|\*|"|\|', '',str(txt[0]))
filter_order = r'http://www.x23us.us/.+?/(.+?).html'
order = re.findall(filter_order, url)[0]
book = name+"\n"+(
re.sub(r'<div id="content" name="content">|</div>|<br/>\n<br/>| |\n', '', str(book_txt[0]))).strip()+"\n\n" # 对过滤和编辑
f = open("E:/txt/txt/" + order+".txt", "a") # a代表追加模式,不覆盖
f.write(book.encode('gbk', 'ignore').decode('gbk'))
f.close()
print(name+"下载完成")
def change():
txtname = os.listdir("E:/txt/"+name+"/")
i = 0
txts = []
for txt in range(0, len(txtname)):
a = "E:/txt/"+name+"/"+txtname[i]
f1 = open(a, "r")
lines = f1.readlines()
o= "\n" + "".join(lines)
txts.append(o)
f1.close()
os.remove(a)
i = i + 1
txts1 = "".join(txts)
g = open("E:/txt/"+name+"/"+name+".txt", "a")
g.write(txts1)
g.close()
if __name__ == '__main__':
for txt in range(0, 999):
a = quote(input("请输入书名(精确):").encode('GBK')) # 接受一个值,编码为GBK,再转换为url编码 (顶点的编码为GBK,UTF-8和GBK的url编码不一样)
if len(a) > 1:
url = "http://www.x23us.us/modules/article/search.php?searchkey=" + a # 搜索的链接
req = urllib.request.Request(url) # 请求链接
req.add_header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0'} # 添加数据头
page = urllib.request.urlopen(req).read() # 打开链接,并读取
html = page.decode('gbk') # 顶点编码为gbk,把显示的内容转换为gbk编码
soup = BeautifulSoup(html, "html.parser")
b = len(soup.find_all(name="div", attrs={"class": "layout"}))
if b == 0:
print("搜索成功")
break
else:
print("无结果,请重新输入!")
else:
print("请输入至少2个字符长度!")
chapter_link=all()[0]
name=all()[1]
print("一共有" + str(len(chapter_link)) + "章")
start=start()
end=end()
img_path = "E:/txt/txt/"
mkdir(img_path)
links = chapter()
pool = multiprocessing.Pool(processes=10)
pool.map(download, links)
pool.close()
pool.join()
remadir()
change()
time.sleep(5)
- python 全栈开发,Day137(爬虫系列之第4章-scrapy框架)
一.scrapy框架简介 1. 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前S ...
- 【Python爬虫实战】多线程爬虫---糗事百科段子爬取
多线程爬虫:即程序中的某些程序段并行执行,合理地设置多线程,可以让爬虫效率更高糗事百科段子普通爬虫和多线程爬虫分析该网址链接得出:https://www.qiushibaike.com/8hr/pag ...
- python 全栈开发,Day135(爬虫系列之第2章-BS和Xpath模块)
一.BeautifulSoup 1. 简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: ''' Beautiful Soup提供一些简单 ...
- python 全栈开发,Day134(爬虫系列之第1章-requests模块)
一.爬虫系列之第1章-requests模块 爬虫简介 概述 近年来,随着网络应用的逐渐扩展和深入,如何高效的获取网上数据成为了无数公司和个人的追求,在大数据时代,谁掌握了更多的数据,谁就可以获得更高的 ...
- python 全栈开发,Day136(爬虫系列之第3章-Selenium模块)
一.Selenium 简介 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全 ...
- Python爬虫开发:反爬虫措施以及爬虫编写注意事项
- Python 之糗事百科多线程爬虫案例
import requests from lxml import etree import json import threading import queue # 采集html类 class Get ...
- python运维开发(十七)----jQuery续(示例)web框架django
内容目录: jQuery示例 前端插件 web框架 Django框架 jQuery示例 dom事件绑定,dom绑定在form表单提交按钮地方都会绑定一个onclick事件,所有查看网站的人都能看到代码 ...
- Java 多线程爬虫及分布式爬虫架构探索
这是 Java 爬虫系列博文的第五篇,在上一篇 Java 爬虫服务器被屏蔽,不要慌,咱们换一台服务器 中,我们简单的聊反爬虫策略和反反爬虫方法,主要针对的是 IP 被封及其对应办法.前面几篇文章我们把 ...
随机推荐
- java web从入门到精通
1.Springboot配置 1.1mybatis mapper.xml所在的目录必须为resource的资源文件夹,如果xml文件在java的package里面,需要修改文件夹类型 idea修改方式 ...
- oo第四次博客
一.测试与正确性论证比较 正确性论证是论证程序达到预期目的的一般性陈述,而该论证与程序输入数据的特定值无关,能够代表穷举性测试. 程序测试是指测试者特意跳出一批输入数据,通过运行程序,检查每个输入数据 ...
- ffmpeg推送直播流的技术进展
首先安装好NGINX并打开服务 然后安装好ffmpeg 然后参考:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=2879051 ...
- 蓝桥每周一题之1. 3n+1 问题
[问题描述] 考虑如下的序列生成算法:从整数 n 开始,如果 n 是偶数,把它除以 2:如果 n 是奇数,把它乘 3 加1.用新得到的值重复上述步骤,直到 n = 1 时停止.例如,n = 22 时该 ...
- java语言入门
Java语言的介绍: Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言. 它最初被命名为Oak,目标设定在家用电器等小型系统的编程语言,来解决诸如电视机.电话.闹钟.烤面包机等家用电器的控 ...
- day08文件操作
今日内容 文件操作 内容详细 1.打开文件 1.open("作业讲解.py",mode="r",encoding="utf-8") 2.文件 ...
- [删括号][判断可行性的dp]
链接:https://ac.nowcoder.com/acm/problem/21303来源:牛客网题目描述 给你一个合法的括号序列s1,每次你可以删除一个"()" 你可以删除0个 ...
- 新学了几个python模块,不是很鸡肋。
先说一个模块分类(基本上所有模块都是小写开头,虽然规范的写法是变量的命名规范,但是,都是这样写的) 1,C编写并镶嵌到python解释器中的内置模块 2,包好的一组模块的包 3.已经被编译好的共享库, ...
- Linux下搭建测试环境
一. 安装虚拟机 1.选择linux 型号 3.0x 64的版本 2.磁盘分区 /目录, home目录 ,boot,var ,设置root密码 3.安装(过程略) 二. 配置虚拟机网卡 路径:cd / ...
- 学习笔记DL003:神经网络第二、三次浪潮,数据量、模型规模,精度、复杂度,对现实世界冲击
神经科学,依靠单一深度学习算法解决不同任务.视觉信号传送到听觉区域,大脑听学习处理区域学会“看”(Von Melchner et al., 2000).计算单元互相作用变智能.新认知机(Fukushi ...