如何使用ruby下载多个动态文件?(How do I download multiple dynamic files using ruby? [closed])
我想从网站下载带有ruby的多个动态文件。 有五个不同的文件,它们都有一个基本结构:
backup_YYYY - MM - DD - HHMM _JRAddOns_Die_Pflegeserie_fr_ ID - CONTENT 。 结尾
所有大胆的书面部分都是动态的,必须更换。
文件可能如下所示:
backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-db.gz backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-others.zip backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-plugins.zip backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-themes.zip backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-uploads.zip
有人可以帮忙吗?
编辑:我尝试了一个不会长时间工作的非动态版本,但是:
puts 'Starting Download...' require 'net/http' Net::HTTP.start("jr-addons.de") do |http| resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-db.gz") open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-db.gz", "wb") do |file| file.write(resp.body) end resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-others.zip") open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-others.zip", "wb") do |file| file.write(resp.body) end resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-plugins.zip") open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-plugins.zip", "wb") do |file| file.write(resp.body) end resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-themes.zip") open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-themes.zip", "wb") do |file| file.write(resp.body) end resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-uploads.zip") open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-uploads.zip", "wb") do |file| file.write(resp.body) end end puts 'Done.'
I want to download multiple dynamic files with ruby from a website. There are five different files and they all have a basic structure:
backup_YYYY-MM-DD-HHMM_JRAddOns_Die_Pflegeserie_fr_ID-CONTENT.ENDING
All the bold written parts are dynamic and have to be replaced.
The files could look like this:
backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-db.gz backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-others.zip backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-plugins.zip backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-themes.zip backup_2017-03-06-1020_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-uploads.zip
Could someone help?
Edit: I tried a non-dynamic version of it which wont work long, though:
puts 'Starting Download...' require 'net/http' Net::HTTP.start("jr-addons.de") do |http| resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-db.gz") open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-db.gz", "wb") do |file| file.write(resp.body) end resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-others.zip") open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-others.zip", "wb") do |file| file.write(resp.body) end resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-plugins.zip") open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-plugins.zip", "wb") do |file| file.write(resp.body) end resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-themes.zip") open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-themes.zip", "wb") do |file| file.write(resp.body) end resp = http.get("/btemp/backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-uploads.zip") open("backup_2016-10-23-1520_JRAddOns_Die_Pflegeserie_fr_2867ed5aba3d-uploads.zip", "wb") do |file| file.write(resp.body) end end puts 'Done.'
原文:https://stackoverflow.com/questions/42621888
最满意答案
解决方案取决于您拥有的编辑器,编辑器可能找到的环境变量以及编辑器是否采用任何命令行参数。
这是一个简单的解决方案,适用于Windows,没有任何环境变量或编辑器的命令行参数。 根据需要进行修改。
import subprocess import os.path def start_editor(editor,file_name): if not os.path.isfile(file_name): # If file doesn't exist, create it with open(file_name,'w'): pass command_line=editor+' '+file_name # Add any desired command line args p = subprocess.Popen(command_line) p.wait() file_name='test.txt' # Probably known from elsewhere editor='notepad.exe' # Read from environment variable if desired start_editor(editor,file_name) with open(file_name,'r') as f: # Do something with the file, just an example here for line in f: print line
The solution will depend on what editor you have, which environment variable the editor might possibly be found in and if the editor takes any command line parameters.
This is a simple solution that works on windows without any environment variables or command line arguments to the editor. Modify as is needed.
import subprocess import os.path def start_editor(editor,file_name): if not os.path.isfile(file_name): # If file doesn't exist, create it with open(file_name,'w'): pass command_line=editor+' '+file_name # Add any desired command line args p = subprocess.Popen(command_line) p.wait() file_name='test.txt' # Probably known from elsewhere editor='notepad.exe' # Read from environment variable if desired start_editor(editor,file_name) with open(file_name,'r') as f: # Do something with the file, just an example here for line in f: print line
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
一般来说,我会确保该关联没有被当前用户的设置覆盖: HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice HKCU\Software\Classes\.py HKCU\Software\Classes\Python.File 但是,当我在更新中读取进程监视器输出时,似乎没有任何这些HKCU密钥在您的系统上定义。 我的下一步是在高架cmd控制台中运行以下步骤: > assoc .PY=Python.Fil ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
通过在.gitconfig中使用editor = open -W -n 假设Textedit是Mac上的默认编辑器,则需要在.gitconfig中针对编辑器指定“打开-W -n” 在执行git commit时,该文件将自动在Textedit中打开 [user] name = My Name email = MyEmail@host.com [core] autocrlf = input safecrlf = true editor = open -W -n By h ...
-
有很多方法。 尝试: vi
, pico , Open /Applications/TextEdit.app 。 There are plenty of ways. Try: vi , pico, Open /Applications/TextEdit.app . -
如何从git这样的命令行程序中调用文本编辑器?(How do I invoke a text editor from a command line program like git does?)[2023-08-09]
解决方案取决于您拥有的编辑器,编辑器可能找到的环境变量以及编辑器是否采用任何命令行参数。 这是一个简单的解决方案,适用于Windows,没有任何环境变量或编辑器的命令行参数。 根据需要进行修改。 import subprocess import os.path def start_editor(editor,file_name): if not os.path.isfile(file_name): # If file doesn't exist, create it with o ... -
以下内容对您有用: git config --global core.editor "'C:/Program Files/Sublime Text 2/sublime_text.exe' -n -w" 您的别名起作用的原因是它被shell扩展: alias sublime="C:/Program\ Files/Sublime\ Text\ 2/sublime_text.exe" sublime -> C:/Program\ Files/Sublime\ Text\ 2/sublime_text.exe - ...
-
只需在任何地方解压缩最新的git for windows发布便携式存档(如PortableGit-2.7.2-64-bit.7z.exe )并添加到PATH中: c:\path\to\git;c:\path\to\git\bin;c:\path\to\git\usr\bin;c:\path\to\git\mingw64\bin 您将获得git-bash.exe (最近基于msys2的4.3+ bash),以及常规CMD shell中的200多个unix命令。 Simply uncompress the ...
-
CLI:实现类似git commit(打开文本编辑器并获取值)(CLI: implement something like git commit (open a text editor and get value))[2022-06-15]
通常,您做四件事: 创建一个临时文件 fork一个外部进程,它执行环境变量$EDITOR指定的程序,将临时文件的名称作为参数。 等待进程返回。 打开并从临时文件中读取以查看用户编写的内容。 Typically, you do four things: Create a temporary file Fork an external process which execs the program specified in the environment variable $EDITOR, giving the ... -
尝试这个: git config --global core.editor "'c:/program files/sublime text 3/subl.exe' -w" 这对我git commit (即在执行git commit打开了Sublime Text窗口,我可以在其中输入提交消息,在保存和关闭窗口之后,我检查了这个提交有我刚刚输入的提交消息在Windows 10上使用Git for Windows版本2.10.2和Sublime Text 3 build 3126进行git show 请注意,Wi ...