如何刮R中的多个表?

时间:2015-10-24 08:33:16

标签: python r web-scraping multiple-tables

我是"新手"当涉及到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

1 个答案:

答案 0 :(得分: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数据框。