下载多个PDF时出现问题

拉赫尔·米兹(Rahel Miz)

运行以下代码后,我无法打开下载的PDF。即使代码成功运行,下载的PDF文件也已损坏。

我的计算机的错误消息是

无法打开文件。它可能已损坏或预览无法识别的格式。

为什么它们损坏了,我该如何解决?

import os
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup

url = "https://github.com/sonhuytran/MIT8.01SC.2010F/tree/master/References/University%20Physics%20with%20Modern%20Physics%2C%2013th%20Edition%20Solutions%20Manual"

#If there is no such folder, the script will create one automatically
folder_location = r'/Users/rahelmizrahi/Desktop/ Physics_Solutions'
if not os.path.exists(folder_location):os.mkdir(folder_location)

response = requests.get(url)
soup= BeautifulSoup(response.text, "html.parser")     
for link in soup.select("a[href$='.pdf']"):

    filename = os.path.join(folder_location,link['href'].split('/')[-1])
    with open(filename, 'wb') as f:
        f.write(requests.get(urljoin(url,link['href'])).content) 
chitown88

这个问题是'blob'当您需要链接时,您正在请求github中的'raw'链接:

'/sonhuytran/MIT8.01SC.2010F/blob/master/References/University%20Physics%20with%20Modern%20Physics%2C%2013th%20Edition%20Solutions%20Manual/A01_YOUN6656_09_ISM_FM.pdf'

但您想要:

'/sonhuytran/MIT8.01SC.2010F/raw/master/References/University%20Physics%20with%20Modern%20Physics%2C%2013th%20Edition%20Solutions%20Manual/A01_YOUN6656_09_ISM_FM.pdf'

因此,只需进行调整即可。完整代码如下:

import os
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup

url = "https://github.com/sonhuytran/MIT8.01SC.2010F/tree/master/References/University%20Physics%20with%20Modern%20Physics%2C%2013th%20Edition%20Solutions%20Manual"

#If there is no such folder, the script will create one automatically
folder_location = r'/Users/rahelmizrahi/Desktop/Physics_Solutions'
if not os.path.exists(folder_location):os.mkdir(folder_location)

response = requests.get(url)
soup= BeautifulSoup(response.text, "html.parser")     
for link in soup.select("a[href$='.pdf']"):
    pdf_link = link['href'].replace('blob','raw')
    pdf_file = requests.get('https://github.com' + pdf_link)
    filename = os.path.join(folder_location,link['href'].split('/')[-1])
    with open(filename, 'wb') as f:
        f.write(pdf_file.content)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从Web API服务下载pdf文件时出现问题

来自分类Dev

从Web API服务下载pdf文件时出现问题

来自分类Dev

下载网页时出现问题

来自分类Dev

下载python 3时出现问题

来自分类Dev

使用Sails js显示和下载PDF文件时出现问题

来自分类Dev

从 Excel 生成 PDF 时出现问题

来自分类Dev

输出多个值时出现问题

来自分类Dev

使用多个列表时出现问题

来自分类Dev

在R中使用gridExtra制作多个PDF页面时出现问题

来自分类Dev

下载图像和更新listView时出现问题

来自分类Dev

使用内容处置下载文件时出现问题

来自分类Dev

在iOS中下载JSON对象时出现问题

来自分类Dev

下载zip文件ASP MVC时出现问题

来自分类Dev

使用Curl下载HTML进行解析时出现问题

来自分类Dev

从Firebase存储获取下载URL时出现问题

来自分类Dev

下载文件时出现问题(java)

来自分类Dev

从Blob agnular打印pdf文件时出现问题

来自分类Dev

使用TCPDF保存PDF文件时出现问题

来自分类Dev

使用铬打印到PDF时出现问题

来自分类Dev

在Visual Studio上使用多个面板时出现问题

来自分类Dev

上载多个相同名称的文件时出现问题

来自分类Dev

在Visual Studio上使用多个面板时出现问题

来自分类Dev

在单个页面上绘制多个图形时出现问题

来自分类Dev

远程访问多个Docker容器时出现问题

来自分类Dev

使用pthreads创建多个线程时出现问题

来自分类Dev

复制多个文件夹时出现问题

来自分类Dev

OleDbConnection ConnectionString,打开多个文件时出现问题

来自分类Dev

使用AFNetworking下载多个文件时出现内存压力问题

来自分类Dev

使用JavaScript选择选项时出现多个文本框时出现问题

Related 相关文章

热门标签

归档