python - Scrapy,验证码登录失败

标签 python authentication scrapy captcha

我正在使用以下蜘蛛来抓取需要身份验证的 tinyz.us 网站。

from scrapy.spiders import BaseSpider
from scrapy.http import FormRequest
import urllib2


class Start(BaseSpider):
    name = 'test'
    start_urls = ["http://tinyz.us"]

    def parse(self, response):

        user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        headers = {'User-Agent': user_agent}
        imgRequest = urllib2.Request("http://tinyz.us/securimage/securimage_show.php", headers=headers)
        imgData = urllib2.urlopen(imgRequest).read()

        with open('captcha.png', 'wb') as f:
            f.write(imgData)

        captcha = raw_input("-----> Enter the captcha in manually :")

        return FormRequest.from_response(
            response=response,
            formdata={"login_user": "myusername",
                      "login_password": "mypass",
                      "captcha_code": captcha},
            formxpath="//*[@id='login-form']",
            callback=self.after_login,
            headers=headers)

    def after_login(self, response):
        print("AFTER LOGIN")
        with open('response.html', 'w') as f:
            f.write(response.body)

该网站使用恒定的 URL 来生成验证码,并且似乎每次都会生成一个新的 URL。我不熟悉相应的技术,但我倾向于解决这个问题的方法是保存验证码并手动传递它。

问题是它总是返回失败的响应,我不确定问题是因为 scrapy 将数据传递到 form 的方式还是因为验证码,我可以'找不到正确调试蜘蛛的方法。

最佳答案

好吧,这里的问题是验证码图像需要从实际响应中接收cookie,并且您正在使用urllib2来发出验证码请求,因此Scrapy默认情况下不处理该请求.

使用 scrapy 请求来检查验证码,例如:

def parse(self, response):
    yield Request(url="http://tinyz.us/securimage/securimage_show.php", callback=self.parse_captcha, meta={'previous_response': response})

def parse_captcha(self, response):
    with open('captcha.png', 'wb') as f:
        f.write(response.body)

    captcha = raw_input("-----> Enter the captcha in manually :")

    return FormRequest.from_response(
        response=response.meta['previous_response'],
        formdata={"login_user": "myusername",
                  "login_password": "mypass",
                  "captcha_code": captcha},
        formxpath="//*[@id='login-form']",
        callback=self.after_login)

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

相关文章:

python - Gensim Doc2Vec most_similar() 方法没有按预期工作

python - 转移概率矩阵

python - 使用python + boto删除未使用的AWS安全组,引发vpc错误

javascript - 处理多个选项卡上的 session

python - Scrapy CSS 选择器忽略标签并仅获取文本

python - 在 python 中使用列表创建 json 数组

javascript - 在跨站点域中自动登录

asp.net-mvc - 是否可以使用 ASP.NET MVC 创建登录系统但不使用成员资格提供程序?

python - Scrapy xpath 在 python 中返回空列表

python - 如何从无限滚动网站上抓取所有内容?抓取