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

时间:2019-11-04 08:37:21

标签: scrapy

如何提取不同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>

1 个答案:

答案 0 :(得分:2)

检查一下

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, Sale, and Reception of Books in the Renaissance
2D-CHEM
Two-Dimensional Chemistry towards New Graphene Derivatives