我试图用PyQuery从一个html文件中获取所有的"id“,但是带来了麻烦……我试着这样做:
from pyquery import PyQuery
file = open('index.html', 'r').read
jQuery = PyQuery(html)
jQuery.attr('id')但什么也没显示出来。
请帮帮我。
发布于 2013-04-07 02:08:44
我不确定您的示例代码是否就是您正在使用的,但是您遗漏了一些不同的东西,比如调用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>]https://stackoverflow.com/questions/15853749
复制相似问题