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
Colorful
V2EX  ›  Python

请教一个 Python 爬虫,抓取 JS 变量的问题

  •  
  •   Colorful · 2020-06-25 23:05:48 +08:00 · 2395 次点击
    这是一个创建于 1402 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在获取到的值是

    "<script> window._xxxx = {}</script>"

    window._xxxx 里面是一个对象

    我想获取 window._xxxx 这个变量,但是在 console 里面直接打印_xxxx 也是可以获取到值的,所以想请教下如何获取到这个值?

    我用的是 scrapy 框架,网上有说可以用 selenium 可以解决这个问题,还在看。

    13 条回复    2020-06-30 10:00:20 +08:00
    supermoonie
        1
    supermoonie  
       2020-06-25 23:11:42 +08:00
    正则提取 {} 整个内容,然后转 JSON
    Colorful
        2
    Colorful  
    OP
       2020-06-25 23:24:17 +08:00
    @supermoonie

    # soup3 = re.compile('window._xxxx = (.*?);', re.S)
    soup3 = re.findall("window._xxxx = (.*?)",soup2)

    这两种方法都试过但都不行...
    是我写的有问题?
    ClericPy
        3
    ClericPy  
       2020-06-25 23:29:51 +08:00
    我自己写框架调用无头浏览器拿变量也就三四行...

    正则的话, 你正则写错了吧? 没原始代码不容易拼,

    r' window\._xxxx = ([\s\S]*?)</script>'

    你这又 xxx 又空 {} 的, 还是好好学学正则吧, 最怕盲猜需求
    musi
        4
    musi  
       2020-06-25 23:34:32 +08:00 via iPhone
    正则转 json 吧。匹配不到就是正则表达式写的有问题,再好好看看。能不用 selenium 就不用 selenium
    Colorful
        5
    Colorful  
    OP
       2020-06-25 23:35:15 +08:00
    @ClericPy 搞定了,要先转 str 再正则才可以
    Colorful
        6
    Colorful  
    OP
       2020-06-25 23:35:41 +08:00
    @musi 先转 str 再正则就可以了。为什么说能不用 selenium 就不用 selenium ?
    supermoonie
        7
    supermoonie  
       2020-06-25 23:39:40 +08:00
    @Colorful python 正则不太会,给你一段 js 的吧

    /.*_xxxx=({.*})<.*/.exec("<script> window._xxxx = {'foo': 'bar'}</script>".replace(/\s/g, ''))
    supermoonie
        8
    supermoonie  
       2020-06-25 23:40:31 +08:00
    @Colorful 因为 selenium 要运行浏览器,属于重量级的操作
    ClericPy
        9
    ClericPy  
       2020-06-25 23:41:34 +08:00
    @Colorful ... 你是把 Tag 对象给正则了... 我以为提前已经 outerHTML 或者 text 了, 早点休息吧, 大过节的能不写代码就不写代码
    supermoonie
        10
    supermoonie  
       2020-06-25 23:42:29 +08:00
    @ClericPy 大过节的,写写自己的代码吧🐶
    Colorful
        11
    Colorful  
    OP
       2020-06-25 23:44:44 +08:00
    @supermoonie 懂了,感谢普及
    lithbitren
        12
    lithbitren  
       2020-06-26 11:05:35 +08:00
    以前还不会正则的时候碰到过这种,直接

    i = html.find('window._xxxx = ')

    获得头坐标,然后再

    j = html.find('</script>', i)

    获得尾坐标,然后再

    json.loads(html[i + len('window._xxxx = '): j])

    就可以得到对象了。。
    warcraft1236
        13
    warcraft1236  
       2020-06-30 10:00:20 +08:00
    scrapy 为啥不直接用 css 提取 script 里边的内容,然后直接截取 {} 里边的内容就好了,连正则都不需要调,简单的很
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5388 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 08:31 · PVG 16:31 · LAX 01:31 · JFK 04:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.