一、请求服务器
1.分析网页
我们在请求服务器之前要先看一下这个网页的构造,我们可以看到它的信息全部都是通过html加载出来的,每一个url 都可以对应一篇小说,所以我们只需要提取到该小说目录的所有链接就可以获取到没有个章节的内容了。
2.获取html文件
import requests
from lxml import etree
url = "http://www.shuquge.com/txt/76615/14606964.html"
reponse = requests.get(url)
print (reponse.text)
运行结果为:
可以看到本来应该是文字格式的地方全被乱码替换了,所以接下来就需要对此代码进行解码才能爬取我们需要的文字信息。
3.解码
#解码
reponse.encoding = reponse.apparent_encoding
print (reponse.text)
通过解密,文本变回正常,接下来就开始下面的爬取步骤。
二、提取并保存信息
1.提取内容
这里仍然使用的lxml
来提取信息,先提取它的标题作为txt文件的名称,并把内容保存进文件内:
from lxml import etree
#解析
etree_html = etree.HTML(reponse.text)
#提取标题
title = etree_html.xpath('//*[@id="wrapper"]/div[4]/div[2]/h1/text()')
print (title)
#提取内容
content = etree_html.xpath('//*[@id="content"]//text()')
text = "".join(content)
text
运行结果为:
2.保存进txt
with open('./第一章 白纸人.txt', 'a', encoding='utf-8') as file: #创建并打开一个文件
file.write(text) #放进去内容,写入
file.close() # 关闭
jupyter运行结果:
txt截图:
3.封装函数
3.1封装函数的方法
#这个方法没有被执行,因为没有入口
def start():
print ("啊啊啊啊啊啊")
# 没有数据返回类型
def funtion():
print ("内容已经被执行")
# 有数据返回型
def get_info(b):
a = b + 10
return a
if __name__ =='__main__':
funtion() #直接进入funtion()中执行
c = get_info(5) #把5传到get_info()方法中,返回值赋给c
print (c)
结果:
内容已经被执行
15
记忆点:start()
方法没有被执行是因为它没有被调用,所以没有执行;我们可以把方法理解为一个房子,它有一扇唯一的门 def funtion()
,我们要进入其中,必须要有对应的钥匙funtion()
才能进入;如果我们要带东西进去,那么必须要先告诉我们是否可以带东西进入?能带多少个?它的门可以为 get_info(b)
,钥匙就为 get_info
(这里放要带的东西,多个用逗号分开) ,需要传出来的值可以用 return
传出,并找一个值接收,如上面的 c
。
3.2封装函数
from lxml import etree
filePath = r"C:\Users\ZYZ\Desktop\小说\\"
#解析
def download_text(url):
reponse = requests.get(url)
#解码
reponse.encoding = reponse.apparent_encoding
etree_html = etree.HTML(reponse.text)
#提取标题
title = etree_html.xpath('//*[@id="wrapper"]/div[4]/div[2]/h1/text()')
print (title)
#提取内容
content = etree_html.xpath('//*[@id="content"]//text()')
text = "".join(content)
text
with open(filePath + title[0] + '.txt', 'a', encoding='utf-8') as file: #创建并打开一个文件
file.write(text) #放进去内容,写入
file.close() # 关闭
if __name__ =='__main__':
target_url = "http://www.shuquge.com/txt/76615/14606965.html"
download_text(target_url)
运行结果:
三、爬取首页所有链接
因为我们只需要 href=" "
双引号中的内容,这部分没有乱码,所以不需要解码。我们获取的 href
标签内容不是一个完整的链接,需要参考它原理完整链接自己拼接出来,代码为:
index_etree = etree.HTML(index_html)
dd = index_etree.xpath('/html/body/div[5]/dl/dd')
for item in dd:
href = "http://www.shuquge.com/txt/76615/"+item.xpath('./a/@href')[0]
print(href)
运行结果:
四、爬取所有章节(代码汇总)
import requests
from lxml import etree
filePath = r"C:\Users\ZYZ\Desktop\小说\\"
#下载方法及文字提取
def download_text(url):
#导入包
reponse = requests.get(url)
#解码
reponse.encoding = reponse.apparent_encoding
#解析
etree_html = etree.HTML(reponse.text)
#提取标题
title = etree_html.xpath('//*[@id="wrapper"]/div[4]/div[2]/h1/text()')
print (title)
#提取内容
content = etree_html.xpath('//*[@id="content"]//text()')
text = "".join(content)
text
with open(filePath + title[0] + '.txt', 'a', encoding='utf-8') as file: #创建并打开一个文件
file.write(text) #放进去内容,写入
file.close() # 关闭
#提取所有文章的链接
def get_link(index_url):
index_html = requests.get(index_url).text
index_etree = etree.HTML(index_html)
dd = index_etree.xpath('/html/body/div[5]/dl/dd')
for item in dd:
href = "http://www.shuquge.com/txt/76615/"+item.xpath('./a/@href')[0]#拼接出完整的链接
link_list.append(href)#打包所有的链接
return link_list
if __name__ =='__main__':
index_url = "http://www.shuquge.com/txt/76615/index.html"
links = get_link(index_url)
for link in links:
target_url = link
print ("正在爬取:", target_url)
download_text(target_url) #带进去的东西
jupyter运行结果:
小说爬取结果: