2012-04-15 8 views
0
from scrapy.spider import BaseSpider 
from scrapy.selector import HtmlXPathSelector 

class DmozSpider(BaseSpider): 
    name = "dmoz" 
    allowed_domains = ["dmoz.org"] 
    start_urls = [ 
     "www.dmoz.org/Computers/Programming/Languages/Python/Books/", 
     "www.dmoz.org/Computers/Programming/Languages/Python/Resources/" 
    ] 

    def parse(self, response): 
     hxs = HtmlXPathSelector(response) 
     sites = hxs.select('//ul/li') 
     for site in sites: 
      title = site.select('a/text()').extract() 
      link = site.select('a/@href').extract() 
      desc = site.select('text()').extract() 
      print title, link, desc 

これは私のコードです。私はループを使用してたくさんのURLを掻き集めることを望みます。では、どうやってこれらに挑戦しているのですか?私はそこに複数のURLを入れましたが、私はそれらのすべてから出力を得ていませんでした。一部のURLが応答を停止します。どうすればこのコードを使って確実にデータを取得できますか?ウェブサイトから複数のウェブページをクロールする

答えて

0

tutorialのコードを投稿したばかりです。あなたがすべきことは、実際にはの文書全体、特にbasic conceptの部分を読むことです。あなたが基本的に望むのは、crawl spiderです。そこでは、スパイダーが指定したコードに従って処理および処理するルールを定義することができます。

は一例でドキュメントを引用する:

from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.selector import HtmlXPathSelector 
from scrapy.item import Item 

class MySpider(CrawlSpider): 
    name = 'example.com' 
    allowed_domains = ['example.com'] 
    start_urls = ['http://www.example.com'] 

    rules = (
     # Extract links matching 'category.php' (but not matching 'subsection.php') 
     # and follow links from them (since no callback means follow=True by default). 
     Rule(SgmlLinkExtractor(allow=('category\.php',), deny=('subsection\.php',))), 

     # Extract links matching 'item.php' and parse them with the spider's method parse_item 
     Rule(SgmlLinkExtractor(allow=('item\.php',)), callback='parse_item'), 
    ) 

    def parse_item(self, response): 
     self.log('Hi, this is an item page! %s' % response.url) 

     hxs = HtmlXPathSelector(response) 
     item = Item() 
     item['id'] = hxs.select('//td[@id="item_id"]/text()').re(r'ID: (\d+)') 
     item['name'] = hxs.select('//td[@id="item_name"]/text()').extract() 
     item['description'] = hxs.select('//td[@id="item_description"]/text()').extract() 
     return item 
+0

ご迷惑をおかけして申し訳ありませんが、ルールを定義しようとしましたが、それは実際の問題ではありません。私の問題は、start_urlsに供給するすべてのページの出力を取得する方法です。最初の3〜4ページのみ.csvファイルに出力されています。私は掻き取るために約20ページ近くあります。それで私はどうやってこれをすると思いますか?これらの3-4ページを掻き集めた後、私のプログラムはサーバーからの応答を受信しなくなります。 – Nits

1

あなたのコードはOKに見えますが、あなたはstart_urlshttp://

start_urls = [ 
    "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/", 
    "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/" 
] 

UPDで始めるべきではないと確信している

start_urls最初から始まるURLのリストです。通常は、1つまたは2つのリンクがあります。まれです。 このページは、Scrapyスパイダーが同じ方法で処理するため、同じHTML構造でなければなりません。

私は4-5のURLをstart_urlsに入れても、最初に2-3の出力がOKです。 urlです。

私はこのことを信じていません。なぜなら、治療は何個のリンクがstart_urlsリストであるか気にしないからです。

しかし、それは応答を停止し、どのように私はこれのためのGUIを実装することができますか教えてください。

治療にはコードをテストするためにdebug shellがあります。

+0

答えのシンプルさを見て、私は正しい質問を理解しているかどうかはわかりません。あなたの答えは私よりも適切かもしれないと思いますが、私はOPがクロールプロセス自体を探しているかもしれないと思うので、とにかくそれを残します。 – DrColossos

+0

「OP」とは何ですか? =) – San4ez

+0

"Opriginal Poster";) – DrColossos

関連する問題