【发布时间】:2020-05-25 04:08:54
【问题描述】:
我无法从我使用 stringr 库命名的字符向量中提取某些匹配项:
classes = read_lines("https://statistics.ucdavis.edu/courses/descriptions-undergrad") %>%
str_flatten()
classes 的小sn-p:
...collaborative data analysis; complete case study review and team data analysis project.
Effective: 2019 Fall Quarter.</p><h2>STA 190X—Seminar (1-2)</h2><p>Seminar—1-2 hour(s). Prerequisite(s):
STA 013 or STA 013Y or STA 032 or STA 100 or STA 103. In-depth examination of a special topic in a small
group setting. Effective: 2018 Spring Quarter.</p><h2>STA 192—Internship in Statistics (1-12)</h2>
<p>Internship—3-36 hour(s); Term Paper...
我可以清楚地看到“STA 190X”这个词在我的向量中,但我似乎无法提取它:
>str_detect(classes, "STA 190X")
[1] FALSE
>str_extract_all(classes, "STA 190X")
[[1]]
character(0)
但是如果我将一个部分直接复制并粘贴到函数中,它就可以工作:
> str_detect("</p><h2>STA 190X—Seminar (1-2)</h2>", "STA 190X")
[1] TRUE
> str_extract_all("</p><h2>STA 190X—Seminar (1-2)</h2>", "STA 190X")
[[1]]
[1] "STA 190X"
有人知道这是为什么吗?
【问题讨论】:
-
您在屏幕上看到的与您的计算机通过
read_lines下载页面时看到的不同(尝试查看源代码或在浏览器中按 Ctrl + U)。右键单击该页面,然后单击检查。建议使用rvest包之类的东西再试一次。 -
read_lines from是哪个包?stringr不知道。str(classes)的输出是什么? -
@dario str(classes) 的输出是
chr "<!DOCTYPE html><!--[if IE 8]> <html class=\"lt-ie9\"> <![endif]--><!--[if gt IE 8]><!--> <html lang=\"en\" dir="| __truncated__。 read_lines 来自readr包,它是 tidyverse 的一部分 -
@John 你能检查一下我显示的输出吗