我需要帮助通过 div 类而不是链接文本单击某些元素以访问页面以抓取一些数据。
- 从页面http://www.salatomatic.com/b/United-States+125开始,如何不使用链接文本而是通过 div 类单击每个州的名称?
- 点击一个州后,例如http://www.salatomatic.com/b/Alabama+7,我需要点击该州的一个区域,再次通过 div 类,而不是链接的文本。
- 在一个区域内,www [dot] salatomatic [dot] com/c/Birmingham+12,我想循环浏览,点击每个项目(本例中为 11 个清真寺)。
- 在项目/清真寺内,我需要抓取地址(在清真寺标题下方的顶部)并将其存储/创建在我的数据库中。
更新:
我现在有这个:
require 'nokogiri'
require 'open-uri'
require 'mechanize'
agent = Mechanize.new
page = agent.get("http://www.salatomatic.com/b/United-States+125")
#loops through all state links
page.search('.subtitleLink a').map{|a| page.uri.merge a[:href]}.each do |uri|
page2 = agent.get uri
#loops through all regions in each state
page2.search('.subtitleLink a').map{|a| page2.uri.merge a[:href]}.each do |uri|
page3 = agent.get uri
#loops through all places in each region
page3.search('.subtitleLink a').map{|a| page3.uri.merge a[:href]}.each do |uri|
page4 = agent.get uri
#I'm able to grab the title of the place but not sure how to get the address b/c there is no div around it.
puts page4.at('.titleBM')
#I'm guessing I would use some regex/xpath here to get the address, but how would that work?
#This is the structure of the title/address in HTML:
<td width="100%"><div class="titleBM">BIS Hoover Crescent Islamic Center </div>2524 Hackberry Lane, Hoover, AL 35226</td> This is the listing page: http://www.salatomatic.com/d/Hoover+12446+BIS-Hoover-Crescent-Islamic-Center
end
end
end