RCurl爬虫抓取验证码例子

本文介绍了使用RCurl包从指定网站抓取验证码的过程。通过设置HTTP头部信息来模拟浏览器行为,并利用循环抓取多个验证码。

抓取验证码

#1)抓取验证的流程
#①打开网页到验证码的位置

#②鼠标左键拖动验证

#③得到验证的url:http://tjcredit.gov.cn/verifycode?date=1461146339377
#④最后,把此url加入到R程序中
#⑤循环抓取验证码即可

#2)抓取一个验证码
library(RCurl)
## Warning: package 'RCurl' was built under R version 3.2.3
## Loading required package: bitops
#伪装报头
myHttpheader <- c("Accept"="image/webp,*/*;q=0.8","Accept-Encoding"="gzip,deflate,sdch","Accept-Language"="zh-CN,zh;q=0.8","Cache-Control"="no-cache","Connection"="keep-alive","Pragma"="no-cache","Referer"="http://bbs.shangdu.com/s/regform.htm","User-Agent"="Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36")

#获取验证码的url
url <- "http://tjcredit.gov.cn/verifycode?date=1461146339377"
png <- getBinaryURL(url, httpheader=myHttpheader)
writeBin(png, con="E:\\新技术\\爬虫\\验证码/111.png")
## NULL
#3)抓取N个验证码
for(i in 1:10) {
  #url <- "http://drops.wooyun.org/wooyun/captcha.php?"
  url1 <- "http://tjcredit.gov.cn/verifycode?date=1461145953254"
  png <- getBinaryURL(url1)
  file_name <- paste("E:\\新技术\\爬虫\\验证码/", i, ".png", sep="")
  writeBin(png, file_name)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值