我正在使用 Scrapy 抓取一个网站,该网站需要启用烹饪和 java 脚本。我认为我不必实际处理 javascript。我所需要的只是假装 javascript 已启用。
这是我尝试过的:
1) 通过以下设置启用 Cookie
COOKIES_ENABLED = True
COOKIES_DEBUG = True
2)使用cookies下载中间件
DOWNLOADER_MIDDLEWARES = {
'scrapy.contrib.downloadermiddleware.useragent.UserAgentMiddleware': 400,
'scrapy.contrib.downloadermiddleware.cookies.CookiesMiddleware':700
}
3) 发送 'X-JAVASCRIPT-ENABLED': 'True'
DEFAULT_REQUEST_HEADERS={
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en',
'X-JAVASCRIPT-ENABLED': 'True'
}
但他们都没有和我一起工作。您能提出任何想法或给我一些指导吗?
提前感谢您的回复。
你应该试试Splash https://splash.readthedocs.org/en/latest/JS 引擎scrapyjs https://github.com/scrapinghub/scrapyjs。以下是如何在蜘蛛项目中进行设置的示例:
SPLASH_URL = 'http://192.168.59.103:8050'
DOWNLOADER_MIDDLEWARES = {
'scrapyjs.SplashMiddleware': 725,
}
抓取中心 http://crawlera.com/与 Scrapy 背后的公司是同一家公司特殊情况 http://scrapinghub.com/splash/运行你的蜘蛛并启用启动。
然后产量SplashRequest
代替Request
在你的蜘蛛中像这样:
import scrapy
from scrapy_splash import SplashRequest
class MySpider(scrapy.Spider):
start_urls = ["http://example.com", "http://example.com/foo"]
def start_requests(self):
for url in self.start_urls:
yield SplashRequest(url, self.parse,
endpoint='render.html',
args={'wait': 0.5},
)
def parse(self, response):
# response.body is a result of render.html call; it
# contains HTML processed by a browser.
# …
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)