Python Can';t在Scrapy中使用带有用户名和密码的API

Python Can';t在Scrapy中使用带有用户名和密码的API,python,curl,scrapy,scrapy-spider,xe-api,Python,Curl,Scrapy,Scrapy Spider,Xe Api,这个卷发有效 https://<user>:<pass>@xecdapi.xe.com/v1/convert_from.json/?from=1000000&to=SGD&amount=AED,AUD,BDT&inverse=True https://:@xecdapi.xe.com/v1/convert_from.json/?from=1000000&to=SGD&amount=AED、AUD、BDT&inverse=True 但是这个卑鄙的

这个卷发有效

https://<user>:<pass>@xecdapi.xe.com/v1/convert_from.json/?from=1000000&to=SGD&amount=AED,AUD,BDT&inverse=True
https://:@xecdapi.xe.com/v1/convert_from.json/?from=1000000&to=SGD&amount=AED、AUD、BDT&inverse=True
但是这个卑鄙的要求不起作用

    yield scrapy.Request("https://<user>:<pass>@xecdapi.xe.com/v1/convert_from.json/?from=1000000&to=SGD&amount=AED,AUD,BDT&inverse=True")

It returns this error:

Traceback (most recent call last):
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\twisted\internet\defer.py", line 1297, in _inlineCallbacks
    result = result.throwExceptionIntoGenerator(g)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\twisted\python\failure.py", line 389, in throwExceptionIntoGenerator
    return g.throw(self.type, self.value, self.tb)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\scrapy\core\downloader\middleware.py", line 43, in process_request
    defer.returnValue((yield download_func(request=request,spider=spider)))
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\scrapy\utils\defer.py", line 45, in mustbe_deferred
    result = f(*args, **kw)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\scrapy\core\downloader\handlers\__init__.py", line 65, in download_request
    return handler.download_request(request, spider)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\scrapy\core\downloader\handlers\http11.py", line 61, in download_request
    return agent.download_request(request)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\scrapy\core\downloader\handlers\http11.py", line 286, in download_request
    method, to_bytes(url, encoding='ascii'), headers, bodyproducer)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\twisted\web\client.py", line 1596, in request
    endpoint = self._getEndpoint(parsedURI)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\twisted\web\client.py", line 1580, in _getEndpoint
    return self._endpointFactory.endpointForURI(uri)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\twisted\web\client.py", line 1456, in endpointForURI
    uri.port)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\scrapy\core\downloader\contextfactory.py", line 59, in creatorForNetloc
    return ScrapyClientTLSOptions(hostname.decode("ascii"), self.getContext())
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\twisted\internet\_sslverify.py", line 1201, in __init__
    self._hostnameBytes = _idnaBytes(hostname)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\twisted\internet\_sslverify.py", line 87, in _idnaBytes
    return idna.encode(text)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\idna\core.py", line 355, in encode
    result.append(alabel(label))
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\idna\core.py", line 276, in alabel
    check_label(label)
  File "d:\kerja\hit\python~1\<project_name>\<project_name>\lib\site-packages\idna\core.py", line 253, in check_label
    raise InvalidCodepoint('Codepoint {0} at position {1} of {2} not allowed'.format(_unot(cp_value), pos+1, repr(label)))
InvalidCodepoint: Codepoint U+003A at position 28 of u'xxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxx@xecdapi' not allowed
yield scrapy.Request(“https://:@xecdapi.xe.com/v1/convert_from.json/?from=1000000&to=SGD&amount=AED、AUD、BDT&inverse=True”)
它返回以下错误:
回溯(最近一次呼叫最后一次):
文件“d:\kerja\hit\python~1\\\lib\site packages\twisted\internet\defer.py”,第1297行,在\u inlineCallbacks中
结果=结果。通过ExceptionToGenerator(g)
文件“d:\kerja\hit\python~1\\\lib\site packages\twisted\python\failure.py”,第389行,位于ThroweExceptionToGenerator中
返回g.throw(self.type、self.value、self.tb)
文件“d:\kerja\hit\python~1\\\lib\site packages\scrapy\core\downloader\middleware.py”,第43行,进程中\u请求
defer.returnValue((yield download_func(request=request,spider=spider)))
文件“d:\kerja\hit\python~1\\\lib\site packages\scrapy\utils\defer.py”,第45行,必须延迟
结果=f(*参数,**kw)
文件“d:\kerja\hit\python~1\\\lib\site packages\scrapy\core\downloader\handlers\\uuuu init\uuu.py”,第65行,在下载请求中
返回处理程序。下载\u请求(请求,spider)
下载请求中的文件“d:\kerja\hit\python~1\\\lib\site packages\scrapy\core\downloader\handlers\http11.py”,第61行
返回代理。下载请求(请求)
下载请求中的文件“d:\kerja\hit\python~1\\\lib\site packages\scrapy\core\downloader\handlers\http11.py”,第286行
方法,到字节(url,encoding='ascii'),头,bodyproducer)
请求中的文件“d:\kerja\hit\python~1\\\lib\site packages\twisted\web\client.py”,第1596行
endpoint=self.\u getEndpoint(parsedURI)
文件“d:\kerja\hit\python~1\\\lib\site packages\twisted\web\client.py”,第1580行,位于\u getEndpoint中
返回self.\u endpointFactory.endpointForURI(uri)
endpointForURI中的文件“d:\kerja\hit\python~1\\\lib\site packages\twisted\web\client.py”,第1456行
uri.port)
文件“d:\kerja\hit\python~1\\\lib\site packages\scrapy\core\downloader\contextfactory.py”,第59行,位于creatorForNetloc中
返回ScrapyClientTLSOptions(hostname.decode(“ascii”)、self.getContext()
文件“d:\kerja\hit\python~1\\\lib\site packages\twisted\internet\\u sslverify.py”,第1201行,在uu init中__
self.\u hostnameBytes=\u idnaBytes(主机名)
文件“d:\kerja\hit\python~1\\\lib\site packages\twisted\internet\\u sslverify.py”,第87行,以字节为单位
返回idna.encode(文本)
文件“d:\kerja\hit\python~1\\\lib\site packages\idna\core.py”,第355行,编码
结果.追加(阿拉贝尔(标签))
文件“d:\kerja\hit\python~1\\\lib\site packages\idna\core.py”,第276行,阿拉伯文
检查标签(标签)
文件“d:\kerja\hit\python~1\\\lib\site packages\idna\core.py”,第253行,在check\u标签中
raise InvalidCodepoint({2}的位置{1}处的代码点{0}不允许)。格式(_unot(cp_值),pos+1,repr(标签)))
InvalidCodepoint:U'xxxxxxxxxxxxxxxxxxxxxxxxxxxx位置28处的代码点U+003A:xxxxxxxxxxxxxxxxxxxxxxxxxxx@xecdapi“不允许

Scrapy不支持通过URL进行HTTP身份验证。我们必须改用httpauth中间件

settings.py
中:

DOWNLOADER_MIDDLEWARES = {
    'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware': 811,
}
在蜘蛛网中:

from scrapy.spiders import CrawlSpider

class SomeIntranetSiteSpider(CrawlSpider):

    http_user = 'someuser'
    http_pass = 'somepass'
    name = 'intranet.example.com'

    # .. rest of the spider code omitted ...

您在第二个url中留下了凭据。代码500表示服务器在处理您的请求时遇到错误,所以出现了问题。我更新了我的问题。之前,我没有关闭CrawleraNote,这里有一个OpenPull请求,实现了从URL读取凭据: