我尝试在子域等中抓取包含下划线的页面:https://taxi-3-extreme-rush_1.en.softonic.com
我检查规格,我发现子域名可以包含下划线。 我也尝试使用link.encode(' idna'),但也不行。
我有错误:
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/twisted/internet/defer.py", line 1297, in _inlineCallbacks
result = result.throwExceptionIntoGenerator(g)
File "/usr/lib64/python2.7/site-packages/twisted/python/failure.py", line 389, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
File "/usr/lib64/python2.7/site-packages/scrapy/core/downloader/middleware.py", line 43, in process_request
defer.returnValue((yield download_func(request=request,spider=spider)))
File "/usr/lib64/python2.7/site-packages/scrapy/utils/defer.py", line 45, in mustbe_deferred
result = f(*args, **kw)
File "/usr/lib64/python2.7/site-packages/scrapy/core/downloader/handlers/__init__.py", line 65, in download_request
return handler.download_request(request, spider)
File "/usr/lib64/python2.7/site-packages/scrapy/core/downloader/handlers/http11.py", line 60, in download_request
return agent.download_request(request)
File "/usr/lib64/python2.7/site-packages/scrapy/core/downloader/handlers/http11.py", line 285, in download_request
method, to_bytes(url, encoding='ascii'), headers, bodyproducer)
File "/usr/lib64/python2.7/site-packages/twisted/web/client.py", line 1596, in request
endpoint = self._getEndpoint(parsedURI)
File "/usr/lib64/python2.7/site-packages/twisted/web/client.py", line 1580, in _getEndpoint
return self._endpointFactory.endpointForURI(uri)
File "/usr/lib64/python2.7/site-packages/twisted/web/client.py", line 1456, in endpointForURI
uri.port)
File "/usr/lib64/python2.7/site-packages/scrapy/core/downloader/contextfactory.py", line 59, in creatorForNetloc
return ScrapyClientTLSOptions(hostname.decode("ascii"), self.getContext())
File "/usr/lib64/python2.7/site-packages/twisted/internet/_sslverify.py", line 1201, in __init__
self._hostnameBytes = _idnaBytes(hostname)
File "/usr/lib64/python2.7/site-packages/twisted/internet/_sslverify.py", line 87, in _idnaBytes
return idna.encode(text)
File "/usr/lib/python2.7/site-packages/idna/core.py", line 355, in encode
result.append(alabel(label))
File "/usr/lib/python2.7/site-packages/idna/core.py", line 276, in alabel
check_label(label)
File "/usr/lib/python2.7/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+005F at position 20 of u'taxi-3-extreme-rush_1' not allowed
答案 0 :(得分:1)
好像它是Twisted的一个问题。
关于它的解决方案存在问题here:
查看Twisted的代码,它将使用idna库(如果可用)。如果我点击卸载idna并再次发出相同的请求,则表示成功。
使用pip install twisted [tls]或pip install treq安装idna。
我尝试通过pip uninstall idna
卸载idna,确实是请求通过了。
答案 1 :(得分:0)
解决方法:
import idna
idna.idnadata.codepoint_classes['PVALID'] = tuple(
sorted(list(idna.idnadata.codepoint_classes['PVALID']) + [0x5f0000005f])
)
答案 2 :(得分:0)
我尝试使用Selenium,它可以正确解析。我可以验证这一点,因为如果禁用了蜘蛛的中间件(我的Selenium代码所在的位置)。抛出相同的错误。
raise InvalidCodepoint('Codepoint {0} at position {1} of {2} not allowed'.format(_unot(cp_value), pos+1, repr(label)))
idna.core.InvalidCodepoint: Codepoint U+005F at position 3 of 'xyx_abc' not allowed