1
<div style="width: 400px;" id="accordion" class="ui-accordion ui-widget ui-helper-reset" role="tablist">
  <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-accordion-header-active ui-state-active ui-corner-top" role="tab" id="ui-accordion-accordion-header-0" aria-controls="ui-accordion-accordion-panel-0" aria-selected="true" tabindex="0"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>
                    Description</h3>
  <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" style="display: block;" id="ui-accordion-accordion-panel-0" aria-labelledby="ui-accordion-accordion-header-0" role="tabpanel" aria-expanded="true" aria-hidden="false">
                    this is the message I want to extract
  </div>
</div>

如何使用 PyQuery 提取消息?

from pyquery import PyQuery as pq
d = pq('http://www.somesite.com')

我试过了:

  • d('#ui-accordion-accordion-panel-0').text()
  • d('ui-accordion-content.ui-helper-reset.ui-widget-content.ui-corner-bottom.ui-accordion-content-active').text()
  • d('#accordion#ui-accordion-accordion-panel-0').text()

如果我使用d('#accordion').text()div则显示其下的每个文本,所以我不明白为什么我的第一个示例没有返回任何内容。

4

2 回答 2

0

最后我设法提取了它。

d('#accordion').find('div').eq(0).text()

于 2015-10-31T16:05:47.140 回答
0

您可以使用 css 路径,如下所示:

d('#accordion').find('.ui-accordion-content').text()

# or : d("#accordion .ui-accordion-content").text()

# or : d(".ui-accordion-content").text()

见例子:

document.write("<br><br><br>Content : " + $("#accordion .ui-accordion-content").text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width: 400px;" id="accordion" class="ui-accordion ui-widget ui-helper-reset" role="tablist">
  <h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-accordion-icons ui-accordion-header-active ui-state-active ui-corner-top" role="tab" id="ui-accordion-accordion-header-0" aria-controls="ui-accordion-accordion-panel-0" aria-selected="true" tabindex="0"><span class="ui-accordion-header-icon ui-icon ui-icon-triangle-1-s"></span>
                    Description</h3>
  <div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" style="display: block;" id="ui-accordion-accordion-panel-0" aria-labelledby="ui-accordion-accordion-header-0" role="tabpanel" aria-expanded="true" aria-hidden="false">
                    this is the message I want to extract
  </div>
</div>

于 2015-10-31T15:37:54.170 回答