![](/img/trans.png)
[英]can i execute a definition written in ruby watir in an excel sheet?
[英]I wrote a script to download a multiple excel files using ruby watir
腳本對我來說很好用。 現在,Iam一次下載500個文件。 我想通過一次指定一些范圍來下載文件,例如一次使用(10-30)文件,另一次指定(30-60)文件,如此使用ruby watir。 這些是我的代碼:
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
我也有圖像等的下載腳本。我在此處用來演示該技術的一小部分用於500px.com。
我將所有下載的文件保留在一個文本文件中,並檢查是否已針對該文件全部下載。 這樣,您可以隨時中斷並稍后恢復。 當然,如果downloaded
達到極限,您可能會中途休息。
我不會發布整個腳本,而只是發布與您的問題有關的內容。
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這樣的簡單數據庫中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.