在 R 中抓取 HTML 文本的特定部分
scrape a certain portion of HTML text in R
我正在尝试抓取国家气象局网页,只取出一部分文本并将其转换为 R 中的字符对象。它最终会成为 NWS 页面上显示的一小段。 (见下文)
我一直在使用 rvest 包抓取网页,并尝试使用 XML 包编写一些代码。
这是我的代码,其中包含天气服务 URL。
weather_con <- read_html("http://forecast.weather.gov/product.php?site=TWC&issuedby=TWC&product=AFD&format=txt&version=1&glossary=1")
weather_con <- weather_con %>%
html_nodes("#localcontent") %>%
html_text()
我也尝试过将 rvest 和 XML 包与此代码一起使用
weather_con <- getURL("http://forecast.weather.gov/product.php?site=TWC&issuedby=TWC&product=AFD&format=txt&version=1&glossary=1")
weather_con <- htmlParse(weather_con, asText = T)
这两组代码都读取了页面中的所有文本。我尝试了其他选项,并试图找到页面的节点以抓取文本的某些部分,但我没有找到任何有用的东西。我对 HTML 没有什么经验,所以我可能在这里遗漏了一些简单的东西。
我只想从网页中提取出概要段落。它是靠近页面顶部的一个小段落,方便地以两个 && 符号结束,位于该段落结束处的下方一行。
也许我需要类似 substr
的功能,以便我可以直接抓取该段落。但是,我希望在 rvest 和/或 XML 中找到一些东西来完成这项工作。
有什么建议吗?
谢谢
weather_con
中已有您需要的文本,但它与所有其他文本一起出现。
提取它的一种方法是使用正则表达式。
synopsis = regmatches(x = weather_con,
m = regexpr(pattern = "SYNOPSIS[^&]*",
text = weather_con))
这将捕获从 SYNOPSIS 到第一个非 &
.
的所有内容
结果:
[1] "SYNOPSIS...Strong high pressure aloft will
maintain well above\naverage temperatures today. Thursday
and Friday will see us between\nlow pressure developing
north of the area and high pressure shifting\nsouthward.
As a result, expect gusty winds and several degrees
of\ncooling. Strengthening high pressure this weekend
will again push\ntemperatures above average.\n\n"
如果 synopsys 包含 &
那么您可以捕获文本直到讨论这个词。
synopsis2 = regmatches(x = weather_con,
m = regexpr(pattern = "SYNOPSIS.*DISCUSSION",
text = weather_con))
结果类似。此结果以 above average.\n\n&&\n\n.DISCUSSION
结尾
我正在尝试抓取国家气象局网页,只取出一部分文本并将其转换为 R 中的字符对象。它最终会成为 NWS 页面上显示的一小段。 (见下文)
我一直在使用 rvest 包抓取网页,并尝试使用 XML 包编写一些代码。
这是我的代码,其中包含天气服务 URL。
weather_con <- read_html("http://forecast.weather.gov/product.php?site=TWC&issuedby=TWC&product=AFD&format=txt&version=1&glossary=1")
weather_con <- weather_con %>%
html_nodes("#localcontent") %>%
html_text()
我也尝试过将 rvest 和 XML 包与此代码一起使用
weather_con <- getURL("http://forecast.weather.gov/product.php?site=TWC&issuedby=TWC&product=AFD&format=txt&version=1&glossary=1")
weather_con <- htmlParse(weather_con, asText = T)
这两组代码都读取了页面中的所有文本。我尝试了其他选项,并试图找到页面的节点以抓取文本的某些部分,但我没有找到任何有用的东西。我对 HTML 没有什么经验,所以我可能在这里遗漏了一些简单的东西。
我只想从网页中提取出概要段落。它是靠近页面顶部的一个小段落,方便地以两个 && 符号结束,位于该段落结束处的下方一行。
也许我需要类似 substr
的功能,以便我可以直接抓取该段落。但是,我希望在 rvest 和/或 XML 中找到一些东西来完成这项工作。
有什么建议吗?
谢谢
weather_con
中已有您需要的文本,但它与所有其他文本一起出现。
提取它的一种方法是使用正则表达式。
synopsis = regmatches(x = weather_con,
m = regexpr(pattern = "SYNOPSIS[^&]*",
text = weather_con))
这将捕获从 SYNOPSIS 到第一个非 &
.
结果:
[1] "SYNOPSIS...Strong high pressure aloft will
maintain well above\naverage temperatures today. Thursday
and Friday will see us between\nlow pressure developing
north of the area and high pressure shifting\nsouthward.
As a result, expect gusty winds and several degrees
of\ncooling. Strengthening high pressure this weekend
will again push\ntemperatures above average.\n\n"
如果 synopsys 包含 &
那么您可以捕获文本直到讨论这个词。
synopsis2 = regmatches(x = weather_con,
m = regexpr(pattern = "SYNOPSIS.*DISCUSSION",
text = weather_con))
结果类似。此结果以 above average.\n\n&&\n\n.DISCUSSION