javascript - Scrapy:POST 请求返回 JSON 响应(200 OK)但数据不完整

标签 javascript python json scrapy

MySpider 正在尝试描述加载更多操作单击,该操作会导致动态加载网页上的更多项目。这一直持续到没有更多的东西需要加载为止。

yield FormRequest(url,headers=header,formdata={'entity_id': '70431','profile_action': 'review-top','page':str(p), 'limit': '5'},callback=self.parse_review)

header = {#'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0',
               'X-Requested-With': 'XMLHttpRequest',
               'Host': 'www.zomato.com',
               'Accept': '*/*',
               'Referer': 'https://www.zomato.com',
               'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
               'dont_filter':'True' }

url = 'https://www.zomato.com/php/social_load_more.php'

收到的响应是 json 响应。

jsonresponse = json.load(response)

我确实看到了 -

('data==', {u'status': u'success', u'left_count': 0, u'html': u"<script type='text/javascript'>if (typeof initiateLaziness == 'function') initiateLaziness() </script>", u'page': u'1', u'more': 0})

你看到我收到了状态、剩余计数、页面等的响应。 不过我对“html”感兴趣。不幸的是,如果通过浏览器完成,我确实收到的不正确值(检查网络调用并验证)

预期的“html”是 ----

<div><a> very long html stuff...............................................<div><script type='text/javascript'>if (typeof initiateLaziness == 'function') initiateLaziness() </script>

我只收到后面的部分

<script>...................................</script>. 

缺少真正的 html 内容。

需要注意的是,我确实收到了回复,但仅针对“html”收到了不完整的回复。一切都很好,适合休息。我相信这可能与动态生成的 html 有关。但我得到了任何线索。

scrapy 中间件不添加内容长度。并且不允许我添加一个。将其添加到 header 时,响应失败并返回 400。

请求 header 实际发送到服务器:

 {'Accept-Language': ['en'], 'Accept-Encoding': ['gzip, deflate,br'], 'Dont_Filter': ['True'], 'Connection': ['keep-alive'], 'Accept': ['*/*'], 'User-Agent': ['Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0'], 'Host': ['www.zomato.com'], 'X-Requested-With': ['XMLHttpRequest'], 'Cookie': ['zl=en; fbtrack=9be27330646d24088c56c2531ea2fbf5; fbcity=7; PHPSESSID=2338004ce3fd540477242c3eaee685168163bd05'], 'Referer': ['https://www.zomato.com'], 'Content-Type': ['application/x-www-form-urlencoded; charset=UTF-8']})

如果我在这里遗漏了什么,有人可以帮助我吗? 或者我可以以某种方式发送内容长度/或让中间件为我发送它? 非常感谢。

最佳答案

由于不使用cookie,您将无法获得响应中的html内容。在您提到的实际请求 header 中,有一个 cookie 属性。但是在您通过代码发送的 ajax 请求中,没有 cookie 字段。

首先,在对 zomato 餐厅页面发出的请求的响应中设置一个 cookie,网址为:https://www.zomato.com/city/restaurant/reviews 。现在,当单击“加载更多”按钮时,将发送一个带有 cookie 字段的请求,该字段包含服务器在上一个对 url ' https://www.zomato.com/php/social_load_more.php 的响应中设置的 cookie。 '。因此,每次发出 ajax 请求时,应在请求 header 中发送上一个响应中设置的 cookie,并在当前请求的响应中设置一个新的 cookie。

因此,为了管理这些cookie,我使用了requests包的session对象。不使用 scrapy 也可以编写脚本。当您在 scrapy 中编写代码时,请查看是否有任何 session 对象可用于管理 scrapy 的 cookie。

我的代码:

import requests
url : 'https://www.zomato.com/city/restaurant/reviews' 
s = requests.Session()
resp = s.get(url, headers=header) 

上面的代码是向餐厅评论的url发送请求。这很重要,因为第一个 cookie 是在此请求的响应中设置的。

params={
        'entity_id':res_id,
        'profile_action':'reviews-dd',
        'page':'1',
        'limit':'5'
    }
header = {"origin":"https://www.zomato.com","Referer":"https://www.zomato.com/","user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0", "x-requested-with":"XMLHttpRequest", 'Accept-Encoding': 'gzip, deflate, br'}
loadreviews_text = s.post("https://www.zomato.com/php/social_load_more.php", data=params, headers=header)
loadreviews = loadreviews_text.json()

现在向social_load_more.php发出请求。对象 's' 管理 cookie。变量 loadreviews 现在将具有 json 格式的 html 数据。

关于javascript - Scrapy:POST 请求返回 JSON 响应(200 OK)但数据不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35160625/

相关文章:

javascript - 如何将 Bootstrap 表单助手国家/地区选择器插入表单?

javascript - 将函数从服务器传递到客户端

Javascript:使用键盘浏览 DIV 列表

json - 在 Haskell 中将元组数组转换为 JSON

java - 将 JSON 转换为 Java 对象时的 Setter 问题

javascript - JQuery 表情符号选择器

python - Django 不接受来自 python-requests.patch() 的数据

python - Python 和 C 之间独立于操作系统的程序间通信

python - f.seek() 和 f.tell() 读取文本文件的每一行

java - GSON 创建对象数组