作者celestialgod (天)
看板R_Language
标题Re: [问题] 爬虫抓取资料问题
时间Sun Mar 19 01:38:27 2017
※ 引述《ya32347844 (虚虚乐)》之铭言:
:
:
: [问题类型]:网路爬虫
:
:
: [软体熟悉度]:入门
:
: [问题叙述]:
: 我想要抓这个网页的资料 但不知道是不是Xpath写错了 我到後来抓到的资料是NULL
: 恳请各位大神给予指教 如果有需要补充的资讯也请不吝指出
: 已经google过相关讯息 用不同的package但结果相同 所以才会觉得是不是一层一层的Tag
: 写错了
: Update Code:
: myheader <- c(
: "User-Agent"="Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0_1 like Mac OS X; ja-jp) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A306 Safari/6531.22.7",
: "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
: "Accept-Language"="en-us",
: "Connection"="keep-alive",
: "Accept-Charset"="GB2312,utf-8;q=0.7,*;q=0.7"
: )
: #加上myheader
: d <- debugGatherer()
: get_url <- getURL(url, httpheader = myheader, debugfunction = d$update, verbose = T)
: get_url_parse = htmlTreeParse(get_url, encoding = "UTF-8", error=function(...){}, useInternalNodes = TRUE,trim=TRUE)
: cat(d$value()[3])
: node<-getNodeSet(get_url_parse, "//div[@class='page-content-wrapper']")
: info<-sapply(node,xmlValue)
: info
:
: [程式范例]:
:
: library(XML)
: library(RCurl)
: url="https://www.eex.com/en/market-data/environmental-markets/spot-market/european-emission-allowances#!/2017/01/04"
: get_url = getURL(url,encoding = "UTF-8")
: #将url解析
: get_url_parse = htmlParse(get_url, encoding = "UTF-8")
: tablehead <- xpathSApply(get_url_parse, "//div[@id='content']/section[@class='clearfix']/article[@id='market_data']/div[@id='content']/div/div/div/div",xmlValue)
:
: [环境叙述]:
:
: mac10.12.2
:
: [关键字]:
:
:
先用浏览器的开发者工具看一下网页回传的资料
会发现它资料是用JSON回传,也就是说
我们只要抓到那个JSON就可以拿到资料了 => 就不用去parsing HTML了
http://imgur.com/a/ThtWH
code:
http://pastebin.com/Zt5j8yue
至於你的code应该是少了cookie的setup
--
R资料整理套件系列文:
magrittr #1LhSWhpH (R_Language) https://goo.gl/72l1m9
data.table #1LhW7Tvj (R_Language) https://goo.gl/PZa6Ue
dplyr(上.下) #1LhpJCfB,#1Lhw8b-s (R_Language) https://goo.gl/I5xX9b
tidyr #1Liqls1R (R_Language) https://goo.gl/i7yzAz
pipeR #1NXESRm5 (R_Language) https://goo.gl/zRUISx
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.233.137.157
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1489858711.A.903.html
※ 编辑: celestialgod (36.233.137.157), 03/19/2017 01:40:00
1F:推 ya32347844: 感谢!这麽详细的解说 其实看出来资料是用Json後 简单 03/19 19:14
2F:→ ya32347844: 很多 果然要抓看看不同网站 才会发现自己缺少哪块知识 03/19 19:15