标签: hpricot

如何在Nokogiri中进行正则表达式搜索以查找与某个开头相匹配的文本?

鉴于:

require 'rubygems'
require 'nokogiri'
value = Nokogiri::HTML.parse(<<-HTML_END)
"<html>
<body>
  <p id='para-1'>A</p>
  <div class='block' id='X1'>
    <h1>Foo</h1>
    <p id='para-2'>B</p>
  </div>
  <p id='para-3'>C</p>
  <h2>Bar</h2>
  <p id='para-4'>D</p>
  <p id='para-5'>E</p>
  <div class='block' id='X2'>
    <p id='para-6'>F</p>
  </div>
</body>
</html>"
HTML_END
Run Code Online (Sandbox Code Playgroud)

我想做一些像我在Hpricot中可以做的事情:

divs = value.search('//div[@id^="para-"]')
Run Code Online (Sandbox Code Playgroud)
  1. 如何在XPath样式中搜索元素的模式?
  2. 我在哪里可以找到帮助我的文档?我没有在rdocs中看到这一点.

ruby hpricot nokogiri

25
推荐指数
2
解决办法
1万
查看次数

Nokogiri vs Hpricot?

你会选哪一个?我的重要属性是(不按顺序):

  1. 支持和未来的增强功能.
  2. 社区和一般知识库(在互联网上).
  3. 全面的(IE,经证明可以解析各种*.*ml页面).
  4. 性能.
  5. 内存占用(运行时,而不是代码库).

ruby hpricot html-parsing nokogiri

23
推荐指数
3
解决办法
7267
查看次数

在Windows上的Ruby 1.9.1上安装Hpricot

我正在尝试使用以下命令安装hpricot:

>gem install hpricot -v 0.8.2
Building native extensions.  This could take a while...
ERROR:  Error installing hpricot:
    ERROR: Failed to build gem native extension.

C:/Ruby19/bin/ruby.exe extconf.rb checking for stdio.h... * extconf.rb failed * Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.

Provided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=C:/Ruby19/bin/ruby C:/Ruby19/lib/ruby/1.9.1/mkmf.rb:362:in try_do': The complier failed …

Run Code Online (Sandbox Code Playgroud)

ruby hpricot

15
推荐指数
2
解决办法
9632
查看次数

open-uri不会将http重定向到https

我正在使用Hpricot和OpenURI来解析网页并从中提取网址.

当我收到"http:rapidshare.com"这样的链接时,它不会重定向到https.这是我得到的错误:

/home/leonidus/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/open-uri.rb:216:in 
`open_loop': redirection forbidden: http:.................=>     
https:.........................
.
.
Run Code Online (Sandbox Code Playgroud)

我试图使用异常处理程序,OPENURI::HTTPREDIRECT但我再次得到相同的错误.我尝试了所有的博客,但它也没有解决.

ruby hpricot open-uri rubygems

12
推荐指数
1
解决办法
9581
查看次数

使用Ruby从HTML文档中删除文本

有很多关于如何使用Ruby从文档中剥离HTML标记的示例,Hpricot和Nokogiri都有inner_text方法,可以轻松快速地删除所有HTML.

我想要做的是相反,删除HTML文档中的所有文本,只留下标记及其属性.

我考虑通过将inner_html文件设置为nil来循环,但实际上你必须反向执行此操作,因为第一个元素(root)具有文档其余部分的inner_html,所以理想情况下我必须从最内层的元素,并将inner_html设置为nil,同时向上移动通过祖先.

有没有人知道一个巧妙的小技巧,有效地做到这一点?我想也许正则表达式可能会这样做,但可能不像HTML tokenizer/parser那样有效.

html ruby hpricot nokogiri

9
推荐指数
1
解决办法
1万
查看次数

任何Ruby的HTML解析器都能用JavaScript来查看生成的DOM吗?

在尝试Hpricot和Nokogiri时,HTML可以被提取和解析,但是他们是否也可以执行Javascript以便内容显示在页面上?(显示在DOM中).那是因为除非JavaScript初始化代码已经运行,否则某些页面不会显示信息.

javascript ruby hpricot html-parsing nokogiri

7
推荐指数
1
解决办法
896
查看次数

什么是"终止对象",为什么我不能在其上调用方法?

我定期得到这个例外:

NotImplementedError: method `at' called on terminated object
Run Code Online (Sandbox Code Playgroud)

