用python抓取网页中所有pdf文件的笨方法
作者:互联网
-
进入下载中心: https://www.sensirion.com/en/download-center/
-
在网页任意地方点击右键,后选择inspection
-
右边选择elements
-
一直向下翻找到 “catgroup downloads"
或者合并第3-4步,直接在本页第一个下载链接点击右键,选择inspection
这样可以直接在链接上面看到catgroup downloads
-
此时可以看到,所有的catgroup downloads,这里面包含了https://www.sensirion.com/en/download-center/所有的应用文档
-
点击右键每个
,选择Edit as HTML
后出现当前 中间的所有内容
-
将里面的信息都复制到文件DownloadCenter_catgroupDownloads.txt中
-
python scripter下载这个txt中间的所有pdf
# -*-coding:utf-8 -*-
import urllib.request as urllib2
import os
def downpdf(pdflist):
x = 0
for pdfurl in pdflist:
print(pdfurl)
name = pdfurl.replace(".","/").split("/")[-2]
filename = r"C:\MorganPersonalFile\Sales\Python Script\72. Grap_data\Sensirion_downloadCenter\DownloadCenter\\" + name + ".pdf"
f = open(filename,'wb')
f.write(urllib2.urlopen(pdfurl).read())
f.close()
x += 1
print ("download %s pdf>>>>" %x)
else:
print ("download finished")
folder = os.path.dirname(os.path.realpath(__file__))
infoName = folder+"\\"+r'DownloadCenter_catgroupDownloads.txt'
pdflist = []
with open(infoName, 'r') as f:
lines = f.readlines()
for i in range(len(lines)):
if ".pdf" in lines[i]:
# print(lines[i].replace('href="','">').split('">'))
pdflist.append(lines[i].replace('href="','">').split('">')[1])
downpdf(pdflist)
- 清除DownloadCenter_catgroupDownloads.txt文件中间的内容,然后第二个catgroup downloads重复第6-7-8步
- 对所有catgroup downloads操作以后,所有的pdf文件都下载到本地电脑了
标签:python,pdflist,downloads,lines,抓取,catgroup,download,pdf 来源: https://blog.csdn.net/morgan2018/article/details/121652187