Python 使用PyQuery删除html文档中嵌入在div中的div

Python 使用PyQuery删除html文档中嵌入在div中的div,python,Python,使用PyQuery,我希望获得一个特定类的div,删除该主div中嵌入的所有div和javascript,并获取主div中剩余内容的文本内容。到目前为止,我的代码如下: pq = pyquery(urllib2.urlopen(source_url).read()) # remove embedded divs and javascript here content = pq('.main_div').text() 删除主div中嵌入的div的最佳方法是什么?没关系,我想出来了。这里有一种方法

使用PyQuery,我希望获得一个特定类的div,删除该主div中嵌入的所有div和javascript,并获取主div中剩余内容的文本内容。到目前为止,我的代码如下:

pq = pyquery(urllib2.urlopen(source_url).read())
# remove embedded divs and javascript here
content = pq('.main_div').text()

删除主div中嵌入的div的最佳方法是什么?

没关系,我想出来了。这里有一种方法:

pq = pyquery(urllib2.urlopen(source_url).read())('.main_div')
pq('div').remove()
pq('script').remove()
content = pq.text()