在这行代码上:

next if Hpricot(html).at('a')
Run Code Online (Sandbox Code Playgroud)

这个错误是什么意思?我怎么能避免呢?

ruby hpricot

6
推荐指数
1
解决办法
525
查看次数


Ruby Mechanize表刮除不捕获整行

我试图用机械化刮一个表网站.我想刮第二排.

当我跑:

agent.page.search('table.ea').search('tr')[-2].search('td').map{ |n| n.text }

我希望它会刮掉整排.但相反它只是刮擦:["2011-02-17","0,00"]

为什么不抓取行中的所有列,而只是第一列和最后一列?

Xpath: / html/body/center/table/tbody/tr [2]/td [2]/table/tbody/tr [3]/td/table/tbody/tr [2]/td/table/tbody/tr [2]

CSS路径: html体中心表tbody tr td table tbody tr td table tbody tr td table.ea tbody tr td.total

该页面与此类似:

<table><table><table>
<table width="100%" border="0" cellpadding="0" cellspacing="1" class="ea">
<tr>
    <th><a href="#">Date</a></th>
    <th><a href="#">One</a></th>    
    <th><a href="#">Two</a></th>    
    <th><a href="#">Three</a></th>     
    <th><a href="#">Four</a></th>    
    <th><a href="#">Five</a></th>        
    <th><a href="#">Six</a></th>        
    <th><a href="#">Seven</a></th>      
    <th><a href="#">Eight</a></th>
</tr>
<tr>
    <td><a href="#">2011-02-17</a></td>
    <td align="right">0</td>    
    <td align="right">0</td>    
    <td align="right">0,00</td>     
    <td align="right">0</td>    
    <td align="right">0</td>        
    <td align="right">0</td>    
    <td align="right">0</td>        
    <td align="right">387</td>      
    <td …
Run Code Online (Sandbox Code Playgroud)

hpricot ruby-on-rails nokogiri ruby-on-rails-3 mechanize-ruby

5
推荐指数
1
解决办法
5726
查看次数

使用Ruby with Mechanize登录网站

我需要从网站上抓取数据,但首先需要登录.我一直在使用hpricot成功地抓住其他网站,但我是新手使用机械化,我真的很困惑如何工作.

我看到这个例子通常引用:

require 'rubygems'
require 'mechanize'

a = Mechanize.new
a.get('http://rubyforge.org/') do |page|
  # Click the login link
  login_page = a.click(page.link_with(:text => /Log In/))

  # Submit the login form
  my_page = login_page.form_with(:action => '/account/login.php') do |f|
    f.form_loginname  = ARGV[0]
    f.form_pw         = ARGV[1]
  end.click_button

  my_page.links.each do |link|
    text = link.text.strip
    next unless text.length > 0
    puts text
  end
end
Run Code Online (Sandbox Code Playgroud)

但我发现它非常神秘.我不明白的部分是这里发生了什么:

f.form_loginname  = ARGV[0]
f.form_pw         = ARGV[1]
Run Code Online (Sandbox Code Playgroud)

来自页面的那些输入标签如何突然成为方法?我在这里错过了什么吗?当我尝试重新创建它时,要登录到AppDataPro(http://www.appdata.com/login),我遇到输入名称包含括号的问题,如下所示:

<Table> 
<tr><td width="150"> 
   <label for="user_session_username">Username</label><br /> 
</td><td > 
    <input id="user_session_username" name="user_session[username]" size="30" type="text" /> 
</td></tr> …
Run Code Online (Sandbox Code Playgroud)

ruby screen-scraping hpricot login mechanize

5
推荐指数
1
解决办法
1万
查看次数