2013-02-10 50 views
0

我有一个问题,导入包含一个表使用(我怀疑)JavaScript填充的网站的HTML。如果我将网站保存为我的HD上的源代码,我可以使用R中的XML包轻松做我想做的事情;如果我尝试直接使用R XML包没有得到页面上的所有信息

doc <- htmlTreeParse(url) 

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

我已经能够得到完整的HTML Python和使用

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

但我宁愿坚持使用R.

我一直在插科打诨的网站的urllib2库是http://bigcharts.marketwatch.com/reports/bigmovers.asp?date=20130104&data=1&start=1&report=1&report_country_code=US

谢谢。

+1

'如何'readHTMLTable(url,stringsAsFactors = FALSE)[[1]]' – GSee 2013-02-10 17:52:10

+0

太好了,谢谢。 – 2013-02-10 19:54:00

回答

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 
+0

为什么downvote?这个答案有问题吗? – agstudy 2013-02-10 18:19:37

+0

+1适用于我所描述的... – Ben 2013-02-10 19:44:21

+0

我希望我没有意外地低估了答案,因为我当然不是故意的。作品应该是完美的。 – 2013-02-10 19:55:42