테이블의 크기를 알려주는 컬럼에 데이터가 항상 있기 때문에 목록 길이로 round-col을 사용합니다. 모든 필드가 일치해야하므로 데이터를 고칩니다.하지만 루프가 '데이터 없음'에 도달하면 목록 인덱스가 범위를 벗어나거나 'TypeError :'NoneType '개체가 스크립트 가능하지 않습니다.Python/Scrapy 핸들 테이블 데이터 누락, 범위를 벗어난리스트 인덱스
from scrapy.selector import Selector
from scrapy.spiders import Spider
from bigcrawler.items import BigcrawlerItem
class CrawlbotSpider(Spider):
name = 'bigcrawler'
allowed_domains = ['www.matchstat.com']
start_urls = ['https://matchstat.com/tennis/all-upcoming-matches']
custom_settings = {
'FEED_FORMAT': 'csv' ,
'FEED_URI': 'test.csv'
}
def parse(self ,response):
hxs = Selector(response)
item = BigcrawlerItem()
round_col = hxs.xpath(".//tr[contains(@class, 'match')]/td[contains(@class, 'round')]/text()").extract()
event_col = hxs.xpath(".//tr[contains(@class, 'match')]/td[contains(@class, 'event-name')]/a/text()").extract()
player1_col = hxs.xpath(".//tr[contains(@class, 'match')]/td[contains(@class, 'player-name')][1]/a/text()").extract()
player2_col = hxs.xpath(".//tr[contains(@class, 'match')]/td[contains(@class, 'player-name')][2]/a/text()").extract()
odds1_col = hxs.xpath(".//tr[contains(@class, 'match highlight')]/td[contains(@class, 'odds-td odds-0')]/a[contains(@class, 'btn btn-default virtual btn-xs btn-outcome odds')][1]/text()").extract_first()
odds2_col = hxs.xpath(".//tr[contains(@class, 'match highlight')]/td[contains(@class, 'odds-td odds-1')]/a[contains(@class, 'btn btn-default virtual btn-xs btn-outcome odds')][2]/text()").extract_first()
h2h_col = hxs.xpath(".//tr[contains(@class, 'match')]/td[contains(@class, 'h2h')]/a[contains(@class, 'h2h')]/text()").extract_first()
for x in range(0,len(round_col)):
item['round'] = round_col[x].strip()
item['event1'] = event_col[x].strip()
item['player_1'] = player1_col[x].strip()
item['player_2'] = player2_col[x].strip()
item['player_1_odds'] = odds1_col[x].strip()
item['player_2_odds'] = odds2_col[x].strip()
item['h_2_h'] = h2h_col[x].strip()
yield item
목록이 모양이 바뀌면 데이터가 쓸모가 없습니다. len 루프를 유지하는 요소에 도달하지 않으면 "없음"을 삽입하도록 루프를 어떻게 수정합니까?
내가 취할 수있는 또 다른 방법이 있습니까? 감사합니다. .
하나의 옵션은'X <렌 (odds_1_col)와 odds1_col [X] .strip() 또는 None'으로이 쓰기하지만 난 아마 클래스로 ...이 다시 작성해야한다고 생각합니다. –