Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Scrapy,使用验证码登录失败_Python_Authentication_Login_Scrapy_Captcha - Fatal编程技术网

Python Scrapy,使用验证码登录失败

Python Scrapy,使用验证码登录失败,python,authentication,login,scrapy,captcha,Python,Authentication,Login,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_a

我正在使用下面的爬行器来抓取需要身份验证的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来生成验证码,似乎每次都会生成一个新的验证码。我不熟悉各自的技术,但我倾向于通过保存验证码并手动传递来解决这个问题


问题是它总是返回失败的响应,我不确定问题是因为scrapy将数据传递到
表单的方式,还是因为验证码,我无法找到正确调试爬行器的方法。

好的,这里的问题是验证码图像需要从实际响应接收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)

那么,在手动输入字符后,我应该如何使用
验证码。您没有在
parse
函数中使用它。另外,
解析
中的
请求
是什么?对不起,我忘了从
解析
中删除不必要的部分,请检查更新的应答它引发了
文件“/usr/lib/python2.7/dist packages/twisted/internet/defer.py”,第588行,在_runcallbackscurrent.result=callback(current.result,*args,**kw)文件中“/usr/local/lib/python2.7/dist packages/scrapy/spider/uuu init_uuu.py”,第76行,在parse raise NotImplementedError
中,它说您的spider中没有
解析方法,也导入请求:
来自scrapy导入请求
是的,我还将meta更改为
response.meta
,它工作正常。谢谢!