为什么nokogiri在服务器繁忙时等待几秒钟(3-5)并且我逐个请求页面,但是当这些请求处于循环中时,nokogiri不会等待并抛出超时消息.我正在使用超时阻止包裹请求,但nokogiri根本不等待那个时间.有关此的任何建议程序?
# this is a method from the eng class
def get_page(url,page_type)
begin
timeout(10) do
# Get a Nokogiri::HTML::Document for the page we’re interested in...
@@doc = Nokogiri::HTML(open(url))
end
rescue Timeout::Error
puts "Time out connection request"
raise
end
end
# this is a snippet from the main app calling eng class
# receives a hash with urls and goes throgh asking one by one
def retrieve_in_loop(links)
(0..links.length).each do |idx|
url = links[idx]
puts "Visiting link #{idx} of #{links.length}"
puts "link: …Run Code Online (Sandbox Code Playgroud) 我正在编写一个 rake 任务,它的功能是从其他网页获取信息。为此,我使用 open-uri 和 nokogiri。我已经在开发中进行了测试并且它可以完成工作,但是随后我部署到生产服务器并失败了。
这是代码:
require 'open-uri'
require 'nokogiri'
desc 'recover eventos llerena'
task recover_eventos_llerena: :environment do
enlaces = []
# Getting index and all links
url = open(URI.parse("https://llerena.org/eventos/lista"))
page = Nokogiri::HTML(url)
page.css("a.fusion-read-more").each do |line|
enlaces.push(line.attr('href'))
end
enlaces = enlaces.uniq
#Inspecting everyone of them
enlaces.each do |link|
url = open(URI.parse(link))
page = Nokogiri::HTML(url)
title = page.css("h1").text
if Evento.find_by_titulo(title) == nil
description = page.css(".tribe-events-single-event-description.tribe-events-content.entry-content.description p").text
date = page.css(".tribe-events-abbr").attr('title')
image = page.css(".size-full").attr('src')
Evento.create!(
titulo: title,
remote_imgevento_url: image,
info: description,
fecha: …Run Code Online (Sandbox Code Playgroud) 所以我在下面有一些基本代码,它从http://www.highcharts.com/demo/获取json .但我希望能够提取哈希值,更具体地说:
series: [{
name: 'Tokyo',
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}, {
name: 'New York',
data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
}, {
name: 'Berlin',
data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
}, {
name: 'London',
data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
}]
});
Run Code Online (Sandbox Code Playgroud)
进入哈希,以便我可以访问不同的数据点.目前,该脚本只是吐出一切.代码如下:
require "json"
require "open-uri" …Run Code Online (Sandbox Code Playgroud) 我一直在使用下面的代码来抓取一个网站,但我想我可能已经抓了太多,并且完全被禁止访问该网站.在,我仍然可以在我的浏览器上访问该站点,但任何涉及open-uri和此站点的代码都会引发503站点不可用错误.我认为这是特定于网站的,因为open-uri仍然适用于google和facebook.这有解决方法吗?
require 'rubygems'
require 'hpricot'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open("http://www.quora.com/What-is-the-best-way-to-get-ove$
topic = doc.at('span a.topic_name span').content
puts topic
Run Code Online (Sandbox Code Playgroud) 我正在使用Ruby和Nokogiri进行数据抓取。是否可以下载并解析计算机中的本地文件?
我有:
require 'open-uri'
url = "file:///home/nav/Desktop/Scraping/scrap1.html"
Run Code Online (Sandbox Code Playgroud)
它给出错误为:
No such file or directory @ rb_sysopen - file:\home/nav/Desktop/Scraping/scrap1.html
Run Code Online (Sandbox Code Playgroud) 所以我有这个问题,我要使用Open-URI列出Excel中列表中的每个国家/地区.一切都运作正常,但我似乎无法想象如何让我的RegExp-"字符串"包括单个命名的国家(如"瑞典"),但也有像南非这样的国家,用空白等分开.我希望我我已经公平地理解了自己,下面我将包括相关的代码片段.
我要匹配的文本如下(例如):
<a href="wf.html">Wallis and Futuna</a>
<a href="ym.html">Yemen</a>
Run Code Online (Sandbox Code Playgroud)
我目前坚持使用这个正则表达式:
/a.+="\w{2}.html">(\w*)<.+{1}/
Run Code Online (Sandbox Code Playgroud)
如你所见,匹配'也门'没有问题.虽然我仍然希望代码能够匹配"瓦利斯和富图纳与也门.也许如果有一种方法可以将所有内容都包含在给定的"> blabla bla <"中?有什么想法吗?我将非常感激!
我正在尝试使用OpenUri和Nokogiri从HTML源获取样式内容.
require 'open-uri'
require 'nokogiri'
require 'css_parser'
url = open('https://google.com')
html = Nokogiri::HTML(url)
css = CssParser::Parser.new
css.add_block!(html.search('style#gstyle').text)
Run Code Online (Sandbox Code Playgroud)
这会返回nil,但Google页面的HTML包含id="gstyle".这是输出结果的图像:
style#gstyle?open-uri ×7
ruby ×6
nokogiri ×3
css-parsing ×1
html ×1
http ×1
json ×1
net-http ×1
ole ×1
rake ×1
rake-task ×1
regex ×1
timeout ×1
web-crawler ×1
web-scraping ×1