我試圖刮掉每年最高的廣告牌前100名。我有一個文件,一次工作一年,但我希望它爬過所有年份,並收集數據以及。這裏是我當前的代碼:刮多個頁面Scrapy
from scrapy import Spider
from scrapy.selector import Selector
from Billboard.items import BillboardItem
from scrapy.exceptions import CloseSpider
from scrapy.http import Request
URL = "http://www.billboard.com/archive/charts/%/hot-100"
class BillboardSpider(Spider):
name = 'Billboard_spider'
allowed_urls = ['http://www.billboard.com/']
start_urls = [URL % 1958]
def _init_(self):
self.page_number=1958
def parse(self, response):
print self.page_number
print "----------"
rows = response.xpath('//*[@id="block-system-main"]/div/div/div[2]/table/tbody/tr').extract()
for row in rows:
IssueDate = Selector(text=row).xpath('//td[1]/a/span/text()').extract()
Song = Selector(text=row).xpath('//td[2]/text()').extract()
Artist = Selector(text=row).xpath('//td[3]/a/text()').extract()
item = BillboardItem()
item['IssueDate'] = IssueDate
item['Song'] = Song
item['Artist'] = Artist
yield item
self.page_number += 1
yield Request(URL % self.page_number)
但我發現了錯誤: 「:在指數不支持的格式字符 '/'(0x2F)分別41 start_urls = [URL%1958年] ValueError異常」
任何想法?我希望代碼將原來的「URL」鏈接自動更改爲1959年,並且一年到一年地停止查找表格,然後關閉。
謝謝!將嘗試這ASAP – DataScienceAmateur
我得到這個錯誤:錯誤:蜘蛛必須返回請求,BaseItem,字典或無,得到'BillboardItem'在
DataScienceAmateur
是的,我修復了代碼,以便BillboardItem是一個實際的'Scrapy.Item'而不是隻是一個對象。現在應該工作。 – sxn