励志一生网站的数据,新手比较难爬,特别是列表页的页码获取比较艰难,这里使用了正则表达式,内容的获取使用了BeautifulSoup库,并且在爬内容的时候出现了乱码,已完美解决!
本文声明:仅用于爬虫学习,请勿商业和恶意攻击网站,本文解释权归作者所有。
from bs4 import BeautifulSoup
import requests
import os
import re
# 设置url
url = "https://www.lz13.cn/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
'Referer':'https://www.lz13.cn/'
}
# 请求页面数据
response = requests.get(url=url,headers=headers)
page_text =response.content
soup =BeautifulSoup(page_text,"lxml",exclude_encodings="utf-8")
# 获取所有“更多”按钮的跳转连接
more_list = soup.select(".more")
#移除第一个不符合的元素
more_list.pop(0)
# 进入列表页面
def get_strive_list(strive_url):
strive_list_content = requests.get(url=strive_url,headers=headers).content
# 这里获取到的strive_list_content,是网站内容,是乱码,所以我是用content字节获取
soup = BeautifulSoup(strive_list_content,"lxml",exclude_encodings="utf-8")
# 然后使用BS的参数exclude_encodings="utf-8"转码,可以完美解决文字乱码问题
strive_list = soup.select(".PostHead>span>h3>a")
title = soup.select(".lz13Title>h2")
for t in title:
title_name = t.text
for s in strive_list:
# 拿到标题 死撑,不是坚持的唯一打开方式
strive_title = s.text
# 拿到标题对应的url https://www.lz13.cn/lizhiwenzhang/184195.html
strive_list_url = s["href"]
# 调用list对应的详情页 返回详情页内容
content = get_strive_detail(strive_list_url)
# 上面是第一页列表,执行完循环就没他什么事儿了
# 判断目录是否存在
file_path = "./励志人生/%s/"%title_name
if not os.path.exists(file_path):
os.mkdir(file_path)
try:
file_name = file_path + strive_title + ".txt"
with open(file_name,"w",encoding="utf-8") as f:
f.write(content)
print("%s的首页,%s下载完成"%(title_name,strive_title))
except OSError:
continue
print("----------------------------------------------首页下载完成")
#下面是获取总页数
# 获取下一页的链接,如图有下一页就递归自己,没有下一页就进入下一个for循环
next_url_list = soup.select(".pager a")
next_url_list.pop(0)
next_url_list.pop(-1)
# 去掉第一页和跳到最后一页的按钮链接 <a href="https://www.lz13.cn/lizhi/lizhiwenzhang-75.html">2</a>
for n in next_url_list:
num_url = n["href"] # https://www.lz13.cn/lizhi/lizhiwenzhang-75.html
num = re.findall(".*?-(\d+)\..*?",num_url) # ['75']
# a_url = re.sub("(\d+)",str(70),num_url) #https://www.lz70.cn/lizhi/lizhiwenzhang-70.html
for next in range(int(num[0]),0,-1):
next_nrl = re.sub("-(\d+)","-" + str(next),num_url) #正则不加 -,会把主路由的13页给替换了
# 从第二页开始 重新搞列表页
strive_list_content = requests.get(url=next_nrl, headers=headers).content
soup = BeautifulSoup(strive_list_content, "lxml", exclude_encodings="utf-8")
strive_list = soup.select(".PostHead>span>h3>a")
for s in strive_list:
# 拿到标题 死撑,不是坚持的唯一打开方式
strive_title = s.text
# 拿到标题对应的url https://www.lz13.cn/lizhiwenzhang/184195.html
strive_list_url = s["href"]
# 调用list对应的详情页 返回详情页内容
if str(strive_title).endswith("?") or str(strive_title).endswith("?"):
strive_title = str(strive_title)[:-1]
elif str(strive_title).find('\"'):
strive_title = str(strive_title).replace('\"','')
# 因为列表中的很多名字含有英文标点符号不能用于文件名,所以使用了Python错误处理
try:
file_name = file_path + strive_title + ".txt"
with open(file_name, "w", encoding="utf-8") as f:
f.write(content)
print("%s,第-%s页,%s下载完成"%(title_name,next,strive_title))
except OSError:
continue
# 外面循环是为了拿总页数,所以只执行一遍就退出循环
break
# 进入详情页面
def get_strive_detail(strive_list_url):
strive_detail_content = requests.get(url=strive_list_url,headers=headers).content
soup = BeautifulSoup(strive_detail_content,"lxml",exclude_encodings="utf-8")
strive_detail_text_list = soup.select(".PostContent p")
# print(strive_detail_text_list)
content = ""
for c in strive_detail_text_list:
content = content + c.text + "\n"
return content
# 循环出每个链接
for i in more_list:
more_url = i["href"]
# print(more_url)
# strive 努力,奋斗,争取 美 [straɪv]
get_strive_list(more_url)