【发布时间】:2020-09-24 04:23:33
【问题描述】:
我正在尝试使用 requests.post 从网页获取帐号
在大约 5 个帖子查询后,该网站请求验证码(post answer = {"captcha_needed": true, "error": "captcha-check-failed"})
如何简单地点击验证码框?我尝试使用 fake_useragent 来避免验证码,也尝试使用 selenium 和 chrome 网络驱动程序,但没有帮助。这是我的代码:
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 web 驱动程序时,它需要额外的图片选择。我想使用其他东西可以解决问题。这是验证码框的屏幕截图。
我希望以前有人遇到过类似的问题并且可以帮助我。
【问题讨论】:
-
我如何简单地点击验证码框? 你不能像页面上的任何其他元素那样与之交互吗?相关:stackoverflow.com/questions/58872451/….
标签: python selenium-webdriver python-requests