【发布时间】:2016-12-23 09:55:59
【问题描述】:
这是我现在的代码的抓取部分
while counter <= 3
url = "http://www.indeed.ca/jobs?q=" << job_title_search << "&l=" << job_location << ",+ON&start=" << (counter * 20).to_s
counter += 1
doc = Nokogiri::HTML(open(url))
# can't put in if loop to only perform once for some reason
page = agent.get(url)
current_page = agent.page.uri
puts current_page
doc.css(".result").each do |item|
job_title = item.at_css(".jobtitle").text[/[^\s][a-zA-Z -]*/]
job_company = item.at_css(".company").text[/[^\s][a-zA-Z -]*/]
full_job = job_title + " - " + job_company
agent.current_page.link_with(:class => '.jobtitle').click
posting_page = agent.page.uri
puts posting_page
if cache.include?(full_job) == false
cache << full_job
puts "#{job_title} - #{job_company}"
end
end
puts ""
end
我知道存在“.jobtitle”类这一事实,因为返回职位标题的部分工作正常。我想弄清楚的是为什么 Mechanize 不允许我使用“点击”功能。在我在维基百科主页上进行的其他测试中,它在 CSS ID、类和文本上运行良好。
所以我的问题是是什么导致它在点击功能上失败?它生成的错误是通用的“nil:NilClass 的未定义方法 'click'”。
【问题讨论】:
标签: ruby web-scraping automation nokogiri mechanize