我使用ruby watir编写了一个脚本来下载多个excel文件
剧本对我来说很好。 现在,我一次下载500个文件。 我想通过使用ruby watir指定一些范围来下载文件,比如一次指定(10-30)个文件,另一次指定(30-60)个文件等等。 这是我的代码:我使用ruby watir编写了一个脚本来下载多个excel文件,ruby,watir-webdriver,Ruby,Watir Webdriver,剧本对我来说很好。 现在,我一次下载500个文件。 我想通过使用ruby watir指定一些范围来下载文件,比如一次指定(10-30)个文件,另一次指定(30-60)个文件等等。 这是我的代码: require 'watir' require 'rubygems' begin chromedriver_path = File.join(File.absolute_path(File.dirname(__FILE__)),"browser","chromedriver.exe") Sel
require 'watir'
require 'rubygems'
begin
chromedriver_path = File.join(File.absolute_path(File.dirname(__FILE__)),"browser","chromedriver.exe")
Selenium::WebDriver::Chrome.driver_path = chromedriver_path
browser = Watir::Browser.new:chrome
browser.goto "" //url to login
sleep 3
browser.text_field(:name=>"").set "" #e_id
sleep 3
browser.text_field(:name=>"").set "" #pwd
browser.button(:value=>"Login").click #submit
browser.div(:id=>"DivMenu").click
#sleep 3
browser.span(:class =>"down").click
sleep 3
browser.execute_script("document.getElementById('hlGenerateStatusReports').click();")
sleep 3
browser.execute_script("document.getElementById('Report').click();")
sleep 3
optncount = browser.select_list(:id => 'head_ddlClient').options.count
puts optncount
i = 0
while i <= optncount do
puts "Inside the loop i = "+i.to_s
i +=1
browser.select_list(:id => 'ddlClient').option(:index => i).select
sleep 3
browser.button(:value=>"Generate Report").click #submit
sleep 10
end
browser.goto " " //url to logout
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
需要“watir”
需要“rubygems”
开始
chromedriver\u path=File.join(File.absolute\u path(File.dirname(\uuuuu File\uuuuu)),“browser”,“chromedriver.exe”)
Selenium::WebDriver::Chrome.driver\u path=chromedriver\u path
browser=Watir::browser.new:chrome
browser.goto”“//登录的url
睡眠3
browser.text_字段(:name=>“”)。设置“”#e_id
睡眠3
browser.text_字段(:name=>“”)。设置“”#pwd
浏览器。按钮(:value=>“登录”)。单击#提交
browser.div(:id=>“DivMenu”)。单击
#睡眠3
browser.span(:class=>“down”)。单击
睡眠3
browser.execute_脚本(“document.getElementById('hlGenerateStatusReports')。单击();”)
睡眠3
browser.execute_脚本(“document.getElementById('Report')。单击();”)
睡眠3
optCount=browser。选择_列表(:id=>'head_ddlClient')。options.count
放置OptCount
i=0
而i'ddlClient')。选项(:index=>i)。选择
睡眠3
browser.按钮(:value=>“生成报告”)。单击#提交
睡眠10
结束
browser.goto”“//要注销的url
救援异常=>e
放电子邮件
放e.backtrace.inspect
结束
我还有图像等的下载脚本。
我在这里用来演示该技术的一小部分是针对500px.com的
我将所有下载的文件保存在一个文本文件中,并检查allready是否根据此文件下载。这样你就可以随时中断,以后再继续。
当然,如果下载的达到限制,您可以中断
我不发表整个剧本,只是关于你的问题
def download url
filename = "#{url[-32..-1]}.jpg"
if get(url, filename, SAVE_FOLDER)
File.open(PROGRESS_FILE,'a+'){|f|f.puts filename}
end
end
PROGRESS_FILE = './500px.txt'
downloaded = 0
....
response = http.get(path, headers)
json = JSON.parse(response.body)["data"]
processed = File.read(PROGRESS_FILE)
json.each do |item|
url = item['images'].last['url']
signature = url[-32..-1]
filename = "#{signature}.jpg"
# check if the filenames is in the textfile and so was downloaded allready
unless processed[filename]
download url
downloaded += 1
end
end
到目前为止,该文件有5亿多行,运行速度足够快(下载时间要长得多)。如果我达到了一个极限,我可以很容易地将这些行放在一个简单的数据库中,比如Sqlite。听起来很合理。我们能提供什么帮助?我需要一个帮助来使用ruby watir脚本下载特定范围的文件。有帮助吗?请使用循环:(10..30)。每个{I | download_file(I)}
嗨,stefan,都有机会在命令提示符下传递参数。在执行ruby文件时。@stefan我们可能会钦佩和自豪,不是吗?