r - R中的抓取列表
问题描述
我想从本地 HTML 文件中抓取元素列表(名称播放器、成本、买家、卖家、日期),但是当我尝试抓取买家和卖家时(在本例中为第一次传输“计算机”和“彼得”)以及第二次传输“计算机”和“詹姆斯”)
document.querySelector("#pressReleases > ul > li:nth-child(**2**) > ul > li.text > div > strong:nth-child(2)")
document.querySelector("#pressReleases > ul > li:nth-child(**3**) > ul > li.text > div > strong:nth-child(2)")
如何刮掉li
使这 2 个变量的元素?
我在 R 中试过这个:
dades<- mylocalfile
player<-dades %>% html_nodes("ul.player li.text strong") %>% html_text() %>% trimws()
cost<-dades %>% html_nodes("ul.player li.text span") %>% html_text() %>% trimws()
buyer<-dades %>% html_nodes("#pressReleases > ul > li:nth-child(2) > ul > li.text > div > strong:nth-child(2)") %>% html_text() %>% trimws()
seller<-dades %>% html_nodes("#pressReleases > ul > li:nth-child(2) > ul > li.text > div > strong:nth-child(1)") %>% html_text() %>% trimws()
day<-dades %>% html_nodes("ul.player li.text time") %>% html_text() %>% trimws()
我检测到这 2 #pressReleases > ul > li:nth-child(2)
对于每个都是可变的li class="post pressRelease"
html代码:
<div class="newsList" id="pressReleases">
<ul>
<li class="date" style="background-color: rgb(128, 128, 128);">
<strong>Fitxatges del dia</strong>
09/08/2019
</li>
<li class="post pressRelease">
<ul class="player">
<li class="photo">
<img src="./futmondo - Fútbol fantasy manager - futmondo_files/espanyol.png" onerror="Futmondo.Helpers.Resources.onErrorPlayerPhoto(this, "L", "espanyol.png")">
<img src="./futmondo - Fútbol fantasy manager - futmondo_files/espanyol(1).png" alt="Espanyol" class="crest">
</li>
<li class="text">
<strong>Player1</strong>
<time>09/08/2019 - 05:30</time>
<span>16.245.485 €</span>
<div class="from">
D'
<strong>computer</strong>
a
<strong>peter</strong>
</div>
</li>
<a class="icon-revert">
</a>
</ul>
<div class="bid second">
<span class="triangle"></span>
<strong class="second">2º puja</strong>
<strong>matheu:</strong>
<span class="price">15.925.828 €</span>
</div>
</li>
<li class="post pressRelease">
<ul class="player">
<li class="photo">
<img src="./futmondo - Fútbol fantasy manager - futmondo_files/real-sociedad.png" onerror="Futmondo.Helpers.Resources.onErrorPlayerPhoto(this, "L", "real-sociedad.png")">
<img src="./futmondo - Fútbol fantasy manager - futmondo_files/real-sociedad(1).png" alt="Real Sociedad" class="crest">
</li>
<li class="text">
<strong>Player2</strong>
<time>09/08/2019 - 05:30</time>
<span>1.111.711 €</span>
<div class="from">
D'
<strong>computer</strong>
a
<strong>james</strong>
</div>
</li>
<a class="icon-revert">
</a>
</ul>
</li>
解决方案
这是获得的可能解决方案buyer/seller
:
# Read the local file
URL <- 'D:/Test/Test.html'
wp <- xml2::read_html(URL, encoding = 'utf-8')
# Extract the relevant nodes
node <- rvest::html_nodes(wp, '.from')
# Extract the names
seller <- gsub('.*D\'\r\n\\s+(.*?)\r\n\\s+a\\s?\r\n\\s+(.*?)\r\n.*', '\\1', rvest::html_text(node))
# [1] "computer" "computer"
buyer <- gsub('.*D\'\r\n\\s+(.*?)\r\n\\s+a\\s?\r\n\\s+(.*?)\r\n.*', '\\2', rvest::html_text(node))
# [1] "peter" "james"
推荐阅读
- php - 插入数据库codeigniter时出现sql语法错误
- oracle - 如何通过 Xquery 更新表中的 oracle XML 数据类型
- spring - 混合html和json模板时如何在百里香中正确设置内容类型
- javascript - PouchDB 获取数组值
- python - 如何使用 tensorflow 2.0 构建基本的 mnist 神经网络?
- sql - 即使没有 rownum 显示记录,rownum 也不会返回任何内容
- tkinter - 使用鼠标滚轮和滚动条一起滚动多个 Tkinter 列表框
- javascript - 将 Google 地球引擎转换为 Python API:空间连接
- visual-studio - w3wp 进程只显示 id 不显示名称
- oracle - FRM-91500:尝试 sudo su - oracle -c 运行 frmcmp_batch.sh 时无法启动/完成构建