wget python .tmp 错误不适用于特定的网址(网络爬行)

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

你好,我在使用 wget 的 Python 中遇到一个奇怪的问题,如果有人能给我帮助,我将非常感激。

我想做什么:

使用 wget、Python 从特定网站(例如 wiki)下载文件('.pdf'、'.djvu')。这应该很容易。

the full target page

我正在尝试进行网络抓取的特定页面

getting the file link for wget

问题:

真的很奇怪。在网站的大多数页面上,它运行良好。

但是有些具有相同HTML结构的页面,它不起作用。

即使在同一页面,有些文件可以使用 wget 很好地下载,但有些则不行

并收到此错误消息


Error message :

`C:\start_automation\crawling_job>C:/Users/sa031/AppData/Local/Programs/Python/Python311/python.exe c:/start_automation/crawling_job/download_test.py
Traceback (most recent call last):
  File "c:\start_automation\crawling_job\download_test.py", line 39, in <module>
    wget.download(url)
  File "C:\Users\sa031\AppData\Local\Programs\Python\Python311\Lib\site-packages\wget.py", line 303, in download
    (fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=prefix, dir=".")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sa031\AppData\Local\Programs\Python\Python311\Lib\tempfile.py", line 341, in mkstemp
    return _mkstemp_inner(dir, prefix, suffix, flags, output_type)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\sa031\AppData\Local\Programs\Python\Python311\Lib\tempfile.py", line 256, in _mkstemp_inner
    fd = _os.open(file, flags, 0o600)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\start_automation\\crawling_job\\CADAL06210101_%E7%9A%87%E6%B8%85%E7%B6%93%E8%A7%A3%E7%BA%8C%E7%B7%A8%EF%BC%9A%E6%98%93%E5%9C%96%E6%A2%9D%E8%BE%AE%E7%9A%87%E6%B8%85%E7%B6%93%E8%A7%A3%E7%BA%8C%E7%B7%A8%EF%BC%9A%E8%99%9E%E6%B0%8F%E6%98%93%E4%BA%8B.djvu.3kii8ipd.tmp'`

我做了什么:

google 搜索,在 wiki 中的几个不同页面进行了测试。

询问chatGPT并获取带有绝对路径的代码,但不起作用

import os
import wget

def download_file(url, save_path):
    try:
        print("Downloading file...")
        wget.download(url, save_path)
        print("\nDownload complete!")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    # URL of the file to download
    file_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/CADAL06210101_%E7%9A%87%E6%B8%85%E7%B6%93%E8%A7%A3%E7%B6%8C%E7%B7%A8%EF%BC%9A%E6%98%93%E5%9C%96%E6%A2%9D%E8%BE%AE%E7%9A%87%E6%B8%85%E7%B6%93%E8%A7%A3%E7%B6%8C%E7%B7%A8%EF%BC%9A%E8%99%9E%E6%B0%8F%E6%98%93%E4%BA%8B.djvu"
    
    # Specify an absolute path for saving the file
    save_location = os.path.join(os.getcwd(), "downloaded_file.djvu")
    
    # Call the function to download the file
    download_file(file_url, save_location)

代码:

下面的代码是包含 URL 的代码,不起作用。

import wget

url='https://upload.wikimedia.org/wikipedia/commons/a/a7/CADAL06210101_%E7%9A%87%E6%B8%85%E7%B6%93%E8%A7%A3%E7%BA%8C%E7%B7%A8%EF%BC%9A%E6%98%93%E5%9C%96%E6%A2%9D%E8%BE%AE%E7%9A%87%E6%B8%85%E7%B6%93%E8%A7%A3%E7%BA%8C%E7%B7%A8%EF%BC%9A%E8%99%9E%E6%B0%8F%E6%98%93%E4%BA%8B.djvu'

wget.download(url)

也许:

.djvu.3kii8ipd.tmp'

错误消息中显示这个奇怪的 .tmp 名称,但不知道有问题。

感谢您的阅读。非常感谢您的帮助。

python directory wget filenotfounderror
1个回答
0
投票

wget 似乎需要一个临时位置,原因我不明白。 wget 上次更新是在 9 年前,可能不再强大。

您可以通过 requests 可靠且轻松地实现此目标,如下所示:

import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15"
}

url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/CADAL06210101_%E7%9A%87%E6%B8%85%E7%B6%93%E8%A7%A3%E7%BA%8C%E7%B7%A8%EF%BC%9A%E6%98%93%E5%9C%96%E6%A2%9D%E8%BE%AE%E7%9A%87%E6%B8%85%E7%B6%93%E8%A7%A3%E7%BA%8C%E7%B7%A8%EF%BC%9A%E8%99%9E%E6%B0%8F%E6%98%93%E4%BA%8B.djvu"
output_file = "downloaded_file.djvu"
chunk = 4096 # usually a good chunk size

with requests.get(url, headers=headers, stream=True) as response:
    response.raise_for_status()
    with open(output_file, "wb") as output:
        for chunk in response.iter_content(chunk):
            output.write(chunk)
© www.soinside.com 2019 - 2024. All rights reserved.