麻烦从维基百科刮表

时间:2015-09-02 00:31:13

标签: r xml web-scraping

我无法按照this question的选定答案进行操作。我试图抓的桌子是this list of U.S. state populations

library(XML)
theurl <- "http://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population"
tables <- readHTMLTable(theurl)
n.rows <- unlist(lapply(tables, function(t) dim(t)[1]))

这是我得到的错误..

Error: failed to load external entity "http://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population"

是什么给出了?

(注意 - 虽然我想解决这个错误,如果你能指出我更容易获得人口数据的方法,我会很感激。)

2 个答案:

答案 0 :(得分:2)

您的代码没有任何问题。但是,您的网址存在问题。

您可以通过转到shell并尝试验证代码中的外部输入是否导致其失败来测试这一点,例如,

curl https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population

将返回一个空体,类似于你的R代码。这应该会让你相信你的R代码不是有问题的。完成此发现后,您可以继续使用您感兴趣的页面中的部分,再次使用curl中的免费且简单的测试环境,然后运行

curl https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population#States_and_territories

绝对不会返回空结果:

...
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-List_of_U_S_states_and_territories_by_population skin-vector action-view">
    <div id="mw-page-base" class="noprint"></div>
    <div id="mw-head-base" class="noprint"></div>
    <div id="content" class="mw-body" role="main">

答案 1 :(得分:1)

import UIKit class ViewController: UIViewController { @IBAction func myButton(sender: AnyObject) { UIApplication.sharedApplication().openURL(NSURL(string: "http://www.mcmastermhs.com")!) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

中这很容易做到
rvest

有关详情,请参阅@ Cory&#39; blog