【发布时间】:2021-07-29 18:15:30
【问题描述】:
我正在尝试抓取特定网站。我用来抓取它的代码与成功抓取许多其他网站的代码相同。
但是,生成的 response.body 看起来完全损坏(下面的部分):
����)/A���(��Ե�e�)k�Gl�*�EI�
����:gh��x@����y�F$F�_��%+�\��r1��ND~l""�54بN�:�FA��W
b� �\�F�M��C�o.�7z�Tz|~0��̔HgA�\���[��������:*i�P��Jpdh�v�01]�Ӟ_e�b߇��,�X��E, ��냬�e��Ϣ�5�Ϭ�B<p�A��~�3t3'>N=`
因此无法解析。
真正令人困惑的是,如果我在同一个 URL 上运行 scrapy shell,一切正常(网站的字符集是 utf-8)——这让我相信这是由 scrapyd 引起的。
非常感谢任何建议。
SETTINGS.py
># -*- coding: utf-8 -*-
BOT_NAME = "[name]"
SPIDER_MODULES = ["[name].spiders"]
NEWSPIDER_MODULE = "[name].spiders"
# Crawl responsibly by identifying yourself (and your website) on the user-agent
# USER_AGENT = '[name] (+http://www.yourdomain.com)'
ROBOTSTXT_OBEY = False
CRAWLERA_MAX_CONCURRENT = 50
CONCURRENT_REQUESTS = CRAWLERA_MAX_CONCURRENT
CONCURRENT_REQUESTS_PER_DOMAIN = CRAWLERA_MAX_CONCURRENT
AUTOTHROTTLE_ENABLED = False
DOWNLOAD_TIMEOUT = 600
DUPEFILTER_DEBUG = True
COOKIES_ENABLED = False # Disable cookies (enabled by default)
DEFAULT_REQUEST_HEADERS = {
"X-Crawlera-Profile": "desktop",
"X-Crawlera-Cookies": "disable",
"accept-encoding": "gzip, deflate, br",
}
DOWNLOADER_MIDDLEWARES = {
"scrapy_fake_useragent.middleware.RandomUserAgentMiddleware": 200,
"scrapy_crawlera.CrawleraMiddleware": 300,
}
CRAWLERA_ENABLED = True
CRAWLERA_APIKEY = "KEY"
ITEM_PIPELINES = {
"[name].pipelines.Export": 400,
}
# sentry dsn
SENTRY_DSN = "Key"
EXTENSIONS = {
"[name].extensions.SentryLogging": -1, # Load SentryLogging extension before others
}```
【问题讨论】:
-
可以添加您的代码和设置吗?
-
@MuratDemir 我已经添加了设置。蜘蛛代码库非常大,所以不会有用。我基本上是在检查已损坏的
response.body(response.encoding是预期的 utf-8),所以 response.css(anything) 失败。 -
你检查过
Content-Type和Content-Encoding吗?您是否尝试过通过 curl 获取网址 - 有什么不同吗? -
@SerhiiShynkarenko 这是
text/html; charset=utf-8,可以很好地使用 curl。即使使用scrapy shell,它也能正常工作。 -
@AimiHat 我想知道是不是因为使用了中间件。我建议在您的配置中一一禁用。
标签: python scrapy character-encoding web-crawler scrapyd