Scrapy как зациклить извлечение данных из другого класса div - PullRequest
0 голосов
/ 04 ноября 2019

Как извлечь данные из другого класса 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 Ответ

3 голосов
/ 04 ноября 2019

Проверьте это

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...