[英]RCurl handle Chinese characters
這是我的代碼。 為什么不能正確解碼漢字:
library(XML)
require(RCurl)
myURL <- "http://data.eastmoney.com/zjlx/600066.html"
html <- getURL(myURL,.encoding = "gb2312")
print(Encoding(html))
basicInfo <- htmlParse(html)
#print(Encoding(basicInfo))
tables <- readHTMLTable(basicInfo)
問題是網站使用javascript動態地向表添加數據。 如果您在禁用js的瀏覽器中加載頁面,您會發現您也沒有看到任何數據。
我在javascript方面取得了一些有限的成功,但沒有編碼(當將字符編碼提供給R時,我不知道如何糾正字符編碼有問題):
# On Windows install the packages and required files
require(devtools)
install_github('seleniumJars', 'LluisRamon')
install_github('relenium', 'LluisRamon')
# Load package
require(relenium)
# Start a new instance of Firefox (this must already be installed on your computer)
firefox <- firefoxClass$new()
# We go to the url using the function get.
firefox$get("http://data.eastmoney.com/zjlx/600066.html")
# The html from the webpage can be obtained with the getPageSource (and returning a "character") function.
html <- firefox$getPageSource()
# Parse the html using the XML package
doc <- htmlParse(html)
# Extract your table
tables <- readHTMLTable(doc, stringsAsFactors=FALSE)
mytable <- tables$dt_1
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13
1 2014-07-24 18.30 2.81% 3893万 10.63% -323万 -0.88% 4217万 11.52% -1600万 -4.37% -2293万 -6.26%
2 2014-07-23 17.80 -0.50% 1287万 8.63% 27.48万 0.18% 1259万 8.44% -333万 -2.24% -953万 -6.39%
3 2014-07-22 17.89 4.25% 7765万 18.46% 5729万 13.62% 2036万 4.84% -4574万 -10.87% -3190万 -7.58%
我不知道stringi包在這種情況下是否有幫助。 它可能在Linux下工作(我經常發現Linux中的文本編碼問題遠遠少於Windows,但這是軼事)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.