草庐IT

xml - RCurl 无法下载 URL 内容

coder 2024-06-28 原文

页面下载失败。这是我收到的错误:

Error in which(value == defs) : 
  argument "code" is missing, with no default

这是我的代码:

require(RCurl)
require(XML)

ok <- "http://www.okcupid.com/match?filter1=0,34&filter2=2,22,40&filter3=3,5&filter4=5,3600&filter5=9,486&filter6=1,1&locid=4265540&lquery=San%20Francisco,%20California&timekey=1&matchOrderBy=MATCH&custom_search=0&fromWhoOnline=0&mygender=m&update_prefs=1&sort_type=0&sa=1&using_saved_search=&count=50"

okc <- getURL(ok, encoding="UTF-8") #Download the page
okcHTML <- htmlParse(okc, asText = TRUE, encoding = "utf-8")

最佳答案

如果您愿意生活在 Hadleyverse 的最前沿,rvest 可以很好地处理这个问题:

library(rvest)

ok_search <- "https://www.okcupid.com/match?filter1=0,34&filter2=2,22,40&filter3=3,5&filter4=5,3600&filter5=9,486&filter6=1,1&locid=4265540&lquery=San%20Francisco,%20California&timekey=1&matchOrderBy=MATCH&custom_search=0&fromWhoOnline=0&mygender=m&update_prefs=1&sort_type=0&sa=1&using_saved_search=&count=50"

pg <- html_session(ok_search)
pg %>% html_nodes("div.profile_info") %>% html_text()

##  [1] "  phenombom   32·San Francisco, CA  "        "  sylvea   24·San Francisco, CA  "          
##  [3] "  haafu   40·San Francisco, CA  "            "  Rebamania   31·San Francisco, CA  "       
##  [5] "  Brilikedacheese   26·San Francisco, CA  "  "  cloudhunteress   23·San Francisco, CA  "  
##  [7] "  Lizzieisdizzy   28·San Francisco, CA  "    "  liddybird80   34·San Francisco, CA  "     
##  [9] "  wander_found   32·San Francisco, CA  "     "  Crunchyisinabox   31·San Francisco, CA  " 
...

我将探讨为什么直接 RCurl(rvest 包装 RCurl)不起作用。

更新

更深一层并使用 httr(另一个 RCurl 抽象):

library(httr)
library(XML)

res <- GET(ok_search)
ok_html <- content(res, as="parsed")
xpathSApply(ok_html, "//div[@class='profile_info']", xmlValue)

它返回与上面相同的结果,所以它也工作正常。

更新/已解决

library(RCurl)
library(XML)

okc <- getURL(ok,  followlocation=TRUE)
ok_html <- htmlParse(okc)
xpathSApply(ok_html , "//div[@class='profile_info']", xmlValue)

您需要添加followlocation=TRUE。原始 URL 导致 302 响应(服务器正在发送重定向)并且 RCurl 默认情况下不会遵循这些响应,但似乎 httrrvest 确保默认设置参数。

您可以在 getURL 上使用 verbose=TRUE 参数将响应视为控制台消息:

## * Adding handle: conn: 0x114ade000
## * Adding handle: send: 0
## * Adding handle: recv: 0
## * Curl_addHandleToPipeline: length: 1
## * - Conn 12 (0x114ade000) send_pipe: 1, recv_pipe: 0
## * About to connect() to www.okcupid.com port 80 (#12)
## *   Trying 198.41.209.131...
## * Connected to www.okcupid.com (198.41.209.131) port 80 (#12)
## > GET /match?filter1=0,34&filter2=2,22,40&filter3=3,5&filter4=5,3600&filter5=9,486&filter6=1,1&locid=4265540&lquery=San%20Francisco,%20California&timekey=1&matchOrderBy=MATCH&custom_search=0&fromWhoOnline=0&mygender=m&update_prefs=1&sort_type=0&sa=1&using_saved_search=&count=50 HTTP/1.1
## User-Agent: curl/7.30.0 Rcurl/1.95.4.3
## Host: www.okcupid.com
## Accept: */*
## 
## < HTTP/1.1 302
## < Date: Mon, 20 Oct 2014 20:07:12 GMT
## < Content-Type: application/octet-stream
## < Transfer-Encoding: chunked
## < Connection: keep-alive
## < Set-Cookie: __cfduid=d0d55f2c9c990d97b0d02dba7148881741413835631999; expires=Mon, 23-Dec-2019 23:50:00 GMT; path=/; domain=.okcupid.com; HttpOnly
## < X-OKWS-Version: OKWS/3.1.30.2
## < Location: https://www.okcupid.com/match?filter1=0,34&filter2=2,22,40&filter3=3,5&filter4=5,3600&filter5=9,486&filter6=1,1&locid=4265540&lquery=San%20Francisco,%20California&timekey=1&matchOrderBy=MATCH&custom_search=0&fromWhoOnline=0&mygender=m&update_prefs=1&sort_type=0&sa=1&using_saved_search=&count=50
## < P3P: CP="NOI CURa ADMa DEVa TAIa OUR BUS IND UNI COM NAV INT", policyref="http://www.okcupid.com/w3c/p3p.xml"
## < X-XSS-Protection: 1; mode=block
## < Set-Cookie: guest=10834912674894888479; Expires=Tue, 20 Oct 2015 20:07:12 GMT; Path=/; Domain=okcupid.com; HttpOnly
## * Server cloudflare-nginx is not blacklisted
## < Server: cloudflare-nginx
## < CF-RAY: 17c7d71bf1880412-EWR
## < 
## * Connection #12 to host www.okcupid.com left intact

它在调试此类问题时非常有用。您也可以将 verbose() 参数用于 httrrvest URL 检索函数。

关于xml - RCurl 无法下载 URL 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26473611/

有关xml - RCurl 无法下载 URL 内容的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  3. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  4. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  7. ruby-on-rails - rails : save file from URL and save it to Amazon S3 - 2

    从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex

  8. ruby - 如何使用 Ruby aws/s3 Gem 生成安全 URL 以从 s3 下载文件 - 2

    我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A

  9. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  10. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

随机推荐