Is there anyway to search and get the value of <a>.. </a>?

人走茶凉 提交于 2019-12-24 12:44:28

问题


In a webpage suppose i have the below values:

<td> <a href="https://www.test.com/test123/a.html"> test11 </a> </td>
<td> <a href="https://www.test.com/test12333/r.html"> test12 </a> </td>
<td> <a href="https://www.test.com/testaa123/t.html"> test21 </a> </td>
<td> <a href="https://www.test.com/test123123/b.html"> test31 </a> </td>

Is there anyway to find the value test21 using Ruby?

Or is there anyway to find the href values which has a substring /testaa123/t.html?


回答1:


Try out this tutorial for Nokogiri.

Example for a <li> tag:

require 'rubygems'
require 'nokogiri'   
require 'open-uri'
PAGE_URL = "http://ruby.bastardsbook.com/files/hello-webpage.html"

page.css('li')[0].text

This will output YouTube from the site below:

<div id="funstuff">
   <p>Here are some entertaining links:</p>
   <ul>
      <li><a href="http://youtube.com">YouTube</a></li>
      <li><a data-category="news" href="http://reddit.com">Reddit</a></li>
      <li><a href="http://kathack.com/">Kathack</a></li>
      <li><a data-category="news" href="http://www.nytimes.com">New York Times</a></li>
  </ul>
</div>


来源:https://stackoverflow.com/questions/14428265/is-there-anyway-to-search-and-get-the-value-of-a-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!