Ошибка в первом ряду таблицы с заголовками.tr
содержит только th
и td
, поэтому ошибка IndexError: list index out of range
.Чтобы избежать этого, просто пропустите строку с пустыми данными, как здесь:
import scrapy
class BootstrapTableSpider(scrapy.Spider):
name = "bootstrap_table"
start_urls = ['https://wiki.dspt.info/index.php/Basic_Item_IDs_Page_1']
def parse(self, response):
for row in response.xpath('//*[@class="wikitable sortable zebra"]//tr'):
data = row.xpath('td//text()').extract()
if not data: # pay attention how we skip empty row here
continue
yield {
'id': data[0],
'name': data[3],
'stackable': data[5],
'category': data[9],
'vendor_price': data[11]
}
Вывод:
...
2019-04-30 08:48:50 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2019-04-30 08:48:50 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://wiki.dspt.info/index.php/Basic_Item_IDs_Page_1> (referer: None) ['cached']
2019-04-30 08:48:51 [scrapy.core.scraper] DEBUG: Scraped from <200 https://wiki.dspt.info/index.php/Basic_Item_IDs_Page_1>
{'category': u' #N/A ', 'stackable': u' 1 ', 'vendor_price': u' 198\n', 'id': u' 1 ', 'name': u' pile_of_chocobo_bedding '}
2019-04-30 08:48:51 [scrapy.core.scraper] DEBUG: Scraped from <200 https://wiki.dspt.info/index.php/Basic_Item_IDs_Page_1>
{'category': u' Furnishings ', 'stackable': u' 1 ', 'vendor_price': u' 391\n', 'id': u' 2 ', 'name': u' simple_bed '}
2019-04-30 08:48:51 [scrapy.core.scraper] DEBUG: Scraped from <200 https://wiki.dspt.info/index.php/Basic_Item_IDs_Page_1>
{'category': u' Furnishings ', 'stackable': u' 1 ', 'vendor_price': u' 1403\n', 'id': u' 3 ', 'name': u' oak_bed '}
2019-04-30 08:48:51 [scrapy.core.scraper] DEBUG: Scraped from <200 https://wiki.dspt.info/index.php/Basic_Item_IDs_Page_1>
{'category': u' Furnishings ', 'stackable': u' 1 ', 'vendor_price': u' 10100\n', 'id': u' 4 ', 'name': u' mahogany_bed '}
2019-04-30 08:48:51 [scrapy.core.scraper] DEBUG: Scraped from <200 https://wiki.dspt.info/index.php/Basic_Item_IDs_Page_1>
{'category': u' Furnishings ', 'stackable': u' 1 ', 'vendor_price': u' 1564\n', 'id': u' 5 ', 'name': u' bronze_bed '}
2019-04-30 08:48:51 [scrapy.core.scraper] DEBUG: Scraped from <200 https://wiki.dspt.info/index.php/Basic_Item_IDs_Page_1>
{'category': u' Furnishings ', 'stackable': u' 1 ', 'vendor_price': u' 12406\n', 'id': u' 6 ', 'name': u' nobles_bed '}
2019-04-30 08:48:51 [scrapy.core.scraper] DEBUG: Scraped from <200 https://wiki.dspt.info/index.php/Basic_Item_IDs_Page_1>
{'category': u' #N/A ', 'stackable': u' 1 ', 'vendor_price': u' 0\n', 'id': u' 7 ', 'name': u' gold_bed '}
...