Scrapy-Splash 问题求教各路大神! Splash 似乎没有渲染网页!

2019-02-25 21:35:10 +08:00
 Martzki

最近毕设需要爬取一些数据,使用了 Scrapy-Splash。
需要爬取的数据是表格,表内容由 JS 函数填充。
使用 Splash 渲染网页并将结果返回给 Scrapy,但是使用 Splash 之后似乎并没有任何作用!
网页: https://www.aqistudy.cn/historydata/daydata.php?city=%E6%AD%A6%E6%B1%89&month=201312
Splash 版本:3.3.1
OS 版本:Windows 10 专业版

表格代码如下:

<table width="100%" class="table table-condensed table-bordered table-striped table-hover table-responsive" style="margin-bottom:5px">
      <tr height=38px>
        <th align="center" style="background:#d9edf7">日期</th>
        <th align="center" style="background:#d9edf7">AQI</th>
        <th align="center" style="background:#d9edf7" width="80px" >质量等级</th>
        <th align="center" style="background:#d9edf7">PM2.5</th>
        <th align="center" style="background:#d9edf7">PM10</th>
        <th align="center" style="background:#d9edf7" class="hidden-xs">SO2</th>
        <th align="center" style="background:#d9edf7" class="hidden-xs">CO</th>
        <th align="center" style="background:#d9edf7" class="hidden-xs">NO2</th>
        <th align="center" style="background:#d9edf7" class="hidden-xs">O3_8h</th>
      </tr>
  </table>

填充表格的函数如下:

function showTable(items) {
  items.forEach(function(item) {
    $('.table tbody').append('\
      <tr>\
        <td align="center">' + item.time_point + '</td>\
        <td align="center">' + item.aqi + '</td>\
        <td align="center"><span style="display:block;width:60px;text-align:center;' + getAQIStyle(item.aqi) + '">' + item.quality + '</span></td>\
        <td align="center">' + item.pm2_5 + '</td>\
        <td align="center">' + item.pm10 + '</td>\
        <td align="center" class="hidden-xs">' + item.so2 + '</td>\
        <td align="center" class="hidden-xs">' + item.co + '</td>\
        <td align="center" class="hidden-xs">' + item.no2 + '</td>\
        <td align="center" class="hidden-xs">' + item.o3 + '</td>\
      </tr>'
      );
  });
}

不管是在爬虫里使用还是 localhost:8050 调试,得到的表格内容都和上述表格代码里内容一样,表格内容并没有得到。

3048 次点击
所在节点    Python
7 条回复
wenbinwu
2019-02-25 21:48:59 +08:00
试试调节一下 timeout ?
Martzki
2019-02-25 21:49:58 +08:00
@wenbinwu 感谢回复,最高设置到 60 秒,还是不行!
wenbinwu
2019-02-25 23:01:07 +08:00
@Martzki 我试了一下,POST historyapi.php 总是返回 undefined :(
SpiderXiantang
2019-02-26 00:51:43 +08:00
scrapy+selenium 试试?
SpiderXiantang
2019-02-26 01:18:12 +08:00
找到了 翻译一下这段代码就可以解决了 method = "GETDAYDATA", object = {city: "武汉", month: "201312"} , callback = ƒ (obj), period = 6

只是对接口的数据进行了加密
```
function getServerData(method, object, callback, period) {
const key = hex_md5(method + JSON.stringify(object));
const data = getDataFromLocalStorage(key, period);
if (!data) {
var param = getParam(method, object);
$.ajax({
url: 'api/historyapi.php',
data: {
hd: param
},
type: "post",
success: function(data) {
data = decodeData(data);
obj = JSON.parse(data);
if (obj.success) {
if (period > 0) {
obj.result.time = new Date().getTime();
localStorageUtil.save(key, obj.result)
}
callback(obj.result)
} else {
console.log(obj.errcode, obj.errmsg)
}
}
})
} else {
callback(data)
}
}
```
Martzki
2019-02-26 10:29:04 +08:00
@SpiderXiantang 多谢,用 splash 是因为了解到比 selenium 效率高。另外我对 web 实在是没有什么基础,能不能请您提示一下下一步应该怎么处理
Martzki
2019-02-28 12:18:06 +08:00
后面换用 selenium+PhantomJS 可以了,splash 还是没能成功

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/538642

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX