Python 每次文件名更改时从Web下载文件
我的任务是每天从本地下载站点下载McAfee病毒定义文件。文件名每天都在更改。下载站点的路径是 这个ID7612每天都会改变,所以我不能硬编码。在将文件名作为参数提供之前,我必须找到一种方法来列出文件名。 在stackoverflow网站上,我找到了一些人的脚本,如果有人能建议如何处理文件名的更改,这些脚本将对我有用。 下面是我将要使用的脚本:Python 每次文件名更改时从Web下载文件,python,Python,我的任务是每天从本地下载站点下载McAfee病毒定义文件。文件名每天都在更改。下载站点的路径是 这个ID7612每天都会改变,所以我不能硬编码。在将文件名作为参数提供之前,我必须找到一种方法来列出文件名。 在stackoverflow网站上,我找到了一些人的脚本,如果有人能建议如何处理文件名的更改,这些脚本将对我有用。 下面是我将要使用的脚本: def download(url): """Copy the contents of a file from a given URL t
def download(url):
"""Copy the contents of a file from a given URL
to a local file.
"""
import urllib
webFile = urllib.urlopen(url)
localFile = open(url.split('/')[-1], 'w')
localFile.write(webFile.read())
webFile.close()
localFile.close()
if __name__ == '__main__':
import sys
if len(sys.argv) == 2:
try:
download(sys.argv[1])
except IOError:
print 'Filename not found.'
else:
import os
print 'usage: %s http://server.com/path/to/filename' % os.path.basename(sys.argv[0])
有人能给我提个建议吗?
提前感谢这是一个分两步的过程。首先,在索引页上搜索文件。第二,抓取最新信息并下载
import urllib
import lxml.html
import os
import shutil
# index page
pattern_files_url = "http://download.nai.com/products/licensed/superdat/english/intel"
# relative url references based here
pattern_files_base = '/'.join(pattern_files_url.split('/')[:-1])
# scrape the index page for latest file list
doc = lxml.html.parse(pattern_files_url)
pattern_files = [ref for ref in doc.xpath("//a/@href") if ref.endswith('xdat.exe')]
if pattern_files:
pattern_files.sort()
newest = pattern_files[-1]
local_name = newest.split('/')[-1]
# grab it if we don't already have it
if not os.path.exists(local_name):
url = pattern_files_base + '/' + newest
print("downloading %s to %s" % (url, local_name))
remote = urllib.urlopen(url)
print dir(remote)
with open(local_name, 'w') as local:
shutil.copyfileobj(remote, local, length=65536)
文件名不是按顺序排列的吗?只需点击此页面,查找exe扩展名并下载itI,我不知道文件名是否按顺序排列-Hackaholic,我喜欢浏览网页的想法,我可以找到最新的并下载。问题是我不知道如何使用python列出该页面的内容,你能给我一些建议吗?谢谢,实际上我正在寻找的是所谓的DAT包安装程序或“XDAT文件”,有人知道我可以从哪里下载吗?我计划用一个脚本来做这件事。提前感谢注意:我以前在这个位置尝试过的方法无效:Index of/140410/products/licensed/superdat/english/intel tdelany,非常感谢您的帮助。这对我来说非常有效。谢谢