如何使用Mechanize / Nokogiri获取页面源

问题描述 投票:23回答:3

我已使用Mechanize登录到网页/ servlet。

我有一个页面对象:

jobShortListPg = agent.get(addressOfPage)

当我使用时:

puts jobShortListPg

我得到了不需要的页面“机械化”版本:

#<Mechanize::Page::Link "Home" "blahICScriptProgramName=WEBLIB_MENU.ISCRIPT3.FieldFormula.IScript_DrillDown&target=main0&Level=0&RL=&navc=3171">

如何获取页面的HTML源代码?

ruby nokogiri mechanize
3个回答
34
投票

使用.body

puts jobShortListPg.body

1
投票

使用页面对象的content方法。

jobShortListPg.content

0
投票

在Nokogiri中,使用主文档节点上的to_sto_s

to_html

或:

to_html

如果让您分心看到嵌入的换行符,这可能会有所帮助:

require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<html>
  <head></head>
  <body>foo</body>
</html>
EOT

doc.to_html
# => "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n" +
#    "<html>\n" +
#    "  <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head>\n" +
#    "  <body>foo</body>\n" +
#    "</html>\n"
© www.soinside.com 2019 - 2024. All rights reserved.