Splash返回多个html快照的问题

时间:2018-06-09 20:35:58

标签: scrapy scrapy-spider scrapy-splash

我尝试使用启动脚本(在单个响应中,如文档中)返回多个html页面并从中提取链接。但我发现在response.text和response.body中,只要返回多个页面,html内容就会被更改。这不是response.data的情况,它工作正常。为什么会这样?

我在与文档http://splash.readthedocs.io/en/stable/scripting-ref.html#splash-html相同的代码(和网站)上尝试了这一点(来自后面部分,来自多个html快照的示例)

这是我的启动请求 - >

 yield Request(url= url, callback= self.parse, dont_filter=True, meta={
     'splash': {
          'args': {'lua_source': self.lua_source, 'timeout': 200},
          'endpoint': 'execute',
       },
 })

lua脚本看起来像这样 - >

function page_info(splash, url)
   splash:go(url)
   local res = {
     html=splash:html(),
     }
   return res
end

function main(splash, args)
   local base = "https://news.ycombinator.com/news?p="
   local result = treat.as_array({})
   for i=1,3 do
      local url =  base .. i
      result[i] = page_info(splash, url)
   end
   return result
end

由此产生的回应:

response.data - >

{u'html': u'<html op="news"><head><meta name="referrer" content="origin"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" type="text/css" href="news.css?i7azI8MkFRPfcPhHQ7HD">\n <link rel="shortcut icon" href="favicon.ico">\n <link rel="alternate" type="application/rss+xml" title="RSS" href="rss">\n <title>Hacker News</title>

response.text和response.body - &gt;

 u'[{"html": "<html op=\\"news\\"><head><meta name=\\"referrer\\" content=\\"origin\\"><meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\"><link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"news.css?i7azI8MkFRPfcPhHQ7HD\\">\\n <link rel=\\"shortcut icon\\" href=\\"favicon.ico\\">\\n <link rel=\\"alternate\\" type=\\"application/rss+xml\\" title=\\"RSS\\" href=\\"rss\\">\\n <title>Hacker News</title></head>

注意第二种情况下的额外\ \。这些可能是转义字符或其他东西,但它们会使用使用response.text的LinkExtractor,并导致链接断开。同样,只有在我返回一组html响应时才会发生这种情况。 我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

Lua脚本返回一个结果数组,它将被转换为Python中与JSON兼容的结构。 response.text中的值似乎是正确的。

您要传递给LinkExtractor的HTML位于json.loads(response.text)[i]['html'],而i位于0..2

In [1]: import json

In [2]: text = u'[{"html": "<html op=\\"news\\"><head><meta name=\\"referrer\\" content=\\"origin\\"><meta name=\\"viewport\\
   ...: " content=\\"width=device-width, initial-scale=1.0\\"><link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"news.css
   ...: ?i7azI8MkFRPfcPhHQ7HD\\">\\n <link rel=\\"shortcut icon\\" href=\\"favicon.ico\\">\\n <link rel=\\"alternate\\" type=
   ...: \\"application/rss+xml\\" title=\\"RSS\\" href=\\"rss\\">\\n <title>Hacker News</title></head>"}]'

In [3]: print(text)
[{"html": "<html op=\"news\"><head><meta name=\"referrer\" content=\"origin\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><link rel=\"stylesheet\" type=\"text/css\" href=\"news.css?i7azI8MkFRPfcPhHQ7HD\">\n <link rel=\"shortcut icon\" href=\"favicon.ico\">\n <link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS\" href=\"rss\">\n <title>Hacker News</title></head>"}]

In [4]: print(json.dumps(json.loads(text), indent=2))
[
  {
    "html": "<html op=\"news\"><head><meta name=\"referrer\" content=\"origin\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><link rel=\"stylesheet\" type=\"text/css\" href=\"news.css?i7azI8MkFRPfcPhHQ7HD\">\n <link rel=\"shortcut icon\" href=\"favicon.ico\">\n <link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS\" href=\"rss\">\n <title>Hacker News</title></head>"
  }
]

In [5]: print(json.loads(text)[0]['html'])
<html op="news"><head><meta name="referrer" content="origin"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" type="text/css" href="news.css?i7azI8MkFRPfcPhHQ7HD">
 <link rel="shortcut icon" href="favicon.ico">
 <link rel="alternate" type="application/rss+xml" title="RSS" href="rss">
 <title>Hacker News</title></head>