R_Language 板


LINE

[问题类型]: 程式谘询(我想用R 做某件事情,但是我不知道要怎麽用R 写出来) [软体熟悉度]: 使用者(已经有用R 做过不少作品) [问题叙述]: 请简略描述你所要做的事情,或是这个程式的目的 问题一 用httr、XML套件想要把批踢踢的文章们存成.txt档以利後续text mining 但是八卦板因为有「确认已满18岁」的网页而无法存出文章 问题二 关於RCurl的问题(详见以下) 问题一 library(XML) library(httr) start <- regexpr('www', line)[1] end <- regexpr('html', line)[1] if(start != -1 & end != -1){ url <- substr(line, start, end+3) html <- content(GET(url), encoding="UTF8") doc <- xpathSApply(html, "//div[@id='main-content']", xmlValue) name <- strsplit(url, '/')[[1]][4] write(doc, gsub('html', 'txt', name)) } # 当 读入八卦板以外的批踢踢文章网址 line = "https://webptt.com/cn.aspx?n=bbs/StupidClown/M.1427811176.A.552.html" 工作路径中会出现一个新的txt档,其中存着这篇笨版文章的内容 # 当 读入八卦板网址 line = "https://webptt.com/cn.aspx?n=bbs/Gossiping/M.1427816656.A.450.html" 存下来的txt档里面却是空的。 研判应该是八卦板的十八岁限制网页造成 https://webptt.com/cn.aspx?n=bbs/Gossiping/M.1427816656.A.450.html 想请问版上高手如何跳过这个网页呢? 问题二 原程式码 url <- substr(line, start, end+3) html <- content(GET(url), encoding="UTF8") doc <- xpathSApply(html, "//div[@id='main-content']", xmlValue) name <- strsplit(url, '/')[[1]][4] write(doc, gsub('html', 'txt', name)) 原本想用RCurl套件来做第二行 html <- htmlParse(getURL(url), encoding='UTF-8') 存出来的html却失败 > html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr> <center>nginx</center> </body> </html> 但後来改成用httr套件的content()和GET()就可以了,却不明白为什麽XD --- 自己研究了一下,以上问题可能会触碰到 HTTP request header的范畴, 但我过去并没有学过这方面的知识 不知道版上各位大大有没有推荐的文章可以介绍自学呢? 非常感恩~~~~!! [环境叙述]: > sessionInfo() R version 3.1.2 (2014-10-31) Platform: x86_64-w64-mingw32/x64 (64-bit) --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 111.80.167.159
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1427881473.A.71F.html ※ 编辑: miao2361 (111.80.167.159), 04/01/2015 17:59:17
1F:推 andrew43: 把 over18 这个 cookie 的值写入 1 应该就可以了。 04/01 18:52
2F:推 andrew43: 不过我不了解在 R 这端怎麽做。 04/01 18:52
3F:→ miao2361: 恩恩~有看过python的code这样写,但R不知如何解... 04/01 19:13
4F:推 celestialgod: http://tinyurl.com/pn8cvwj Rcurl有option可以设定 04/01 20:04
5F:推 celestialgod: ,请参考rcurl的website,目前人在外面,不方便测 04/01 20:04
6F:推 celestialgod: 试,抱歉。 04/01 20:04
7F:→ celestialgod: rcurl website: http://tinyurl.com/o3ckae3 04/01 20:06
8F:→ miao2361: 非常感谢楼上!解开了,比想像中简单太多XD 程式码如下 04/08 17:54
9F:→ miao2361: GET(url, config=set_cookies("over18"="1"),...)即可 04/08 17:54
10F:→ celestialgod: 不客气,谢谢您的回覆 04/08 20:17







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP