繁体   English   中英

如何使用R从HTML提取包含破折号的URL?

[英]How can I extract URLs containing dashes from HTML using R?

我有一些看起来像这样的HTML:

<ul><li><a href="http://www.website.com/index.aspx" target="_blank">Website</a></li>
<li><a href="http://website.com/index.html" target="_blank">Website</a></li>
<li><a href="http://www.website-with-dashes.org" target="_blank">Website With Dashes</a></li>
<li><a href="http://website2.org/index.htm" target="_blank">Website 2</a></li>
<li><a href="http://www.another-site.com/">Another Site</a></li>

运用

m<-regexpr("http://\\S*/?", links, perl=T)
links<-regmatches(links, m)

获取链接,但其中带有破折号的链接会被截断,如下所示:

http://www.website.com/index.aspx
http://website.com/index.html
http://www.website
http://website2.org/index.htm
http://www.another-site.com/

我以为/ S与任何非空白匹配。 这是怎么回事?

使用XML::getHTMLlinks

例如

library(XML)
# assuming your html document is'foo.html')

 getHTMLLinks(doc = 'foo.html')
# [1] "http://www.website.com/index.aspx"  "http://website.com/index.html"      "http://www.website-with-dashes.org"
# [4] "http://website2.org/index.htm"      "http://www.another-site.com/" 

用正则表达式解析HTML不一定很简单。 https://stackoverflow.com/a/1732454/1385941很有趣。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM