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

爬虫新手求助

  •  
  •   freeman49 · 2015-05-28 11:52:28 +08:00 · 1937 次点击
    这是一个创建于 3275 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用requests和beautifulsoup想把下面这个页面里的每一个subject下所有课程代码和课名抓出来
    https://courses.students.ubc.ca/cs/main?sessyr=2015&sesscd=S

    但是这个页面被js渲染过,bs4解析不出来内容,请问高手应该如何解决
    4 条回复
    gkiwi
        1
    gkiwi  
       2015-05-28 12:07:57 +08:00
    1. 如果你需要的数据是通过ajax异步过来的,你直接调用他的接口就好了。
    2. 如果是渲染过来页面的时候,数据被写到了js里面,然后再拼接的,那你就用正则匹配下试试。
    3. http://casperjs.org/ 那就用浏览器来执行js把。。

    还有就是真不清楚你想抓什么。。youtube页面上的列表?
    morefreeze
        2
    morefreeze  
       2015-05-28 21:32:25 +08:00
    顶1楼,另外搜了下,除了casperjs还有phantomjs也可用,撸个脚本就把之后的表格打印或者存成图片,很方便。
    var page = require('webpage').create();
    system = require('system');
    url = system.args[1];
    page.open(url, function(status) {
    var title = page.evaluate(function() {
    var tbl = document.getElementById("mainTable");
    return tbl.innerHTML;
    });
    console.log(title);
    page.render('example.png');
    phantom.exit();
    });
    保存成show_course.js

    然后执行`phantomjs show_course.js > course.html`就行啦
    morefreeze
        3
    morefreeze  
       2015-05-28 21:33:54 +08:00
    @morefreeze 第二个参数是url,应该是phantomjs show_course.js your_url > course.html
    latyas
        4
    latyas  
       2015-05-28 22:00:58 +08:00
    js渲染过就分析js,
    用phantomjs速度真是慢出翔
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5523 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 03:49 · PVG 11:49 · LAX 20:49 · JFK 23:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.