从网站下载所有pdf文件不支持通配符

问题描述 投票:0回答:1

我想下载“https://journals.ametsoc.org/view/journals/mwre/131/5/mwre.131.issue-5.xml”网站中的所有pdf文件。 我用 wget 尝试了很多事情: wget --wait 10 --random-wait --continue https://journals.ametsoc.org/downloadpdf/view/journals/mwre/131/5/1520-0493_2003_131_*.co_2.pdf 但我收到这条消息: 警告:HTTP 不支持通配符。 --2024-03-29 23:01:27-- https://journals.ametsoc.org/downloadpdf/view/journals/mwre/131/5/1520-0493_2003_131_*.co_2.pdf 正在解析 Journals.ametsoc.org (journals.ametsoc.org)... 54.73.220.207, 52.208.161.60 正在连接journals.ametsoc.org (journals.ametsoc.org)|54.73.220.207|:443...已连接。 HTTP 请求已发送,正在等待响应... 500 2024-03-29 23:01:28 错误 500:(无描述)。

有没有办法使用 wget、python 或任何工具来做到这一点? 预先感谢您。

python pdf download wget
1个回答
0
投票

据我所知,你想从 html 页面进行抓取,所以它不会像文件管理器那样工作。您需要使用Python的Beautifulsoap或Lxml库。下面的代码是使用 lxml 库编写的,它应该可以完成您想要的操作...它将把 pdf 保存到执行代码的文件夹中。

import requests
from lxml import html

headers = {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0'
            }
url="https://journals.ametsoc.org/view/journals/mwre/131/5/mwre.131.issue-5.xml"
response=requests.get(url, headers=headers)
page = html.fromstring(response.text)
url_list = page.xpath("//h1/a[@class='c-Button--link']/@href")

for url in url_list:        
    url_half = url.replace('.xml','.pdf')
    url_base = "https://journals.ametsoc.org/downloadpdf"
    url_pdf= url_base+url_half
    print(url_pdf)
    response = requests.get(url_pdf, headers=headers)
    if response.headers.get('content-type') == 'application/pdf':
        # Write the content to a PDF file
        with open(filename, 'wb') as file:
            file.write(response.content)
        print("PDF file downloaded successfully!")
    else:
        print("The response does not contain a PDF file.")
© www.soinside.com 2019 - 2024. All rights reserved.