2016-03-21 9 views
0

私はScrapyとPythonが初めてです。私はhttps://in.bookmyshow.com/moviesからデータを取得しようとしていました。データを抽出しようとしていたすべての映画の情報が必要なので、私のコードに問題があります。複数のリンクからのデータを取得する

rules = (Rule(SgmlLinkExtractor(allow=('https://in\.bookmyshow\.com/movies/.*',)), callback="parse_items", follow= True),) 


def parse_items(self, response): 
    for sel in response.xpath('//div[contains(@class, "movie-card")]'): 
     item = Ex1Item() 
     item['Moviename'] = sel.xpath('.//a[@class="__movie-name"]/text()').extract() 
     item['Language'] = sel.xpath('/html/body/div[1]/div[2]/div/div[1]/div[2]/section[1]/div/div[2]/div[1]/div[1]/div/div/div[2]/div[2]/ul/li/text()').extract() 
     item['Info'] = sel.xpath('.//div[@class="__rounded-box __genre"]/text()').extract() 
     item['Synopsis'] = sel.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[4]/div[2]/div[2]/blockquote/text()').extract() 
     item['Release'] = sel.xpath('.//span[@class="__release-date"]/text()').extract() 
     yield item 

答えて

1

コードは正常であるようです。おそらく問題はあなたがここに投稿した部分の外です。

これが私の仕事:

import scrapy 
from scrapy.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 


class BookmyshowSpider(CrawlSpider): 
    name = "bookmyshow" 
    start_urls = ['https://in.bookmyshow.com/movies'] 
    allowed_domains = ['bookmyshow.com'] 
    rules = (Rule(SgmlLinkExtractor(allow=('https://in\.bookmyshow\.com/movies/.*',)), callback="parse_items", follow= True),) 

    def parse_items(self, response): 
     for sel in response.xpath('//div[contains(@class, "movie-card")]'): 
      item = Ex1Item() 
      item['Moviename'] = sel.xpath('.//a[@class="__movie-name"]/text()').extract() 
      item['Language'] = sel.xpath('/html/body/div[1]/div[2]/div/div[1]/div[2]/section[1]/div/div[2]/div[1]/div[1]/div/div/div[2]/div[2]/ul/li/text()').extract() 
      item['Info'] = sel.xpath('.//div[@class="__rounded-box __genre"]/text()').extract() 
      item['Synopsis'] = sel.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[4]/div[2]/div[2]/blockquote/text()').extract() 
      item['Release'] = sel.xpath('.//span[@class="__release-date"]/text()').extract() 
      yield item 

EDIT:バージョンscrapy.Spider()

import scrapy 

class BookmyshowSpider(scrapy.Spider): 
    name = "bookmyshow" 
    start_urls = ['https://in.bookmyshow.com/movies'] 
    allowed_domains = ['bookmyshow.com'] 

    def parse(self, response): 
     links = response.xpath('//a/@href').re('movies/[^\/]+\/.*$') 
     for url in set(links): 
      url = response.urljoin(url) 
      yield scrapy.Request(url, callback=self.parse_movie) 

    def parse_movie(self, response): 
     for sel in response.xpath('//div[contains(@class, "movie-card")]'): 
      item = {} 
      item['Moviename'] = sel.xpath('.//a[@class="__movie-name"]/text()').extract() 
      item['Language'] = sel.xpath('/html/body/div[1]/div[2]/div/div[1]/div[2]/section[1]/div/div[2]/div[1]/div[1]/div/div/div[2]/div[2]/ul/li/text()').extract() 
      item['Info'] = sel.xpath('.//div[@class="__rounded-box __genre"]/text()').extract() 
      item['Synopsis'] = sel.xpath('/html/body/div[1]/div[2]/div[1]/div[2]/div[4]/div[2]/div[2]/blockquote/text()').extract() 
      item['Release'] = sel.xpath('.//span[@class="__release-date"]/text()').extract() 
      yield item 

parse()標準クモのクラスを使用して、スタートページから動画ページへのすべてのリンクを解析します。 parse_movie()は、特定のムービーページへのすべてのリクエストのコールバックとして使用されます。このバージョンでは、スパイダーの動作をより詳細に制御できます。

+0

コードは複数回反復され、無限ループとしても使用されます。データは抽出できますが、適切な順序では抽出できません。とにかく私の質問 – Shraddha

+1

を通過していただきありがとうございますあなたはCrawlSpiderタイプのルールとスパイダーを使用しています。このタイプのスパイダーはstart_urlsで始まり、ルールに一致するすべてのリンクに従います。私の例のスパイダーは無限ループに陥ることはありませんが、1000を超える映画ページをクロールするには時間がかかります。これはあなたが本当にしたい行動ですか?そうでない場合は、少し正確に説明してください。他のスパイダーのタイプがどんなものかを教えてください。 – dron22

+0

私はそのページから各映画の情報を取りたいと思っています。メインページとその情報を別のページから取得するのとよく似ており、メインページにあるすべてのムービーに対して実行する必要があります。私はCrawlSpiderをthis.Howeverヘルプのために使用する必要があるとは思わない。 – Shraddha

関連する問題