【问题标题】:How to scrape multiple tables in R?如何在 R 中抓取多个表?
【发布时间】:2015-10-24 08:33:16
【问题描述】:

谈到 R,我是一个“新手”,但我真的很想知道如何从以下网站抓取多个表格(我不知道其尺寸):

https://en.wikipedia.org/wiki/World_population

(具体来说,这是python中的代码:

from bs4 import BeautifulSoup
import urllib2

url1 = "https://en.wikipedia.org/wiki/World_population"
page = urllib2.urlopen(url1)
soup = BeautifulSoup(page)

table1 = soup.find("table", {'class' : 'wikitable sortable'})
trs = soup.find_all('tr')
tds = soup.find_all('td')

for row in trs:
    for column in tds:
        a = column.get_text().strip()
        print a
    break

【问题讨论】:

  • 欢迎来到 SO!在 SO for R 上有 scores 的示例。除非您有一些 R 代码表明它不起作用,否则这很可能会因为重复或“过于宽泛”而被关闭。

标签: python r web-scraping multiple-tables


【解决方案1】:

在 R 中,

u <- "https://en.wikipedia.org/wiki/World_population" # input

library(XML)
b <- basename(u)
download.file(u, b)
L <- readHTMLTable(b)

L 现在是u 中 29 个表的列表,每个表都是一个 R 数据框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    相关资源
    最近更新 更多