Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to download all the links in a webpage using a ruby script. My idea was to extract the href links if I could get the Source code, set a download location using command prompt. Is there a way for this ?
#!/usr/bin/env ruby
#
require "net/http"
require "uri"
#past your link
gets = STDIN.gets rescue nil
url = URI.parse gets
http = Net::HTTP.start(url.host, url.port) do |resp|
puts resp.get(url.request_uri).body
end
#your regular for href
or use http://nokogiri.org/
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How do I know if the response for a url is 200 or 404 and how do I check the availability of the url?
You can make a GET request using Net::HTTP:
uri = URI('http://example.com/index.html')
res = Net::HTTP.get_response(uri)
puts res.code
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have simple HTML form, which I got from a web page:
<form id="my">
inputs....
</form>
I need to get this form via it's ID, which I know how to do:
#get_doc = Nokogiri::HTML(page)
nb = #get_doc.at_css('#my')
maybe could i iterate via object ?
I need to get all the input values and input names into some variable, and then pass it to URI.encode_www_form.
How can I do this? How could I get all the inputs inside the form with names and values, and pass them to encode_www_form?
arr = []
# form = doc.at_css '#form'
form.css('input').each do |i|
arr << [i['name'], i['value']]
end
URI.encode_www_form arr
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am running an automated test script written in Ruby in which I get a result page, I would like to capture some of the text on the page and print them in a file. Can anyone assist with this effort?
Like the comments say, use Nokogiri.
Install it with gem install nokogiri.
To print the first top-level heading from example.com:
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open("http://www.example.com/"))
puts doc.css("h1").first
For more information on finding the text you want, try this guide
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is ruby code:
[:val1, :val2, :val3].each do |method_name|
define_method(met_n) do |param1, param2|
inflect(param1, param2, SOME_CONST[met_n.to_s])
end
end
It's not mine code. I tried to figure out what inflect is, but I failed, although it should be a standard ruby function.
So how is it defined or where do I find a documentation about it?
inflect isn't a standard ruby function. This must be part of someone else's API.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I have a paragraph of #text and I want to extract the #twitter handles from it and make them into a list, how would I do this?
Use String#scan
Use the String#scan method to extract your expression into an array. For example:
str = <<'EOF'
If I have a paragraph of #text and I want to extract the
#twitter handles from it and make them into a list, how
would I do this?
EOF
str.scan /#\S+/
#=> ["#text", "#twitter"]
Assuming your #text is a string, use regex to extract #twitter handles and add them into an array.