2

私はスクレイピーに取り組んでおり、サイトをスクレイピングしてすべての情報を取得しました

Actually I had 3 spiders with different data, I had created these 3 spiders in the same folder with the following structure

scrapy.cfg
myproject/
    __init__.py
    items.py
    pipelines.py
    settings.py
    spiders/
         __init__.py
           spider1.py
           spider2.py
           spider3.py

Now when we run that particular spider I need to create a csv file through pipeline with that spider name , for example

spider1.csv,spider2.csv,spider3.csv and so on (Spiders are not limited they may be more)> According to the number of spiders and spider names I want to create csv files

Here whether we can create more than one pipeline in pipeline.py ? also how to create the csv file with spider name dynamically if more than one spider exists

Here I had 3 spider and I want to run all the 3 spiders at once(by using scrapyd), when I run all the 3 spiders 3 csv files with their spider names should be created. And I want to schedule this spiders running for every 6 hours. If something is wrong in my explanation please correct me and let me know how to achieve this.

Thanks in advance

Edited Code: For example I am pasting my code for only spider1.py

code in spider1.py:

class firstspider(BaseSpider):
    name = "spider1"
    domain_name = "www.example.com"
    start_urls = [
                   "www.example.com/headers/page-value"
                 ]
def parse(self, response):
    hxs = HtmlXPathSelector(response)
            ........
            .......
            item = Spider1Item()
            item['field1'] = some_result
            item['field2'] = some_result
            .....
            .....
            return item

Pipeline.py code:

import csv
from csv import DictWriter

class firstspider_pipeline(object):

def __init__(self):
    self.brandCategoryCsv = csv.writer(open('../%s.csv' % (spider.name), 'wb'),
    delimiter=',', quoting=csv.QUOTE_MINIMAL)
    self.brandCategoryCsv.writerow(['field1', 'field2','field3','field4'])



def process_item(self, item, spider):
    self.brandCategoryCsv.writerow([item['field1'],
                                item['field2'],
                                item['field3'],
                                item['field4'])
    return item 

As I stated before when I run the above spider with spider name, a csv file with the spider name will be created dynamically..... but now when if I run the remaining spiders like spider2,spider3,spider3 , the csv files with their corresponding spider names should generate.

  1. whether the above code is enough for the above functionality?

  2. whether we need to create another pipeline class to create another csv file?(Is it possible to create more than one pipeline classes in a single pipeline.py file?)

  3. If we create multiple pipeline classes in a single pipeline.py file, how to match the particular spider to its related pipeline class

I want to achieve the same functionality when saving to database, I mean when I run the spider1 all data of spider1 should saved to database into a table with relative spider name. Here for each spider I had different sql queries(so need to write different pipeline classes)

  1. Here intension is when we run multiple spiders all at a time(using scrapyd) , multiple csv files should generate with their spider names and multiple tables should be created with spider names(When saving in to database)

Sorry if am wrong anywhere, I hope its well explained and if not please let me know.

4

1 に答える 1

3

あなたは一般的に正しい方向に進んでいます。

しかし、私がすぐに指摘できるいくつかのポイントがあります:

  1. おそらくクラスは必要ありません(=使用すべきではありません)。PythonはJavaではありません。クラスが2つのメソッドのみで構成され、最初のメソッドが-メソッドである場合、クラスは__init__ほぼ確実に必要ありませんが、関数は問題なく機能します。整理整頓=より良いコード!

  2. SOは、一般的なコードレビューに適した場所ではありません。代わりにコードレビューを試してください。SO-ユーザーは(ほとんどの場合)友好的で役立つ集団ですが、コードを書くのは好きではありません。彼らは説明し、助言し、そして訂正するのが好きです。したがって、アプリケーションを実装してみてください。問題が発生した場合は、自分で解決できない場合は、もう一度戻ってアドバイスを求めてください。上で述べたように、あなたは概念的に正しい方向に進んでいます。それを実装してみてください。

  3. あなたはクラスの概念を誤解しているようです。少なくともそれがpython-classesである限り:

    1. 私が見る限り、BaseSpiderクラスは必要ありません。基本クラスとサブクラスの違いは何ですか?クラスを派生させても、プログラムがOO、またはそれ以上になるわけではありません。Liskovsの原則を検索して、Pythonでサブクラスが適切な場合の一般的な理解を得る。(これは多少逆のロジックですが、アプローチをサブクラス化または変更する必要があるかどうかを確認するための最速の方法の1つです。)

    2. クラス宣言の直後に宣言されるPythonクラス変数と、__init__メソッドで初期化されるインスタンス変数には明確な違いがあります。クラス変数は、クラスのすべてのインスタンス間で共有されます。ここで、インスタンス変数は個々のインスタンスにプライベートです。シングルトンパターンであるクラス変数を使用することはほとんどありません。これは、デバッグで頭痛や苦情を引き起こすため、ほとんどの場合避けたいものです。

したがって、私はあなたのSpiderクラスを次のように変更します:

class Spider(object):
    def __init__(self, name, url=None):
        self.name = name
        self.domain_name = url
        self.start_urls = [url]
        ...

crawlers = [Spider('spider %s' %i) for i in xrange(4)] #creates a list of 4 spiders 

しかし、宣言型のメタクラスアプローチを使用している可能性がありますが、投稿されたコードからはそれを確認できません。

クローラーを並行して実行する場合は、threading-moduleを検討する必要があります。multiprocessingこれは、並列計算を目的とした-moduleとは対照的に、連続したI/O操作を対象としています。

あなたは概念的に正しい方向に進んでいます。プロジェクトを細かく分割し、エラーが発生するたびに戻ってきます。

「Googleを再作成したくないのですが、どうすれば最善の方法で最短時間で再作成できますか」などの質問に対して完全な回答が得られるとは期待しないでください。;-)

于 2012-07-06T09:40:56.137 に答える