scrapy - 下载页面不返回状态码
问题描述
我发现我需要下载的页面在返回的标头中不包含 http 状态代码。我得到了错误:ParseError: ('non-integer status code', b'Tag: "14cc1-5a76434e32f9e"')
这显然是准确的。但否则返回的数据是完整的。
我只是想在回调中手动保存页面内容:afilehandle.write(response.body)
某种东西。这是一个pdf。有没有办法可以绕过这个并仍然获取页面的内容?
返回的示例也使提琴手崩溃。标题中的第一件事是标签。
Tag: "14cc1-5a76434e32f9
e"..Accept-Ranges: bytes
..Content-Length: 85185.
.Keep-Alive: timeout=15,
max=100..Connection: Ke
ep-Alive..Content-Type:
application/pdf....%PDF-
1.4.%ÓôÌá.1 0 obj.<<./Cr
eationDate(D:20200606000
828-06'00')./Creator(PDF
sharp 1.50.4740 \(www.pd
fsharp.com\))./Producer(
PDFsharp 1.50.4740 \(www
.pdfsharp.com\)).>>.endo
bj.2 0 obj.<<./Type/Cata
log./Pages 3 0 R.>>.endo
bj.3 0 obj.<<./Type/Page
s./Count 2./Kids[4 0 R 8
0 R].>>.endobj.4 0 obj.
<<./Type/Page./MediaBox[
0 0 612 792]./Parent 3 0
R./Contents 5 0 R./Reso
urces.<<./ProcSet [/PDF/
Text/Ima.... etc
注意:对于任何不熟悉 PDF 文件结构的人%PDF-1.4
,以及之后的所有内容都是 PDF 文档的正确格式。即使有错误的标题,Chrome 也能很好地下载 PDF。
解决方案
最后,我直接修改文件twisted/web/_newclient.py
以不抛出错误,并使用我可以识别的奇怪状态代码:
def statusReceived(self, status):
parts = status.split(b' ', 2)
if len(parts) == 2:
version, codeBytes = parts
phrase = b""
elif len(parts) == 3:
version, codeBytes, phrase = parts
else:
raise ParseError(u"wrong number of parts", status)
try:
statusCode = int(codeBytes)
except ValueError:
# Changes were made here
version = b'HTTP/1.1' #just assume it is what it should be
statusCode = 5200 # deal with invalid status codes later
phrase = b'non-integer status code' # sure, pass on the error message
# and commented out the line below.
# raise ParseError(u"non-integer status code", status)
self.response = Response._construct(
self.parseVersion(version),
statusCode,
phrase,
self.headers,
self.transport,
self.request,
)
我将蜘蛛设置为接受该状态码。
class MySpider(Spider):
handle_httpstatus_list = [5200]
但是,最后我发现目标站点在通过 https 访问时表现正确,因此我最终回滚了上述所有更改。
请注意,在您更新库之前,上述 hack 将起作用,此时您需要重新应用 hack。但如果你绝望,它可能会完成它。
推荐阅读
- pytorch - 在 PyTorch 中用张量索引多维张量
- typescript - 在 Jest 中使用 toMatchObject 和 stringMatching(regexp)
- log4j2 - log4j2 appender 与 sql server 数据库对话
- micronaut - Micronaut HelloWorld 错误:意外错误:非法名称 .$HelloControllerDefinition
- unit-testing - 开玩笑:如何正确测试包含 promise 的 void 函数?
- brightway - Brightway2:“聚合 LCI”ecoinvent 版本和“unit”ecoinvent 版本之间的计算时间差异
- mongodb - 如何使用 MongoDb 聚合获取不匹配的记录值
- sql - SQL 事务自动回滚
- jenkins - 在 jenkins 中配置 LDAP 全局设置
- sql-server - 旧的 SQL 文件仍在使用中