如果是在 Rails 里,直接在Action中写,比如:
def hello
redirect_to :action =>"index"
end
def hello
redirect_to 'http://baidu.com'
end
url = "http:"xpath = "/html/body/div[2]/div[3]/div[3]/div[33]/span[3]"
doc = Nokogiri::HTML(open(url),nil,'utf-8')
page=1
doc.search("#{xpath}").each do |i|
page = i.content
puts page
end
1.upto(page.to_i) do |p|
puts p
if url =~/\?/
if url=~/page/
url_2 = url.gsub(/page=(\d+)/,"page=#{p}") #gsub 替换
else
url_2="#{url}&page=#{p}"
end
else
url_2="#{url}?page=#{p}"
end
puts url_2
xpath2="html body div.content div.main div.right626 div.contbox div.intitle div.middle h4 a"
doc2 = Nokogiri::HTML(open(url_2),nil,'utf-8')
doc2.search("#{xpath2}").each do |i2|
puts "=============="
puts i2.content
url_3=i2['herf']
doc3=Nokogiri::HTML(open(url_3),nil,'utf-8')
info="/html/body/div[2]/div[3]/div[3]/div/div[3]"
tebietishi="/html/body/div[2]/div[3]/div[3]/div[8]/span"
zhoubianjuli="/html/body/div[2]/div[3]/div[3]/div[11]/table/tbody/tr[1]"
fangfa="/html/body/div[2]/div[3]/div[3]/div[11]/table/tbody/tr[2]"
sheshipeitao="/html/body/div[2]/div[3]/div[3]/div[14]/table"
doc3.search(info).each do |info|
puts info.content
end
doc3.search(tishi).each do |tishi|
puts tishi.content
end
doc3.search(zhoubianjuli).each do |zhoubian|
puts zhoubian.content
end
doc3.search(fangfa).each do |fangda|
puts fangfa.content
end
doc3.search(sheshipeitao).each do |sheshi|
puts sheshi.content
end
end
在url_3的时候,显示没有给赋值,怎么解决?急啊
因为这个元素在60秒内一直没有展现出来吧,所以报了超时,贴下这个方法的源码:#
# Waits until the element is present.
#
# @example
# browser.button(:id =>'foo').wait_until_present
#
# @param [Fixnum] timeout seconds to wait before timing out
#
# @see Watir::Wait
# @see Watir::Element#present?
#
def wait_until_present(timeout = 30)
message = "waiting for #{selector_string} to become present"
Watir::Wait.until(timeout, message) { present? }
end
def until(timeout = 30, message = nil, &block)
end_time = ::Time.now + timeout
until ::Time.now >end_time
result = yield(self)
return result if result
sleep INTERVAL
end
raise TimeoutError, message_for(timeout, message)
end
希望对你有帮助