logo资料库

python爬取第一PPT爬虫PPT.docx

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
结果如下 网站分析
选择自己需要下载的类型,这边我选取的是工作计划进行爬取下载 http://www.1ppt.com/xiazai/jihua/ppt_jihua_1.html 我的是谷歌浏览器 ,按 F12 或者右击点检查,之后按 F5 刷新重新抓取数据 ,打开network 找到当前页面地址,找到请求头request headers 上面的 Cookie 和User-Agent 试过了如果去除 cookie 是拿不到数据的。 注意:cookie 每次切换其他模板(总结 PPT)的时候都需要更换一下,不然是加密数据 右击检查第一个 PPT,找到 href 链接, 这个 5 个数字是我们后面需要用到的 aid
随便打开两个 PPT 进入下载页面 发现网页上不同的是 aid, 上面我们已经知道 aid 是怎么来的了 http://www.1ppt.com/plus/download.php?open=0&aid=74311&cid=3 http://www.1ppt.com/plus/download.php?open=0&aid=39784&cid=3 右击检查下载按钮,发现这个就是我们需要抓取的 PPT 文件,压缩包。所以我们往前面找这个 aid。就能进行 url 的拼接进行下载
不多 BB,附上代码 import requests import time import random import parsel def get_url_id(): #用来存 aid id = [] #自己根据实际的总页码进行范围获取 for page in range(1,14): #加个暂停时间,避免一次就被反爬 time.sleep(3) url = headers = { 'Cookie': f'http://www.1ppt.com/xiazai/zongjie/ppt_zongjie_{page}.html' 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3573.0 Safari/537.36', 'UM_distinctid=17692865dd2557-018bd625c95955-6655742e-1fa400-17692865 dd3630; __gads=ID=f437d6e56fba8990-22377a9451c50051:T=1608776070:RT=160877607 0:S=ALNI_MaLfbX-OxcIBOxksbzk3OOxxm3G4w; acw_sc__v2=5fe41bbe69be4b911c9979f186ab6e51ee0ffb7a; CNZZDATA5092133=cnzz_eid%3D1600233145-1608775575-%26ntime%3D160878637 5; acw_tc=2760828816087865362652910e5a0cae37845089b57eeff2dddc313a460059 '
} response = requests.get(url,headers=headers) response.encoding = 'gb2312' html_data = response.text # print(html_data) #数据解析,获取我们想要得到的PPT 链接href parse = parsel.Selector(html_data) li_all = parse.xpath('//ul[@class="tplist"]/li') for li in li_all: url_id = li.xpath('./a/@href').get() print(url_id[9:14]) #对链接进行切片处理,我们只需要其中的 5 位数字 id.append(url_id[9:14]) print(id) return id #下载 def down_url(get_url_id): headers1 = { 'Cookie': 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3573.0 Safari/537.36', 'UM_distinctid=17692865dd2557-018bd625c95955-6655742e-1fa400-17692865 dd3630; __gads=ID=f437d6e56fba8990-22377a9451c50051:T=1608776070:RT=160877607 0:S=ALNI_MaLfbX-OxcIBOxksbzk3OOxxm3G4w; acw_tc=2760825916087847079424973e0d47a3cfeb93200107412da24690cfab77ad; acw_sc__v2=5fe41bbe69be4b911c9979f186ab6e51ee0ffb7a; CNZZDATA5092133=cnzz_eid%3D1600233145-1608775575-%26ntime%3D160878637 5'} for id in get_url_id: #拼接下载的 url 地址 down_url = 'http://www.1ppt.com/plus/download.php?open=0&aid=' + f'{id}' + '&cid=3' print(down_url) response1 = requests.get(url=down_url,headers=headers1) response1.encoding ='gb2312' html_data1 = response1.text parse1 = parsel.Selector(html_data1) down_li = parse1.xpath('//ul[@class="downloadlist"]/li[1]/a/@href').get() parse1.xpath('//dl[@class="downloadpage"]/dt/h1/a/text()').get() down_title = print(down_li)
# down_href = li.xpath('').get() # print(down_href) zipdata = requests.get(down_li).content num = num + 1 #保存数据在当前路径创建 PPT 文件夹 with open('PPT\\'+str(down_title) +'.zip','wb') as f : f.write(zipdata) print('保存成功') down_url(get_url_id())
分享到:
收藏