gpt4 book ai didi

ruby - 如何使用 Mechanize gem 从 html 页面上的 'td' 标签中获取 'table' 标签内的所有文本?

转载 作者:行者123 更新时间:2023-12-04 16:20:09 26 4
gpt4 key购买 nike

我正在尝试使用 Mechanize gem 解析表,但我不知道如何迭代表。

最佳答案

Mechanize 用途 nokogiri 用于解析 HTML,因此您应该在那里查找文档。即,看看 xpath 方法。

这是一个示例,解析当前页面:

require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open('http://stackoverflow.com/questions/4265745/how-to-get-all-text-inside-td-tags-from-table-tag-on-html-page-using-mechaniz'))
table = doc.xpath('//table').first # getting the first table on the page
table.xpath('tr/td').count # getting all the td nodes right below table/tr and counting them
#=> 4

关于ruby - 如何使用 Mechanize gem 从 html 页面上的 'td' 标签中获取 'table' 标签内的所有文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4265745/

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