使用带有grecaptcharesponse argumen的POST提交表单

2024-10-01 00:23:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从以下网页提交表单: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的响应。这部分工作得很好,正如我所料。反应如下:

^{pr2}$

之后,我要提交表格。这是我做不到的部分。在

我试着给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函数)。在


Tags: keyapiidhttpurlnetresponsewww
1条回答
网友
1楼 · 发布于 2024-10-01 00:23:35

如果检查html,您将看到表单的操作是../cgi-bin/statos_OIB.cgi,这意味着表单被提交到http://www.hzzo-net.hr/cgi-bin/statos_OIB.cgi,因此必须使用该url。在

另外,经过一些测试后,我发现服务器返回500个响应,除非提供了有效的referer(http://www.hzzo-net.hr/statos_OIB.htm)。在

我不熟悉R,但我可以用Python提供一个示例,使用^{}库。在

import requests

url = "http://www.hzzo-net.hr/cgi-bin/statos_OIB.cgi"
hzzo_response = 'your token'
data = {
    'upoib': '93335620125', 
    'g-recaptcha-response': hzzo_response
}
headers = {'referer': 'http://www.hzzo-net.hr/statos_OIB.htm'}
r = requests.post(url, data=data, headers=headers)
html = r.text

print(html)

在研究了httr文档之后,我设法在R中“翻译”了上述代码。如果提供了有效的令牌,代码将生成正确的结果。在

^{pr2}$

相关问题 更多 >