首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在r中进行web抓取时,xpath返回空文本

在r中进行web抓取时,xpath返回空文本
EN

Stack Overflow用户
提问于 2019-05-26 19:46:10
回答 1查看 39关注 0票数 0

我在试着从https://www.kff.org/interactive/subsidy-calculator那里获取信息。例如,放置state=California,zip=90001,income=20000,无覆盖,1人,1成人,无儿童,age=21,无烟草。

我们得到以下信息:https://www.kff.org/interactive/subsidy-calculator/#state=ca&zip=94704&income-type=dollars&income=20000&employer-coverage=0&people=1&alternate-plan-family=individual&adult-count=1&adults%5B0%5D%5Bage%5D=21&adults%5B0%5D%5Btobacco%5D=0&child-count=0

我想得到“估计的财务帮助”和“你的银色计划的费用”的数字(它们在“结果”灰色框中以蓝色粗体显示,由于某种原因,我无法上传屏幕截图)。当我对数字使用xpath时,我得到的结果是空字符串。如果我要检索一些其他文本(不在灰色框中),情况就不是这样了。我想知道这会有什么问题。我附加了下面的代码。如果这是一个愚蠢的问题,请原谅我,因为我对网络抓取非常陌生。谢谢!

代码语言:javascript
复制
state = tolower('CA')
zip = 94704
income = 20000
people = 1
adult = 1
children = 0

url = paste0("https://www.kff.org/interactive/subsidy-calculator/#state=", state, "&zip=", zip, "&income-type=dollars&income=", income, "&employer-coverage=0&people=", people, "&alternate-plan-family=individual&adult-count=", adult, "&adults%5B0%5D%5Bage%5D=21&adults%5B0%5D%5Btobacco%5D=0&child-count=", children)

# This returns empty string
r = read_html(url) %>%
      html_nodes(xpath ='//*[@id="subsidy-calculator-new"]/div[5]/div/div/dl/dd[1]/span') %>% html_text()

# This returns "Number of children (20 and younger) enrolling in Marketplace coverage", a line that's not in the grey box.
r = read_html(url) %>%
  html_nodes(xpath = '//*[@id="subsidy-form"]/div[2]/div[3]/div[3]/p') %>%
  html_text()
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56313196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档