【问题标题】:PyQuery: extract id from html?PyQuery:从 html 中提取 id?
【发布时间】:2013-03-29 00:55:28
【问题描述】:

我尝试使用 PyQuery 从 html 文件中获取所有“id”,但带来了麻烦……我试试这个:

from pyquery import PyQuery

file = open('index.html', 'r').read
jQuery = PyQuery(html)

jQuery.attr('id')

但什么也没显示...

请帮帮我。

【问题讨论】:

    标签: python html attr pyquery


    【解决方案1】:

    我不确定您的示例代码是否是您正在使用的,但您在那里缺少一些不同的东西,例如调用 read() 而不是将 file 设为 read 方法,然后您从不使用它。当您从未为它分配任何东西时,您也会传入html

    但这是我写的东西,似乎找到了所有带有id 的元素,我尽可能地遵循你的名字,但我不想重复使用file,因为这是一个保留字我知道:

    from pyquery import PyQuery
    
    html = open('temp.html').read()
    
    jquery = PyQuery(html)
    ids = jquery.find('[id]')
    
    print ids
    >>>[<link#screen-switcher-stylesheet>, <div#search>, <input#term.input-text>, <input#submit.input-button>]
    

    【讨论】:

    • 感谢 TankorSmash,我没有解释得很好。我需要脚本在 html 中取 id 的名称。例如:
      content
      ,脚本要给我“hello”
    • @Benyi 没问题!如果该解决方案适合您,请随时单击我的答案附近的复选标记,将此问题标记为已解决。
    猜你喜欢
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多