qq1151985918 发表于 2021-1-20 16:25:37

问一下我这种情况下载的文件怎样命名?

问一下我这种情况下载的文件怎样命名?
文件名有时是 doc 有时是 ppt 有时是 pdf
文件名不在网址中
浏览器下载的话会自动生成文件名
python怎样也用它自动生成的文件名?http://www.wdfxw.net/goDownFiles.aspx?key=28541108
就像这个下载地址

逃兵 发表于 2021-1-20 17:09:11

不知道webdriver是否满足你
from selenium import webdriver

wd = webdriver.Chrome(r"C:\Users\admin\AppData\Local\Programs\Python\Python38-32\Scripts\chromedriver.exe")

wd.implicitly_wait(60)

wd.maximize_window()


url = 'http://www.wdfxw.net/goDownFiles.aspx?key=28541108'

urllst =

for i in urllst:
    wd.get(url)

Cool_Breeze 发表于 2021-1-20 17:44:03

# coding=utf-8

import urllib
import urllib.request



def download(url, savePath=None):
    res = urllib.request.urlopen(url)
    name = urllib.parse.unquote(res.getheader('Content-Disposition').split('='))
    with open(name, 'wb') as f:
      f.write(res.read())

url = 'http://www.wdfxw.net/goDownFiles.aspx?key=28541108'
download(url)

qq1151985918 发表于 2021-1-20 18:54:25

本帖最后由 qq1151985918 于 2021-5-17 09:20 编辑

Cool_Breeze 发表于 2021-1-20 17:44


用 requests 模块实现

# coding=utf-8

import requests

def download(url, savePath = None):      
    res = requests.get(url)
    name = requests.utils.unquote(res.headers['Content-Disposition'].split('='))
    with open(name, 'wb') as f:
      f.write(res.content)

url = 'http://www.wdfxw.net/goDownFiles.aspx?key=28541108'
download(url)

Cool_Breeze 发表于 2021-1-20 19:18:25

qq1151985918 发表于 2021-1-20 18:54
大佬,我刚试过了确实可以,但是我想问下能用 requests 模块实现吗?

这个得你自己测试,我没有使用过requests 模块

Cool_Breeze 发表于 2021-1-20 19:21:09

qq1151985918 发表于 2021-1-20 18:54
大佬,我刚试过了确实可以,但是我想问下能用 requests 模块实现吗?

可以, 我刚刚看了

Cool_Breeze 发表于 2021-1-20 19:26:47

# coding=utf-8

import urllib
import requests

def download(url, savePath=None):
    res = requests.get(url)
    name = urllib.parse.unquote(res.headers['Content-Disposition'].split('='))
    with open(name, 'wb') as f:
      f.write(res.content)

url = 'http://www.wdfxw.net/goDownFiles.aspx?key=28541108'
download(url)
页: [1]
查看完整版本: 问一下我这种情况下载的文件怎样命名?