gpt4 book ai didi

python - Scrapy & 验证码

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:01 29 4
gpt4 key购买 nike

我在站点中使用 scrapy 提交表单 https://www.barefootstudent.com/jobs (任何进入页面的链接等http://www.barefootstudent.com/los_angeles/jobs/full_time/full_time_nanny_needed_in_venice_217021)

我的 scapy 机器人成功登录,但我无法避免验证码。对于表单提交,我使用 scrapy.FormRequest.from_reponse

frq = scrapy.FormRequest.from_response(response, formdata={'message': 'itttttttt', 
'security': captcha, 'name': 'fx',
'category_id': '2', 'email': 'ololo%40gmail.com', 'item_id': '216640_2', 'location': '18', 'send_message': 'Send%20Message'
}, callback=self.afterForm)

yield frq

我想从此页面加载验证码图像,并手动输入脚本运行时。等等

captcha = raw_input("put captcha in manually>")  

我试试

 urllib.urlretrieve(captcha, "./captcha.jpg")

但是这个方法加载了不正确的验证码(网站拒绝我的输入)。我尝试在一个运行脚本中反复调用 urllib.urlretieve,每次他返回不同的验证码 :(

之后我尝试使用 ImagePipeline。但我的问题是,即使我使用 yeild,return item(下载图片)也只会在函数执行完毕后发生。

 item = BfsItem()
item['image_urls'] = [captcha]
yield item
captcha = raw_input("put captcha in manually>")
frq = scrapy.FormRequest.from_response(response, formdata={'message': 'itttttttt',
'security': captcha, 'name': 'fx',
'category_id': '2', 'email': 'ololo%40gmail.com', 'item_id': '216640_2', 'location': '18', 'send_message': 'Send%20Message'
}, callback=self.afterForm)
yield frq

当时,当我的脚本请求输入时,图片没有下载!

如何修改我的脚本并在手动输入验证码后调用 FormRequest?

非常感谢!

最佳答案

我使用的方法通常效果很好,看起来像这样(只是一个要点,您需要添加您的具体细节):

第 1 步 - 获取验证码 url(并保留表单的响应以备后用)

def parse_page_with_captcha(response):
captcha_url = response.xpath(...)
data_for_later = {'captcha_form': response} # store the response for later use
return Request(captcha_url, callback=self.parse_captcha_download, meta=data_for_later)

第 2 步 - 现在 scrapy 将下载图像,我们必须在 scrapy 回调中正确处理它

def parse_captcha_download(response):
captcha_target_filename = 'filename.png'
# save the image for processing
i = Image.open(StringIO(response.body))
i.save(captcha_target_filename)

# process the captcha (OCR, or sending it to a decaptcha service, etc ...)
captcha_text = solve_captcha(captcha_target_filename)

# and now we have all the data we need for building the form request
captcha_form = response.meta['captcha_form']

return scrapy.FormRequest.from_response(captcha_form, formdata={'message': 'itttttttt',
'security': captcha_text, 'name': 'fx',
'category_id': '2', 'email': 'ololo%40gmail.com', 'item_id': '216640_2', 'location': '18', 'send_message': 'Send%20Message'
}, callback=self.afterForm)

重要细节

Captcha 保护表单需要某种方式将验证码图像与看到并回答此验证码的特定用户/客户联系起来。这通常是使用基于 cookie 的 session 或隐藏在验证码表单中的特殊参数/图像标记来完成的。

爬虫代码必须小心不要破坏这个链接,否则它会回答一些验证码,而不是它必须回答的验证码。

为什么它不适用于 Verz1Lka 发布的两个示例?

urllib.urlretrieve 方法完全在 scrapy 之外工作。虽然这通常是一个坏主意(这不会利用 scrapy 调度等的好处),但这里的主要问题是:此请求将完全在目标站点用于跟踪的任何 session cookie、url 参数等之外工作验证码被发送到特定的浏览器。

另一方面,使用图像管道的方法在 Scrapy 的规则中运行良好,但这些图像下载计划在稍后完成,因此验证码下载在需要时将不可用。

关于python - Scrapy & 验证码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27948326/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com