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

如何使用pyquery获得一个页面的所有链接?

  •  
  •   shanshuise · 2013-01-31 16:45:40 +08:00 · 4484 次点击
    这是一个创建于 4100 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # -*- coding:utf-8 -*-
    from pyquery import PyQuery as pq

    d = pq(url='http://hi.baidu.com/tombkeeper/')
    d.make_links_absolute()
    lasers = d('.a-title')

    获取class为a-title的内容后,如果直接.attr('href')只能返回第一个链接,怎么才能返回里面的全部链接呢?
    4 条回复    1970-01-01 08:00:00 +08:00
    lijie0427
        1
    lijie0427  
       2013-01-31 19:43:23 +08:00
    for url in lasers:
    print url.attrib['href']
    shanshuise
        2
    shanshuise  
    OP
       2013-01-31 19:52:07 +08:00
    @lijie0427 多谢,已经可以返回全部的链接了。

    请问attrib['href']这一个写法在哪里有相应的介绍吗?我想看一看。
    lijie0427
        3
    lijie0427  
       2013-01-31 20:36:29 +08:00
    @shanshuise
    pyquery底层调用的是lxml库。
    http://lxml.de/api/index.html
    你看那个lxml.html.HtmlElement
    zz
        4
    zz  
       2013-01-31 20:40:27 +08:00 via Android
    才知道有这个库,一直都是用lxml
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1309 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:42 · PVG 01:42 · LAX 10:42 · JFK 13:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.