js 抓取网页内容(通过CSS元素作为识别所需内容部分的方法,我可以轻松地抓取内容 )

优采云 发布时间: 2022-04-03 10:24

  js 抓取网页内容(通过CSS元素作为识别所需内容部分的方法,我可以轻松地抓取内容

)

  我可以通过使用 CSS 元素来轻松地抓取 HTML 内容,以此来识别我想要的部分内容,但我需要抓取网页的一部分:

  

PeopleSafe

//

  我需要从这一行解析纬度和经度:

  map.setCenter( new GLatLng( 51.612308, -1.239453 ), 11 );

  所以,在我的表格的一列中,我想看到第一部分:

  51.612308

  在第二列,我想看第二部分:

  -1.239453

  如果没有 CSS 选择器,这可能吗?

  编辑

  非常感谢到目前为止的帮助!

  最初的问题是在您登录该站点后立即重定向,我对其进行了排序,现在当我这样做时:

  put page.root

  我得到了我期望的页面的完整来源。所以现在我的代码(登录后)是:

  html_doc = page.root

# Find the first in the head that does not have src="..."

#script = html.at_xpath('/html/head/script[not(@src)]')

# Use a regex to find the correct code parts in the JS, using named captures

parts = script.text.match(/new GLatLng\(\s*(?.+?)\s*,\s*(?.+?)\s*\)/)

p parts[:lat], parts[:long]

#=> "51.612308"

#=> "-1.239453"

  运行上述命令时出现错误:

  undefined local variable or method `script' for main:Object

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线