gpt4 book ai didi

ruby - 使用 ruby​​ 从网站检索文档

转载 作者:行者123 更新时间:2023-12-04 16:19:04 24 4
gpt4 key购买 nike

我用 ruby​​ 编写了一个脚本,它可以浏览网站并进入表单页面。填写表单页面后,脚本会点击提交按钮,然后会打开一个对话框,询问您将其保存在哪里。我在尝试获取此文件时遇到问题。我在网上搜索过,找不到任何东西。我将如何检索文档的文件名?

如果有人可以帮助我,我将不胜感激

我的代码如下:

browser = Mechanize.new

## CONSTANTS
LOGIN_URL = 'https://business.airtricity.com/ews/welcome.jsp'
HOME_PAGE_URL = 'https://business.airtricity.com/ews/welcome.jsp'
CONSUMPTION_REPORT_URL = 'https://business.airtricity.com/ews/touConsChart.jsp?custid=209495'
LOGIN = ""
PASS = ""
MPRN_GPRN_LCIS = "10000001534"
CONSUMPTION_DATE = "20/01/2013"
END_DATE = "27/01/2013"
DOWNLOAD = "DL"



### Login page
begin
login_page = browser.get(LOGIN_URL)
rescue Mechanize::ResponseCodeError => exception
login_page = exception.page
end

puts "+++++++++"
puts login_page.links
puts "+++++++++"

login_form = login_page.forms.first
login_form['userid'] = LOGIN
login_form['password'] = PASS
login_form['_login_form_'] = "yes"
login_form['ipAddress'] = "137.43.154.176"
login_form.submit



## home page
begin
home_page = browser.get(HOME_PAGE_URL)
rescue Mechanize::ResponseCodeError => exception
home_page = exception.page
end

puts "----------"
puts home_page.links
puts "----------"


# Consumption Report
begin
Report_Page = browser.get(CONSUMPTION_REPORT_URL)
rescue Mechanize::ResponseCodeError => exception
Report_Page = exception.page
end

puts "**********"
puts Report_Page.links
pp Report_Page
puts "**********"

Report_Form = Report_Page.forms.first
Report_Form['entity1'] = MPRN_GPRN_LCIS
Report_Form['start'] = CONSUMPTION_DATE
Report_Form['end'] = END_DATE
Report_Form['charttype'] = DOWNLOAD
Report_Form.submit

## Download Report

begin
browser.pluggable_parser.csv = Mechanize::Download
Download_Page = browser.get('https://business.airtricity.com/ews/touConsChart.jsp?custid=209495/meter_read_download_2013-1-20_2013-1-27.csv').save('Hello')
rescue Mechanize::ResponseCodeError => exception
Download_Page = exception.page
end

最佳答案

http://mechanize.rubyforge.org/Mechanize.html#method-i-get_file
从 url 下载文件使用 Mechanize 非常简单:

browser = Mechanize.new
file_url = 'https://raw.github.com/ragsagar/ragsagar.github.com/c5caa502f8dec9d5e3738feb83d86e9f7561bd5e/.html'
downloaded_file = browser.get_file file_url
File.open('new_file.txt', 'w') { |file| file.write downloaded_file }

关于ruby - 使用 ruby​​ 从网站检索文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14585843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com