所以我想湊表可以在這裏找到:http://www.betdistrict.com/tipsters卡住刮特定表scrapy
名爲「六月統計信息」表後我。
這裏是我的蜘蛛:
from __future__ import division
from decimal import *
import scrapy
import urlparse
from ttscrape.items import TtscrapeItem
class BetdistrictSpider(scrapy.Spider):
name = "betdistrict"
allowed_domains = ["betdistrict.com"]
start_urls = ["http://www.betdistrict.com/tipsters"]
def parse(self, response):
for sel in response.xpath('//table[1]/tr'):
item = TtscrapeItem()
name = sel.xpath('td[@class="tipst"]/a/text()').extract()[0]
url = sel.xpath('td[@class="tipst"]/a/@href').extract()[0]
tipster = '<a href="' + url + '" target="_blank" rel="nofollow">' + name + '</a>'
item['Tipster'] = tipster
won = sel.xpath('td[2]/text()').extract()[0]
lost = sel.xpath('td[3]/text()').extract()[0]
void = sel.xpath('td[4]/text()').extract()[0]
tips = int(won) + int(void) + int(lost)
item['Tips'] = tips
strike = Decimal(int(won)/tips) * 100
strike = str(round(strike,2))
item['Strike'] = [strike + "%"]
profit = sel.xpath('//td[5]/text()').extract()[0]
if profit[0] in ['+']:
profit = profit[1:]
item['Profit'] = profit
yield_str = sel.xpath('//td[6]/text()').extract()[0]
yield_str = yield_str.replace(' ','')
if yield_str[0] in ['+']:
yield_str = yield_str[1:]
item['Yield'] = '<span style="color: #40AA40">' + yield_str + '%</span>'
item['Site'] = 'Bet District'
yield item
這給了我一個列表索引超出範圍的錯誤的第一個變量(名稱)。
然而,當我重寫我的XPath選擇開始//,e.g:
name = sel.xpath('//td[@class="tipst"]/a/text()').extract()[0]
蜘蛛運行,但一遍又一遍刮掉第一線人。
我認爲這與表沒有一個thead,但在tbody的第一個tr中包含th標籤有關。
任何幫助,非常感謝。
---------- ----------編輯
針對拉爾斯建議:
我試圖用你提出什麼但仍得到超出範圍的錯誤列表:
from __future__ import division
from decimal import *
import scrapy
import urlparse
from ttscrape.items import TtscrapeItem
class BetdistrictSpider(scrapy.Spider):
name = "betdistrict"
allowed_domains = ["betdistrict.com"]
start_urls = ["http://www.betdistrict.com/tipsters"]
def parse(self, response):
for sel in response.xpath('//table[1]/tr[td[@class="tipst"]]'):
item = TtscrapeItem()
name = sel.xpath('a/text()').extract()[0]
url = sel.xpath('a/@href').extract()[0]
tipster = '<a href="' + url + '" target="_blank" rel="nofollow">' + name + '</a>'
item['Tipster'] = tipster
yield item
另外,我做的事情這樣假設,多爲循環需要,因爲不是所有的細胞具有相同的類?
我也嘗試做的事情,而沒有for循環,但在這種情況下,它再次刮只有第一個線人多次:當您們的說法
感謝
感謝您的回覆拉爾斯。自從試圖實現這一點以來,我已經添加了一個編輯,但仍然沒有運氣! – preach
@preach,儘管我們已經改變了for循環語句的XPath表達式,但sel仍然保存着tr元素而不是td元素。這是因爲XPath謂詞(方括號內的內容)不表示進一步的位置步驟;他們只是篩選你已經選擇的'tr's。因此,您需要將'name'的XPath更改爲'td [@ class =「tipst」]/a/text()',而不僅僅是'a/text()'。 – LarsH