0

我在导入包含使用(我怀疑的)javascript 填充的表的网站的 html 时遇到问题。如果我将网站作为源代码保存在我的 HD 上,我可以使用 R 中的 XML 包轻松地做我想做的事;如果我尝试直接使用阅读该网站

doc <- htmlTreeParse(url)

表中的数据丢失。我怀疑这是因为 htmlTreeParse 不处理并发请求。有没有办法让 xml 包请求“完整”网站?

我已经能够使用 Python 和 urllib2 库获得完整的 html

req = urllib2.Request('http://www.theURL.com')
response = urllib2.urlopen(req)
the_page = response.read()

但我更愿意坚持使用 R。

我一直在搞乱的网站是http://bigcharts.marketwatch.com/reports/bigmovers.asp?date=20130104&data=1&start=1&report=1&report_country_code=US

谢谢你。

4

1 回答 1

2

使用 Gsee 评论我得到了这个,

url.xml <- 'http://bigcharts.marketwatch.com/reports/bigmovers.asp?date=20130104&data=1&start=1&report=1&report_country_code=US'
dat <- readHTMLTable(url.xml)
  dat[[1]]
   Rank                      Company Name Symbol Go to: % Increase in Price     Volume Closing Price Price Change P/E Ratio
1     1             US Airways Group Inc.    LCC                      7.83% 12,049,183         14.73        +1.07       N/A
2     2                         Yelp Inc.   YELP                      6.80%  1,966,749         21.52        +1.37       N/A
3     3  Arcos Dorados Holdings Inc. Cl A   ARCO                      6.69%  2,613,174         13.72        +0.86       N/A
4     4 Thompson Creek Metals Co. Inc. Un  TC.PT                      6.21%     30,188         22.05        +1.29       N/A
5     5 Nationstar Mortgage Holdings Inc.    NSM                      6.13%  3,054,272         33.23        +1.92       N/A
6     6        Consolidated Graphics Inc.    CGX                      6.05%     60,052         36.31        +2.07       N/A
7     7         Bonanza Creek Energy Inc.   BCEI                      5.95%  1,268,468         30.10        +1.69       N/A
8     8                    Huntsman Corp.    HUN                      5.72%  9,176,196         17.74        +0.96       N/A
9     9             Assured Guaranty Ltd.    AGO                      5.54%  1,853,300         15.62        +0.82       N/A
10   10              Dollar General Corp.     DG                      5.36%  8,772,419         44.60        +2.27       N/A

也许您需要强制Volume转换为数字。

as.numeric(gsub("[^0-9.-]+","",dat[[1]]$Volume))
 [1] 12049183  1966749  2613174    30188  3054272    60052  1268468  9176196  1853300  8772419
于 2013-02-10T18:15:30.983 回答