V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · May 28, 2015 · 2366 views
    This topic created in 3992 days ago, the information mentioned may be changed or developed.
    用requests和beautifulsoup想把下面这个页面里的每一个subject下所有课程代码和课名抓出来
    https://courses.students.ubc.ca/cs/main?sessyr=2015&sesscd=S

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

    还有就是真不清楚你想抓什么。。youtube页面上的列表?
    morefreeze
        2
    morefreeze  
       May 28, 2015
    顶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  
       May 28, 2015
    @morefreeze 第二个参数是url,应该是phantomjs show_course.js your_url > course.html
    latyas
        4
    latyas  
       May 28, 2015
    js渲染过就分析js,
    用phantomjs速度真是慢出翔
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2306 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 01:34 · PVG 09:34 · LAX 18:34 · JFK 21:34
    ♥ Do have faith in what you're doing.