推荐学习书目
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
woshichuanqilz
V2EX  ›  Python

请教一个抓取动态网页内容的问题

  •  
  •   woshichuanqilz · Jan 3, 2019 · 2761 views
    This topic created in 2778 days ago, the information mentioned may be changed or developed.

    我想抓取这个页面上, 下拉框里面内容的最大的一个数字(就是选择 quantity 那里)。但是这个数字在 html 里面一开始是没有显示的。 在点击下拉按钮之后才会出现需要的内容。

    quantity

    我现在的方法是用 selenium 模拟点击这个按钮然后抓取。 但是速度很慢。

    所以考虑用 scrapy 抓取的话, 就比较理想。

    我做了如下尝试:

    1. 我跟了一下 chrome 的 network, 在点击那个按钮的时候没有新的网络内容的加载. 说明这个 quantity 的内容已经加载好了。
    2. 我在 eventlistener 里面跟了点击事件的处理 js 的函数, 对 js 不是特别的熟悉, 但是看这里似乎没有需要的信息。
          var r = function() {
                        var r = this;
                        n && k(n) && n.apply(this, arguments);
                        var o = Array.prototype.slice.call(arguments);
                        try {
                            var a = o.map(function(e) {
                                return Be(e, t)
                            });
                            return e.handleEvent ? e.handleEvent.apply(this, a) : e.apply(this, a)
                        } catch (e) {
                            throw Fe += 1,
                            setTimeout(function() {
                                Fe -= 1
                            }),
                            i.withScope(function(n) {
                                return l(r, void 0, void 0, function() {
                                    var r = this;
                                    return c(this, function(a) {
                                        return n.addEventProcessor(function(e) {
                                            return l(r, void 0, void 0, function() {
                                                var n;
                                                return c(this, function(r) {
                                                    return n = s({}, e),
                                                    t.mechanism && (n.exception = n.exception || {},
                                                    n.exception.mechanism = t.mechanism),
                                                    n.extra = s({}, n.extra, {
                                                        arguments: V(o, 2)
                                                    }),
                                                    [2, n]
                                                })
                                            })
                                        }),
                                        i.getCurrentHub().captureException(e, {
                                            originalException: e
                                        }),
                                        [2]
                                    })
                                })
                            }),
                            e
                        }
                    };
    
    

    所以这里求助一下, 我的这个尝试的方向是不是正确下面应该怎么走?

    5 replies    2019-01-03 19:43:10 +08:00
    yuanfnadi
        1
    yuanfnadi  
       Jan 3, 2019
    方向完全错了。。。



    直接爬接口啊。完全都不需要请求 html



    https://www.noon.com/_svc/catalog/api/product/N14772965V?slug=ask-spr-tig-3-4-waist-band-tights-white&offerCode=c1b02b19114fb58f&offerId=0&shippingCountryCode=AE&lang=en


    返回键结果中的


    product -> variants -> offers -> stock 就是你需要的数字。
    xiaocsl
        2
    xiaocsl  
       Jan 3, 2019   ❤️ 1
    说一下,思路,
    先判断库存是否是 ajax 请求的,
    用 fiddler,设置断点,发现网页请求了第一个 HTML 以后,就出现了库存数据.得出结论,请求的第一个 HTML 文件中就包含有库存信息.
    网页上右键 > 查看网页源代码 > Ctrl + F > 习惯性上手搜了一下库存的英文单词"stock".

    看到数据
    sku_config":"N14967100V","stock":10,"price":"107.95","sale_price":"42.10"

    完事..get 一个 HTML 页面就完事了.
    xiaocsl
        3
    xiaocsl  
       Jan 3, 2019
    楼上的是最好的,一般,指向性爬数据的时候,尽量减少爬 HTML 的情况,
    效率太低了.

    打开 F12 过一遍 XHR 对象 完事.

    找不到就去访问移动页面找.

    移动页面没找到就抓 APP.
    woshichuanqilz
        4
    woshichuanqilz  
    OP
       Jan 3, 2019
    @yuanfnadi @xiaocsl 多谢已经解决。


    @xiaocsl 你的那个一般性方法很受用~
    andrewTeller
        5
    andrewTeller  
       Jan 3, 2019
    弱弱的问下一楼的大神,接口是怎么找到的......
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3282 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 11:06 · PVG 19:06 · LAX 04:06 · JFK 07:06
    ♥ Do have faith in what you're doing.