鉴于:
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)
你会选哪一个?我的重要属性是(不按顺序):
我正在尝试使用以下命令安装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) 我正在使用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从文档中剥离HTML标记的示例,Hpricot和Nokogiri都有inner_text方法,可以轻松快速地删除所有HTML.
我想要做的是相反,删除HTML文档中的所有文本,只留下标记及其属性.
我考虑通过将inner_html文件设置为nil来循环,但实际上你必须反向执行此操作,因为第一个元素(root)具有文档其余部分的inner_html,所以理想情况下我必须从最内层的元素,并将inner_html设置为nil,同时向上移动通过祖先.
有没有人知道一个巧妙的小技巧,有效地做到这一点?我想也许正则表达式可能会这样做,但可能不像HTML tokenizer/parser那样有效.
在尝试Hpricot和Nokogiri时,HTML可以被提取和解析,但是他们是否也可以执行Javascript以便内容显示在页面上?(显示在DOM中).那是因为除非JavaScript初始化代码已经运行,否则某些页面不会显示信息.
我定期得到这个例外:
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)
这个错误是什么意思?我怎么能避免呢?
我试图用机械化刮一个表网站.我想刮第二排.
当我跑:
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
我需要从网站上抓取数据,但首先需要登录.我一直在使用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) hpricot ×10
ruby ×9
nokogiri ×5
html-parsing ×2
html ×1
javascript ×1
login ×1
mechanize ×1
open-uri ×1
rubygems ×1