简体   繁体   English

使用'puts'从外部域获取信息

[英]using 'puts' to get information from external domain

ive just started with ruby on rails the other day and i was wandering is it possible to using the puts function to get the content of a div from a page on an external page. 我刚刚开始使用ruby on rails并且我在游荡是否可以使用puts函数从外部页面上的页面获取div的内容。

something like puts "http://www.example.com #about" puts "http://www.example.com #about"类的东西

would something like this work ? 会不会像这样的工作? or would you have to get the entire page and then puts that section that you wanted ? 或者你必须得到整个页面,然后把你想要的部分?

additionaly if the content on the "example.com" #about div is constantly changing would puts constantly update its output or would it only run the script each time the page is refreshed ? 另外,如果“example.com”#about div上的内容不断变化会不断更新其输出,还是每次刷新页面时都会运行脚本?

The open-uri library (for fetching the page) and the Nokogiri gem (for parsing and retrieving specific content) can assist with this. open-uri库(用于获取页面)和Nokogiri gem(用于解析和检索特定内容)可以帮助解决这个问题。

require 'open-uri'
require 'nokogiri'

doc = Nokogiri::HTML(open('http://www.example.com/'))

puts doc.at('#about').text

puts will not work that way. puts会不会这样。 Ruby makes parsing HTML fairly easy though. Ruby使得解析HTML相当容易。 Take a look at the Nokogirl library, and you can use xpath queries to get to the div you want to print out. 看一下Nokogirl库,您可以使用xpath查询来获取要打印的div。 I believe you would need to reopen the file if the div changes, but I'm not positive about that - you can easily test it (or someone here can confirm or reject that statement). 我相信你需要重新打开文件,如果div改变了,但我不是肯定的 - 你可以轻松地测试它(或者这里有人可以确认或拒绝该声明)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM