我正在尝试使用request.post从网页获取帐号
大约5个帖子查询后,该网站正在请求验证码(帖子答案= {““ captcha_needed”:是,“错误”:“ captcha-check-failed”))
如何简单地单击验证码框?我尝试过用fake_useragent来避免验证码,我也尝试过使用selenium和chrome web驱动程序,但这没有帮助。这是我的代码:
class Account(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.servers = {'red': 1, 'green': 2, 'blue': 3, 'lime': 4}
self.config = configparser.ConfigParser()
self.config.read('config.ini')
@commands.command()
async def account(self, ctx, nickname):
for allowed in eval(self.config['account']['access']):
if ctx.message.author.id != allowed:
flag = False
else:
flag = True
break
if flag:
serverName = 'red'
server = str(serverName).lower()
server_id = self.servers[server]
lenght = len(nickname) + 61
ua = UserAgent()
userAgent = ua.random
headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
'Connection': 'keep-alive',
'Content-Length': str(lenght),
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': '_ym_uid=155880155795497451; _ga=GA1.2.1836303506.1558801557; arp_ab=ff4d30d27222533a8a483bd63b1d7e; _ym_d=1589195755; csrftoken=QVHCsDlmUM2NSm18Xiw3QHexjMYrbbDMFITFBd4eV5hJJQJUzBjoiVQQIOEf9STZ; _gid=GA1.2.525781881.1591250700; _ym_isad=1; _ym_visorc_8171623=w; sessionid=n8b07bhdedsqt3m0swcap60avntvn6qi',
'Host': 'www.advance-rp.ru',
'Origin': 'https://www.advance-rp.ru',
'Referer': 'https://www.advance-rp.ru/donate/',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': userAgent,
'X-Requested-With': 'XMLHttpRequest'
}
payload = {
'g-recaptcha-response': '',
'sum': str(random.randint(1, 1000)),
'account': nickname,
'service': 'unitpay',
'server': str(server_id)
}
ans = requests.post('https://www.advance-rp.ru/donate/request/do/', data = payload, headers = headers)
print(ans.text)
if ans.text == '{"captcha_needed": true, "error": "captcha-check-failed"}':
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://www.advance-rp.ru/donate/')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='recaptcha-anchor']"))).click()
ans = requests.post('https://www.advance-rp.ru/donate/request/do/', data = payload, headers = headers)
result = ans.text.split(',')
result.reverse()
account_id_field = result[0]
account_id_field = '{' + account_id_field
account_id = eval(account_id_field)
我正在创建Discord机器人,因此不理会与其相关的所有代码。主要问题是,即使我使用chrome Web驱动程序解决了问题,也不确定它是否可以在托管服务(例如,heroku)上运行。当我尝试手动获取此页面时,我可以简单地单击验证码框并进行浏览,但是当我尝试使用chrome网络驱动程序时,它需要其他图片选择。我想使用其他方法可以解决问题。这是一个验证码框的屏幕截图。
我希望有人以前也遇到过类似的问题,可以为我提供帮助。
答案 0 :(得分:0)
由于iframe位于其他域上,因此您还必须使用“ --disable-web-security”启动chrome:
opts = Options()
opts.add_argument("--disable-web-security")
driver = webdriver.Chrome(options=opts)
然后切换到iframe以单击:
iframe = driver.find_element_by_css_selector('iframe[src*="api2"]')
driver.switch_to.frame(iframe)
driver.execute_script("""
document.querySelector('.rc-anchor-checkbox').click()
""")
您可能需要调整该CSS。