【问题标题】:Python: CSS Selector to use inside lxml.cssselectPython:在 lxml.cssselect 中使用的 CSS 选择器
【发布时间】:2012-01-29 05:06:54
【问题描述】:

我正在尝试使用lxml.html 并使用CSSSelector 而不是XPath 解析下面给出的html 代码。

link = doc.cssselect('html body div.results dl dt a)

上面的代码给了我content-1content-2 作为输出,但我想要的输出是link 1 link 2。所以我用

替换了我的代码
link = doc.cssselect('html body div.results dl dt a[href]')

但仍然得到相同的输出。所以我的问题是获取 href 属性的正确 CSS 选择器是什么。

             <div class = "results">
                     <div> some tags here </div>
                        <dl> 
                              <dt title = "My Title 1" style = "background: transparent url('/img/accept.png') no-repeat right center">
                              <a href = "/link 1"> content-1</a> 
                              </dt>
                       </dl>

                      <dl>
                             <dt title = "My Title 2" style = "background: transparent url('/img/accept.png') no-repeat right center">
                             <a href = "/link 2">content-2</a>
                             </dt>
                     </dl>
            </div>

【问题讨论】:

    标签: python css css-selectors lxml


    【解决方案1】:

    相信您无法通过 CSS 选择器获取属性值。你应该得到元素...

    >>> elements = doc.cssselect('div.results dl dt a')
    

    ...然后从中获取属性:

    >>> for element in elements:
    ...     print element.get('href')
    ... 
    /link 1
    /link 2
    

    当然,列表推导是你的朋友:

    >>> [element.get('href') for element in elements]
    ['/link 1', '/link 2']
    

    由于您无法在 CSS 中更新属性的属性,我相信通过 CSS 选择器获取它们是没有意义的。您可以在 CSS 选择器中“提及”属性以仅检索以匹配其元素。 然而,这只是一种思考,我可能错了;如果我是,请有人纠正我:)好吧,@Tim Diggs 在下面证实了我的假设:)

    【讨论】:

    • @brandizzi,你是对的 - 你只能选择 css 中的元素,而不是属性 - 括号用于过滤要选择的元素(但只选择 标签不是一个坏主意没有 href 属性(这是 a[href] 所做的)。
    • @RanRag,即使您最终不需要它,也应该将brandizzi 的答案勾选为正确。
    • 我打算打勾,但你只能在一段时间后接受答案(我相信它会在 5 分钟后)
    【解决方案2】:

    你需要获取cssselect结果的属性(它总是返回元素,从不返回属性):

    首先,我不确定 doc.cssselect (但也许这是你自己的函数?)

    lxml.cssselect 正常使用:

    from lxml.cssselect import CSSSelector
    sel = CSSSelector('html body div.results dl dt a[href]')
    

    那么,假设你已经有了一个文档

    links = []
    for a_href in sel(doc):
        links.append(a_href.get('href'))
    

    或更简洁:

    links = [a_href.get('href') for a_href in doc.cssselect('html body div.results dl dt a[href]')]
    

    【讨论】:

    • 基本上 doc 相当于 doc=lxml.html.fromstring(content) 其中内容是我来自 urllib and read 函数的 html 数据
    【解决方案3】:

    我已经成功使用

    #element-id ::attr(value)
    

    获取 HTML 元素的“值”属性。

    【讨论】:

      【解决方案4】:

      lxml cssselector 与属性选择一起使用。下面的代码可以从 HTML 脚本元素中选择 src 属性。

         select = cssselect.CSSSelector("script[src]")
         links = [ el.get('src') for el in select(dochtml) ]
         links=iter(links)
         for n, l in enumerate(links):
             print n, l
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-21
        • 1970-01-01
        • 2022-01-14
        • 2013-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-01
        相关资源
        最近更新 更多