r - 使用 R 配置随机代理进行抓取
问题描述
我抓取了一个授权抓取机器人规则的网站,但有时我被阻止了。
当我联系管理员了解原因时,我想了解如何在 R 中使用不同的代理来继续抓取而不会被阻止。
我遵循了这个快速教程: https: //support.rstudio.com/hc/en-us/articles/200488488-Configuring-R-to-Use-an-HTTP-or-HTTPS-Proxy
所以我编辑了环境文件:
file.edit('~/.Renviron')
在其中我插入了一个随机选择的代理列表:
proxies_list <- c("128.199.109.241:8080","113.53.230.195:3128","125.141.200.53:80","125.141.200.14:80","128.199.200.112:138","149.56.123.99:3128","128.199.200.112:80","125.141.200.39:80","134.213.29.202:4444")
proxy <-paste0('https://', sample(proxies_list, 1))
https_proxy=proxy
但是当我使用这段代码时:
download.file(url_proxy, destfile ='output.html',quiet = TRUE)
html_output <- read_html('output.html')
我一直被屏蔽。
我没有正确设置代理吗?
谢谢 !M。
解决方案
您需要设置环境变量,而不是 R 变量。有关?download.file
更多详细信息,请参阅。
例如
Sys.setenv(http_proxy=proxy)
在其他任何事情发生之前。另请注意文档中的警告:
These environment variables must be set before the download code is
first used: they cannot be altered later by calling 'Sys.setenv'.
推荐阅读
- c - 当给出的唯一信息是后序遍历时,如何构造严格的二叉树?
- mysql - 为什么我得到错误 updated_at cannot be null activerecord rails 5.2 mysql 5.7?
- r - 使用crossing和sample_n对多个不同的样本大小进行采样以创建单个df
- java - 将字符串存储到带有出现的哈希图中
- javascript - 如何使用 lodash 将两个数组(数组 1d 和数组 2d 对象)合并为一个数组
- android - RecyclerView中的ViewPager:Viewpager拦截父View的点击
- python - 熊猫根据条件创建列
- redux-form - 即使使用 initialValues 设置,React Redux Form 也无法通过所需的验证
- python-2.7 - HTTP 响应代码错误地出现在实际为 200 的位置
- javascript - SCSS data-theme="dark" 变量不加载