ruby - Test existance of a page using mechanize -
I want to check that a url exists before it is downloaded, I do it for
agent = Mechanize.New page = agent.get ("www.some_url.com/atributes")
but want to instantly install that URL Or
The only way to see if a page exists ( And that you can access through the internet To make a genuine request, you can first request an HTTP head request, which only requests the header, not the actual content:
url = "www.some_url.com / Atributes "agent = Start the form of the mechanize.New agent.head (Url) page_exists = true Rescue SocketError page_exists = false end if page_exists page = agent.get (url) # Do something with the page ... end
But again, you can simply get rid of additional requests and defend against errors directly with gate requests:
url = "www.s Ome_url.com/atributes "agent = Mechanize.New page = agent.get (url) # Do something with the page ... the rescue socket array puts" no such page. " End
Comments
Post a Comment