如何提取消息?

时间:2015-10-31 15:33:33

标签: python pyquery

<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下的每个文字,因此我不明白为什么我的第一个示例不会返回任何内容。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

最后我设法提取它。

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