2017-10-16 78 views
0

我是新手到Python和scrapy無法使用scrapy

這裏是我的代碼,把所有的產品名稱,價格,圖片,標題從所有的下一個頁面,生成下一個頁面CSV

import scrapy  
class TestSpider(scrapy.Spider):  
name = "testdoc1"  
start_urls = ["https://www.amazon.in/s/ref=amb_link_46?ie=UTF8&bbn=1389432031&rh=i%3Aelectronics%2Cn%3A976419031%2Cn%3A%21976420031%2Cn%3A1389401031%2Cn%3A1389432031%2Cp_89%3AApple&pf_rd_m=A1VBAL9TL5WCBF&pf_rd_s=merchandised-search-leftnav&pf_rd_r=CYS25V3W021MSYPQ32FB&pf_rd_r=CYS25V3W021MSYPQ32FB&pf_rd_t=101&pf_rd_p=1ce3e975-c6e8-479a-8485-2e490b9f58a9&pf_rd_p=1ce3e975-c6e8-479a-8485-2e490b9f58a9&pf_rd_i=1389401031"] 

def parse(self, response): 
    for post_link in response.xpath('//a/@href').extract(): 
     link = response.urljoin(post_link) 
     yield scrapy.Request(link, callback=self.parse_post) 

    # Checks if the main page has a link to next page if True keep parsing. 
    next_page = response.xpath('(//a[@class="pagnNext"])[1]/@href').extract_first() 
    if next_page: 
     yield scrapy.Request(next_page, callback=self.parse) 

def parse_post(self, response): 
    # Scrape name,price,image, link from product. 
    for post in response.xpath('//li[contains(@class,"s-result-item celwidget")]'): 
     item = dict() 
     item['Name'] = post.xpath('.//h2[contains(@class,"a-size-base s-inline s-access-title a-text-normal")]/text()').extract() 
     item['Price'] = post.xpath('.//span[contains(@class,"a-size-base a-color-price s-price a-text-bold")]/text()').extract() 
     item['Image'] = post.xpath('.//img[contains(@class,"s-access-image cfMarker")]/@src').extract() 
     item['Link'] = post.sel.xpath('.//a[contains(@class,"a-link-normal s-access-detail-page s-color-twister-title-link a-text-normal")]/@href').extract() 
     yield item 

    # If the products page has a link to next page keep parsing. 
    next_page = response.xpath('(//a[@class="pagnNext"])[1]/@href').extract_first() 
    if next_page: 
     yield scrapy.Request(next_page, callback=self.parse_post) 

我爬不給任何錯誤,但我的CSV是空 `

+0

你的問題是什麼? –

+0

請具體說明你的問題,你做了什麼來解決它,以及你需要幫助的地方。 –

+0

你有錯誤的xpaths。 – Verz1Lka

回答

0

你的問題是低於線

yield scrapy.Request(next_page, callback=self.parse) 

該URL即將作爲相對URL。所以,你應該用

yield response.follow(next_page, callback=self.parse) 

這會自動解決相對URL

編輯-1

剛剛意識到你正在瀏覽的個人網頁,你只需要提取從結果頁中的數據。所以你的parse_post函數根本就不需要。以下是你需要怎麼做的

class TestSpider(scrapy.Spider): 
    name = "testdoc1" 
    allowed_domains = ['amazon.in'] 
    start_urls = [ 
     "https://www.amazon.in/s/ref=amb_link_46?ie=UTF8&bbn=1389432031&rh=i%3Aelectronics%2Cn%3A976419031%2Cn%3A%21976420031%2Cn%3A1389401031%2Cn%3A1389432031%2Cp_89%3AApple&pf_rd_m=A1VBAL9TL5WCBF&pf_rd_s=merchandised-search-leftnav&pf_rd_r=CYS25V3W021MSYPQ32FB&pf_rd_r=CYS25V3W021MSYPQ32FB&pf_rd_t=101&pf_rd_p=1ce3e975-c6e8-479a-8485-2e490b9f58a9&pf_rd_p=1ce3e975-c6e8-479a-8485-2e490b9f58a9&pf_rd_i=1389401031"] 

    def parse(self, response): 
     for post in response.css('li.s-result-item'): 
      item = dict() 
      item['Name'] = post.xpath(
       './/h2[contains(@class,"a-size-base s-inline s-access-title a-text-normal")]/text()').extract() 
      item['Price'] = post.xpath(
       './/span[contains(@class,"a-size-base a-color-price s-price a-text-bold")]/text()').extract() 
      item['Image'] = post.xpath('.//img[contains(@class,"s-access-image cfMarker")]/@src').extract() 
      item['Link'] = post.xpath(
       './/a[contains(@class,"a-link-normal s-access-detail-page s-color-twister-title-link a-text-normal")]/@href').extract() 
      yield item 


     # Checks if the main page has a link to next page if True keep parsing. 
     next_page = response.xpath('(//a[@class="pagnNext"])[1]/@href').extract_first() 
     if next_page: 
      yield response.follow(next_page, callback=self.parse) 
+0

先生,我改變了我的代碼,但是CSV沒有生成所有下一頁的確切結果,爲什麼會這樣? –

+0

@Zarinaveeru,請檢查更新回答 –

+0

是的,先生,它的工作。 –