推荐学习书目
› 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 · Jan 31, 2013 · 5634 views
    This topic created in 4996 days ago, the information mentioned may be changed or developed.
    # -*- 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 replies  •  1970-01-01 08:00:00 +08:00
    lijie0427
        1
    lijie0427  
       Jan 31, 2013
    for url in lasers:
    print url.attrib['href']
    shanshuise
        2
    shanshuise  
    OP
       Jan 31, 2013
    @lijie0427 多谢,已经可以返回全部的链接了。

    请问attrib['href']这一个写法在哪里有相应的介绍吗?我想看一看。
    lijie0427
        3
    lijie0427  
       Jan 31, 2013
    @shanshuise
    pyquery底层调用的是lxml库。
    http://lxml.de/api/index.html
    你看那个lxml.html.HtmlElement
    zz
        4
    zz  
       Jan 31, 2013 via Android
    才知道有这个库,一直都是用lxml
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2647 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:08 · PVG 12:08 · LAX 21:08 · JFK 00:08
    ♥ Do have faith in what you're doing.