抓紧如何循环从不同的div类提取数据

如何提取不同div类下的数据?我只能用span.field-content来获取,而不能用div

<div class="view-content">
  <div class="views-row views-row-1 views-row-odd views-row-first">
    <div class="views-field views-field-acronym">
      <span class="field-content">15CBOOKTRADE</span>
    </div>
    <div class="views-field views-field-title">
      <span class="field-content">The 15th-century Book Trade: An Evidence-based Assessment and Visualization of the Distribution,Sale,and Reception of Books in the Renaissance</span> 
    </div> 
  </div>
  <div class="views-row views-row-2 views-row-even">
    <div class="views-field views-field-acronym">       
      <span class="field-content">2D-CHEM</span> 
    </div>
    <div class="views-field views-field-title">
      <span class="field-content">Two-Dimensional Chemistry towards New Graphene Derivatives</span>
    </div>  
  </div>
</div>
Lee_Ryan 回答:抓紧如何循环从不同的div类提取数据

检查一下

from scrapy.selector import Selector

body = '''
     <div class="view-content">
  <div class="views-row views-row-1 views-row-odd views-row-first">
    <div class="views-field views-field-acronym">
      <span class="field-content">15CBOOKTRADE</span>
    </div>
    <div class="views-field views-field-title">
      <span class="field-content">The 15th-century Book Trade: An Evidence-based Assessment and Visualization of the Distribution,Sale,and Reception of Books in the Renaissance</span> 
    </div> 
  </div>
  <div class="views-row views-row-2 views-row-even">
    <div class="views-field views-field-acronym">       
      <span class="field-content">2D-CHEM</span> 
    </div>
    <div class="views-field views-field-title">
      <span class="field-content">Two-Dimensional Chemistry towards New Graphene Derivatives</span>
    </div>  
  </div>
</div>
    '''

for n in Selector(text=body).xpath('//span[@class="field-content"]/text()'):
    print(n.get())


输出

15CBOOKTRADE
The 15th-century Book Trade: An Evidence-based Assessment and Visualization of the Distribution,and Reception of Books in the Renaissance
2D-CHEM
Two-Dimensional Chemistry towards New Graphene Derivatives

本文链接:https://www.f2er.com/3168180.html

大家都在问