【发布时间】: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')
但什么也没显示...
请帮帮我。
【问题讨论】:
我尝试使用 PyQuery 从 html 文件中获取所有“id”,但带来了麻烦……我试试这个:
from pyquery import PyQuery
file = open('index.html', 'r').read
jQuery = PyQuery(html)
jQuery.attr('id')
但什么也没显示...
请帮帮我。
【问题讨论】:
我不确定您的示例代码是否是您正在使用的,但您在那里缺少一些不同的东西,例如调用 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>]
【讨论】: