【发布时间】:2019-10-16 00:27:19
【问题描述】:
我正在尝试用 scrapy 和 splash 抓取一个网站。 我想从图像中出现的响应中抓取特定的 html 代码。 这是带有标题的响应:
这是响应(我要抓取的 html):
我可以使用检查工具找到该 HTML。我的代码返回的是我可以使用“查看页面源”工具看到的 html。因此,这意味着 Javascript 在嵌入代码之前会对其进行修改。但是,splash 的作用是运行 javascript 并返回 HTML,不是吗? response.body 返回页面的源代码,没有我上面提到的响应中需要的 html 代码。
import scrapy
from scrapy_splash import SplashRequest
from bs4 import BeautifulSoup
class NetherSplashSpider(scrapy.Spider):
name = 'nether_splash'
download_delay = 10
custom_settings = {
'SPLASH_URL': 'http://localhost:8050',
'DOWNLOADER_MIDDLEWARES': {
'scrapy_splash.SplashCookiesMiddleware': 723,
'scrapy_splash.SplashMiddleware': 725,
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810,
},
'SPIDER_MIDDLEWARES': {
'scrapy_splash.SplashDeduplicateArgsMiddleware': 100,
},
'DUPEFILTER_CLASS': 'scrapy_splash.SplashAwareDupeFilter',
}
def start_requests(self):
yield SplashRequest(
url='https://www.gaslicht.com/stroom-vergelijken?partial=true&aanbieders=eneco&skip=0&take=10&_=1559207102962',
callback=self.parse,
)
def parse(self, response):
filename = 'splash.html'
with open(filename, 'wb') as f:
f.write(response.body)
【问题讨论】:
-
您能否使用 Inspect 工具(而不是网络工具)在浏览器中找到该 HTML?也许 JavaScript 在嵌入之前稍微修改了代码。还可以尝试在 Splash 配置中使用更长的时间。
-
是的,我可以使用检查工具找到该 HTML。我的代码返回的是我可以使用“查看页面源”工具看到的 html。可以肯定的是,Javascript 在嵌入代码之前会对其进行修改。但是,splash 的作用是运行 javascript 并返回 HTML,不是吗??
标签: python html scrapy web-crawler scrape