gpt4 book ai didi

ruby - 在 Ruby 中使用 Mechanize 提取链接

转载 作者:行者123 更新时间:2023-12-04 16:22:05 25 4
gpt4 key购买 nike

我正在尝试使用 Mechanize 从元素 (.jobtitle a) 中提取链接。我正在尝试在下面的链接变量中做到这一点。有谁知道怎么做?

require 'rubygems'
require 'mechanize'

agent = Mechanize.new
page = agent.get('http://id.indeed.com/')
indeed_form = page.form('jobsearch')
indeed_form.q = ''
indeed_form.l = 'Indonesia'
page = agent.submit(indeed_form)
page.search(".row , .jobtitle a").each do |job|
job_title = job.search(".jobtitle a").map(&:text).map(&:strip)
company = job.search(".company span").map(&:text).map(&:strip)
date = job.search(".date").map(&:text).map(&:strip)
location = job.search(".location span").map(&:text).map(&:strip)
summary = job.search(".summary").map(&:text).map(&:strip)
link = job.search(".jobtitle a").map(&:text).map(&:strip)
end

最佳答案

我认为您不能使用 css 路径选择属性。

从 Mechanize 文档:

search()

Search for paths in the page using Nokogiri's search. The paths can be XPath or CSS and an optional Hash of namespaces may be appended.

See Nokogiri::XML::Node#search for further details.



您应该查看 XPaths。见例如:

Getting attribute using XPath

http://www.w3schools.com/xpath/

您可能需要重写遍历页面的方式。

关于ruby - 在 Ruby 中使用 Mechanize 提取链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22140189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com