2016-04-27 6 views
2

ユーザーは、フォームにキーワードを入力して送信することができるため、ビューでキーワードを取得できます。その後、キーワードでstart_urlを作ることができます。 start_urlをscrapyスパイダーに渡して起動するにはどうすればよいですか?djangoビュー内で引数付きのスパイダースパイダーを実行する方法

これは私のビューメソッドです。

def results(request): 
    """Return the search results""" 
    key= request.GET['keyword'].strip() 
    books = Book.objects.filter(title__contains=key) 
    if books is None: 
     # I want to call the scrapy spider here. 
     pass 
     books = Book.objects.filter(title__contains=key) 
    context = {'books': books, 'key': title} 
    return render(request, 'search/results.html', context) 

これは私のクモクラスののinit()メソッドです。

def __init__(self, key): 
    self.key = key 
    url = "http://search.example.com/?key=" + key 
    self.start_urls = [url] 
+0

から

from scrapy.crawler import CrawlerRunner from scrapy.utils.project import get_project_settings if books is None: # I want to call the scrapy spider here. os.environ.setdefault("SCRAPY_SETTINGS_MODULE","whereyourscrapysettingsare") crawler_settings = get_project_settings() crawler = CrawlerRunner(crawler_settings) crawler.crawl(yourspider, key=key) 

は、あなたがこれまで持っているもののコード投稿することができますか? – Eli

答えて

関連する問題