我想从以下网页提交表格:http ://www.hzzo-net.hr/statos_OIB.htm
首先,我使用 2captcha 服务绕过 recaptcha:
# parameters
api_key <- "c+++"
api_url <- "http://2captcha.com/in.php"
site_key <- "6Lc3SAgUAAAAALFnYxUbXlcJ8I9grvAPC6LFTKQs"
hzzo_url <- "http://www.hzzo-net.hr/statos_OIB.htm"
# GET method
req_url <- paste0("http://2captcha.com/in.php?key=", api_key,"&method=userrecaptcha&googlekey=",
site_key, "&pageurl=", hzzo_url)
get_response <- POST(req_url)
hzzo_content <- content(get_response)
hzzo_content <- xml_text(hzzo_content)
captcha_id <- stringr::str_extract_all(hzzo_content[[1]], "\\d+")[[1]]
# solve captcha
Sys.sleep(16L)
captcha2_solve <- function(apiKey, capstchaID){
req_url <- paste0("http://2captcha.com/res.php?key=", api_key,"&action=get&id=", capstchaID)
result <- GET(req_url)
captcha_content <- content(result)
hzzo_response <- xml_text(captcha_content)
hzzo_response <- strsplit(hzzo_response, "\\|")
return(hzzo_response)
# hzzo_response <- hzzo_response[[1]][[2]]
# return(hzzo_response)
}
hzzo_response <- captcha2_solve(api_key, captcha_id)
while(hzzo_response[[1]] == "CAPCHA_NOT_READY"){
Sys.sleep(16L)
hzzo_response <- captcha2_solve(api_key, captcha_id)
return(hzzo_response)
}
hzzo_response <- hzzo_response[[1]][[2]]
执行此代码后,我得到了输入到 recaptcha 的 textarea 的响应。正如我所料,这部分工作正常。响应如下所示:
"03AHqfIOmo9BlCsCKyg-lDes4oW-U3PWgCtATRUqXFcEV032acDgGoOzrV8GiZNDzCF4TbCVLcY8HZ8hR1JqO11YdRExvgPDL0EUsjCZdI0rUm_LnBRRifyb66X7V6r4n8CIm1si3EKmw36XIcZK7MGrHSNWRrj2aGzWAYO8ceobViOICOhkYe9Bsfv64tUHWvHSqNIoesD_FHplbWG3B0eMag5341NyycjpNLxgNCwVzA8mhCU3oQUcloze-mIclFMZ7J_nbVhXdy8-qipF5ZFH4xIhSQXHH-TqxyaGQFjKdgLch7MuDEQVRcQGo1o4QuSEoeCTjlPn3Mah5vC8zKrnqfbMgiOVOIDJFGvFY4KOivbBzYTz5nW9g"
之后,我应该提交表格。这是我无法正确处理的部分。
我试图将所有参数添加到 POST:
parameters <- list(
'upoib' = "93335620125", # example of number to enter
'g-recaptcha-response' = hzzo_response
)
test <- POST(
"http://www.hzzo-net.hr/statos_OIB.htm",
body = toJSON(parameters),
encode = "json",
verbose()
)
但这只是给了我初始页面。
如果我有 recaptcha 响应变量,如何提交表单?是否可以使用 httr 包提交,或者我必须使用 Selenium。代码可以是 R 或 Python(只需要最后一部分,POST 函数)。