【问题标题】:Text mining a webpage and extracting only <li> features文本挖掘网页并仅提取 <li> 特征
【发布时间】:2017-10-06 15:19:06
【问题描述】:

我正在尝试向网站发送关键字,但不知道如何进行。

我想从网页中提取所有带有&lt;li&gt; 标签的项目。所以我有 HTML 代码;

<H1> Some header </H1>
<text> Some noise I am not interested in </text>
<li> some text here </li>
<li> some more text here </li>
<li> even more text here </li>
<text> Some more noice I am also not interested in </text>

所以我只对列表&lt;li&gt;&lt;/li&gt;的项目感兴趣

有人可以为我指出正确的方向吗?

或者,我使用了 readLines 函数并将网页保存为 data_frame,因此我正在考虑尝试在不使用 &lt;li&gt; 命令的情况下删除所有行。

任何想法

【问题讨论】:

  • 我喜欢 Python 和 Beautiful Soup 模块
  • 你只要连接到网页,然后soup.find_all('li')返回所有li元素
  • 谢谢,但我忘了说我只用 R 编程!对不起,我的坏事。
  • 试试library(rvest); html &lt;- read_html("link"); html %&gt;% html_nodes("li")
  • Python 对 R 问题的建议似乎……错了? @Mako212 Esp,因为 R 能够胜任这项任务。

标签: r web-scraping text-mining


【解决方案1】:

如果你有一个网页列表,你可以迭代这个列表并使用下面的 sn-p 来提取&lt;li&gt;-Tags之间的实际文本

library(rvest)

scraper <- read_html("http://yourpage.com")

scraping_wiki %>%
    html_nodes("li")%>%
    html_text()

在你的情况下,这将返回

    ##  [1] " some text here "                             
    ##  [2] " some more text here "  
    ##  [3] " some more text here "                     

【讨论】:

    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 2013-11-05
    • 2018-09-21
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2019-05-05
    • 2013-02-09
    相关资源
    最近更新 更多