如何从 div poltergeist/capybara 中的多个段落中提取文本
How to extract text from multiple paragraph in div poltergeist/capybara
我正在使用这个命令来提取 p 标签
session.all('.entry p')
给出结果
[#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[1]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[2]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[3]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[4]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[5]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[6]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[7]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[8]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[9]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[10]">]
现在我想从p节点中提取所有文本,我知道有循环方法可以合并所有段落文本,还有其他方法吗?水豚提供?
#all
的结果是 Capybara::Result。文档说:
A Result represents a collection of Node::Element on the page. It is
possible to interact with this collection similar to an Array because
it implements Enumerable [...]
因此,您可以像与枚举对象一样与它交互,它不提供您要求的任何方法。
您可以这样做来检索串联的内容:
session.all('.entry p').map(&:text).join
根据您的标签 "web-scraping" 我假设您使用水豚进行网络抓取,而不是用于测试。由于 capybara 的主要目的是测试它没有内置方法来满足您的要求。
如果你正在实施测试,你应该做这样的事情(我在这里使用 RSpec):
within('.entry') do
expect(page).to have_text 'something'
end
或者,如果您确实需要非常具体地说明规范的位置(在大多数情况下这是不必要的),您应该单独测试每个元素:
expect(session.all('.entry p')[0]).to have_content 'something'
expect(session.all('.entry p')[1]).to have_content 'something else'
最后一个旁注:对于网络抓取,有比水豚更好的选择。
我正在使用这个命令来提取 p 标签
session.all('.entry p')
给出结果
[#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[1]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[2]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[3]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[4]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[5]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[6]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[7]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[8]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[9]">,
#<Capybara::Element tag="p" path="//HTML[1]/BODY[1]/DIV[3]/DIV[1]/SECTION[1]/DIV[1]/ARTICLE[1]/DIV[3]/P[10]">]
现在我想从p节点中提取所有文本,我知道有循环方法可以合并所有段落文本,还有其他方法吗?水豚提供?
#all
的结果是 Capybara::Result。文档说:
A Result represents a collection of Node::Element on the page. It is possible to interact with this collection similar to an Array because it implements Enumerable [...]
因此,您可以像与枚举对象一样与它交互,它不提供您要求的任何方法。
您可以这样做来检索串联的内容:
session.all('.entry p').map(&:text).join
根据您的标签 "web-scraping" 我假设您使用水豚进行网络抓取,而不是用于测试。由于 capybara 的主要目的是测试它没有内置方法来满足您的要求。
如果你正在实施测试,你应该做这样的事情(我在这里使用 RSpec):
within('.entry') do
expect(page).to have_text 'something'
end
或者,如果您确实需要非常具体地说明规范的位置(在大多数情况下这是不必要的),您应该单独测试每个元素:
expect(session.all('.entry p')[0]).to have_content 'something'
expect(session.all('.entry p')[1]).to have_content 'something else'
最后一个旁注:对于网络抓取,有比水豚更好的选择。