【发布时间】:2021-11-03 12:54:17
【问题描述】:
我正在尝试完成以下任务: - 在需要登录的网页上查找所有 .PDF 文件 - 将 .PDF 文件重命名为只有文件名而不是完整 URL - 在本地用户桌面上创建一个文件夹 - 仅下载已创建文件夹中不存在的文件 - 将给定的 .PDF 文件下载到新文件夹
下面的代码登录网站并检索所有 .PDF 文件,将名称斜线仅是文件名并将它们下载到文件夹中。但是所有关闭的文件似乎都已损坏(无法打开)
任何关于如何修复它的反馈或建议都将不胜感激。 (有效负载已更改为不泄露任何凭据)
附加信息:
Sampleurl 是登录后网站的主页。 Loginurl 是用户获得身份验证的页面 secure_url 是包含我要下载的所有 .PDF 的页面
代码:
# Import libraries
import requests
from bs4 import BeautifulSoup
import os
from pprint import pprint
import time
import re
from urllib import request
from urllib.parse import urljoin
import urllib.request
# Fetch username
username = os.getlogin()
# Set folder location to local users desktop
folder_location = r'C:\Users\{0}\desktop\Vodafone_Invoices'.format(username)
Sampleurl = ('https://www.tict.io')
loginurl =('https://www.tict.io/auth/login')
secure_url = ('https://www.tict.io/tool/87dd1218-f632-4ddb-b4d2-1f195bb4a5ca')
payload = {
'username': 'xxxx',
'password': 'xxx',
'ltfejs': 'xx'
}
with requests.session() as s:
print("Connecting to website")
s.post(loginurl, data=payload)
r = s.get(secure_url)
soup = BeautifulSoup(r.content, 'html.parser')
links = soup.find_all('a', href=re.compile(r'(.pdf)'))
print("Gathering .PDF files")
# clean the pdf link names
url_list = []
for el in links:
if(el['href'].startswith('https:')):
url_list.append(el['href'])
else:
url_list.append(Sampleurl + el['href'])
pprint(url_list)
print("Downloading .PDF files")
# download the pdfs to a specified location
for url in url_list:
print(url)
fullfilename = os.path.join(r'C:\Users\{0}\desktop\Vodafone_Invoices'.format(username), url.split("/")[-1])
if not os.path.exists(folder_location):os.mkdir(folder_location)
print(fullfilename)
request.urlretrieve(Sampleurl,fullfilename)
print("This program will automatically close in 5 seconds ")
time.sleep(5)
输出
Connecting to website
Gathering .PDF files
['https://www.tict.io/tool/87dd1218-f632-4ddb-b4d2-1f195bb4a5ca/quickscan.pdf',
'https://www.tict.io/tool/87dd1218-f632-4ddb-b4d2-1f195bb4a5ca/fullscan.pdf',
'https://www.tict.io/tool/87dd1218-f632-4ddb-b4d2-1f195bb4a5ca/improvementscan.pdf',
'https://www.tict.io/downloads/privacylabel.pdf']
Downloading .PDF files
https://www.tict.io/tool/87dd1218-f632-4ddb-b4d2-1f195bb4a5ca/quickscan.pdf
C:\Users\MATH\desktop\Vodafone_Invoices\quickscan.pdf
https://www.tict.io/tool/87dd1218-f632-4ddb-b4d2-1f195bb4a5ca/fullscan.pdf
C:\Users\MATH\desktop\Vodafone_Invoices\fullscan.pdf
https://www.tict.io/tool/87dd1218-f632-4ddb-b4d2-1f195bb4a5ca/improvementscan.pdf
C:\Users\MATH\desktop\Vodafone_Invoices\improvementscan.pdf
https://www.tict.io/downloads/privacylabel.pdf
C:\Users\MATH\desktop\Vodafone_Invoices\privacylabel.pdf
This program will automatically close in 5 seconds
当手动单击输出中的一个超链接时,它确实会下载一个有效的 .PDF。
编辑
我已经调整了我的代码,现在它确实将工作 PDF 下载到分配的文件夹中,但是它只获取列表中的最后一个文件,并且不会为其他文件重复循环
print("Downloading .PDF files")
# download the pdfs to a specified location
for PDF in url_list:
fullfilename = os.path.join(r'C:\Users\{0}\desktop\Vodafone_Invoices'.format(username), url.split("/")[-1])
if not os.path.exists(folder_location):os.mkdir(folder_location)
myfile = requests.get(PDF)
open(fullfilename, 'wb').write(myfile.content)
print("This program will automaticly close in 5 seconds ")
time.sleep(5)
仅 privacylabel.pdf(url_list 中的最后一个文件) 被下载。其他不会出现在文件夹中。 打印 PDF 时,它也只返回 privacylabel.pdf
【问题讨论】:
-
能否在PDF下载代码中添加
print(myfile),看看每次迭代返回什么Response? -
它打印出:我的文件响应: 我的文件响应: 我的文件响应: 我的文件响应: 我想200response 用于隐私标签,404 用于所有其他文件。这可能与secure_url上的隐私标签在登录之前也可以从网站上获得的事实有关。
-
是的,这可能是真的。
-
我也做了一个打印(PDF)并在下载代码中,它返回有效的超链接,当手动单击时下载文件,所以我不知道为什么它仍然会响应 404暗示找不到该文件。有什么我可以尝试的建议吗?
-
你可以试试
selenium
标签: python pdf beautifulsoup https python-requests