python爬虫智能翻页批量下载文件的实例详解
作者:宋宋大人 时间:2023-06-20 14:17:12
python爬虫遇到爬取文件内容时,需要一页页的翻页爬取,这样很是麻烦,其实可以获取每个列表信息下的文件名和文件链接,让文件名和文件链接处理为列表,保存后下载,实现智能翻页批量下载文件,本文以以京客隆为例,批量下载文件,如财务资料,他的每一份报告都是一份pdf格式的文档。以此页面为目标,下载他每个分类的文件python爬虫实战之智能翻页批量下载文件。
1、引入库
import requests
import pandas as pd
from lxml import etree
import re
import os
2、解析初始页面
baseUrl ='http://www.jkl.com.cn/cn/invest.aspx' # 爬取页面的数据
heade ={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/81.0.4044.92 Safari/537.36'
}
res =requests.get(url=baseUrl,headers=heade).text
# print(res.text)
html = etree.HTML(res)
res =requests.get(url=baseUrl,headers=heade).text # 设置变量接受 基础页的响应数据
# print(res.text)
html = etree.HTML(res)
3、获得投资关系的分类名称和url地址
data_name = html.xpath('//div[@class="infoLis"]//a/text()') # 投资者列表的名字
data_link = html.xpath('//div[@class="infoLis"]//@href') # 全部列表的链接
name = [data_name.strip() for data_name in data_name] # 通过for循环去掉空字符
link = ['http://www.jkl.com.cn/cn/'+ data_link for data_link in data_link] # 拼接字符串
# 合并为字典,方便保存文件
file = dict(zip(name,link))
4、每个列表信息,保存一个文件夹
for name,link in file.items():
name=name.replace('/','.')
name=name.replace('...','报表')
# 上面的把文件名带特许字符的 强制转换为我们想要的文本类型
path = 'E:/'+ name
if not os.path.exists(path):
os.mkdir(path)
#建立储存位置
5、对列表的每个项目链接进行解析,拿到尾页
res_list = requests.get(url = link, headers = heade).text
list_html = etree.HTML(res_list)
# print(html_erJi) 解析每个分类的链接
weiYe = list_html.xpath('//a[text()="尾页"]/@href')
# print(html_weiye)
# 拿到尾页信息
if weiYe !=[]:
# 正则提取尾页信息
get_weiYe =re.search("(\d+)'\)",html_weiye[0])
get_yeMa = get_html_weiYe.group(1)
else:
get_yeMa=1
# print(get_html_yeMa) 看看是不是提取成功
6、获取每个列表信息下的文件名和文件链接
for get_yeMa in range(1,int(get_yeMa)+1): # 翻页
yaMa= {
'__EVENTTARGET': 'AspNetPager1',
'__EVENTARGUMENT': get_yeMa
}
get_lei_html = requests.get(url = link, headers = heade, params = yaMa).text
res3 =etree.HTML(get_lei_html)
# print(res3)
pdf_name = res3.xpath('//div[@class="newsLis"]//li/a/text()')
# print(pdf_name)
pdf_url = res3.xpath('//div[@class="newsLis"]//li//@href')
7、让文件名和文件链接处理为列表,保存后下载
pdf_names = [pdf_name.strip() for pdf_name in pdf_name]
# print(pdf_names)
if all(pdf_url):
pdf_urls = ['http://www.jkl.com.cn'+pdf_url for pdf_url in pdf_url]
# print(pdf_url)
pdf_data=dict(zip(pdf_names,pdf_urls)) # pdf地址和名字整合为字典
for pdfName,pdfUrl in pdf_data.items():
pdfName =pdfName.replace('/','.')
res_pdf= requests.get(url =pdfUrl,headers=heade).content
houZui = pdfUrl.split('.')[-1]
pdf_pash = path + '/' + pdfName + '.'+ houZui #
# print(pdf_pash)
with open(pdf_pash,'wb') as f:
f.write(res_pdf)
print(pdfName,'下载成功')
来源:https://www.py.cn/spider/example/23545.html
标签:python爬虫,下载文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Sql学习第一天——SQL UNION 和 UNION ALL 操作符认识
2024-01-18 23:33:26
Python tabulate结合loguru打印出美观方便的日志记录
2021-02-23 02:39:45
如何获得ADO的连接信息?
2009-11-23 20:33:00
Python keras.metrics源代码分析
2023-10-28 20:58:14
MySQL中 and or 查询的优先级分析
2024-01-12 13:53:50
![](https://img.aspxhome.com/file/2023/5/70465_0s.png)
Python实现Pig Latin小游戏实例代码
2022-06-27 04:15:36
asp如何实时获知多少人在线?
2010-05-18 18:34:00
七个生态系统核心库[python自学收藏]
2021-09-17 03:51:12
![](https://img.aspxhome.com/file/2023/9/101699_0s.jpg)
python实现在列表中查找某个元素的下标示例
2023-08-20 07:16:38
![](https://img.aspxhome.com/file/2023/5/131155_0s.png)
pandas读取csv文件提示不存在的解决方法及原因分析
2022-10-27 19:08:02
SQL2005 自动备份的脚本
2024-01-23 20:34:27
python 爬取百度文库并下载(免费文章限定)
2022-05-08 16:18:28
![](https://img.aspxhome.com/file/2023/3/65413_0s.png)
SQL Server树型结构数据处理的存储过程
2010-01-26 13:40:00
![](https://img.aspxhome.com/file/UploadPic/20101/201012615399712s.jpg)
500行代码使用python写个微信小游戏飞机大战游戏
2023-01-16 06:58:09
![](https://img.aspxhome.com/file/2023/3/76703_0s.gif)
如何把ASP源代码编写成DLL组件
2007-10-19 13:49:00
Django数据统计功能count()的使用
2022-12-21 23:49:29
![](https://img.aspxhome.com/file/2023/5/115405_0s.png)
python画图中文不显示问题的解决方法
2023-05-30 14:07:09
![](https://img.aspxhome.com/file/2023/4/103694_0s.jpg)
利用Python将图片中扭曲矩形的复原
2022-05-24 21:35:54
![](https://img.aspxhome.com/file/2023/3/95233_0s.jpg)
利用python数据分析处理进行炒股实战行情
2022-01-30 10:28:29
![](https://img.aspxhome.com/file/2023/9/68909_0s.jpg)
Windows服务器MySQL中文乱码的解决方法
2024-01-12 16:46:51
![](https://img.aspxhome.com/file/2023/2/126312_0s.png)