V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Martzki
V2EX  ›  Python

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

  •  1
     
  •   Martzki · 2019-02-25 21:35:10 +08:00 · 3027 次点击
    这是一个创建于 1858 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近毕设需要爬取一些数据,使用了 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 调试,得到的表格内容都和上述表格代码里内容一样,表格内容并没有得到。

    7 条回复    2019-02-28 12:18:06 +08:00
    wenbinwu
        1
    wenbinwu  
       2019-02-25 21:48:59 +08:00
    试试调节一下 timeout ?
    Martzki
        2
    Martzki  
    OP
       2019-02-25 21:49:58 +08:00
    @wenbinwu 感谢回复,最高设置到 60 秒,还是不行!
    wenbinwu
        3
    wenbinwu  
       2019-02-25 23:01:07 +08:00
    @Martzki 我试了一下,POST historyapi.php 总是返回 undefined :(
    SpiderXiantang
        4
    SpiderXiantang  
       2019-02-26 00:51:43 +08:00
    scrapy+selenium 试试?
    SpiderXiantang
        5
    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
        6
    Martzki  
    OP
       2019-02-26 10:29:04 +08:00
    @SpiderXiantang 多谢,用 splash 是因为了解到比 selenium 效率高。另外我对 web 实在是没有什么基础,能不能请您提示一下下一步应该怎么处理
    Martzki
        7
    Martzki  
    OP
       2019-02-28 12:18:06 +08:00
    后面换用 selenium+PhantomJS 可以了,splash 还是没能成功
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1185 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:10 · PVG 07:10 · LAX 16:10 · JFK 19:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.