我想尝试一些方法在我的蜘蛛。 例如在我的项目,我有这样的模式:如何测试我的scrapy方法与unitest类
toto/
├── __init__.py
├── items.py
├── pipelines.py
├── settings.py
├── spiders
│ ├── __init__.py
│ └── mySpider.py
└── Unitest
└── unitest.py
我unitest.py
样子说:
# -*- coding: utf-8 -*-
import re
import weakref
import six
import unittest
from scrapy.selector import Selector
from scrapy.crawler import Crawler
from scrapy.utils.project import get_project_settings
from unittest.case import TestCase
from toto.spiders import runSpider
class SelectorTestCase(unittest.TestCase):
sscls = Selector
def test_demo(self):
print "test"
if __name__ == '__main__':
unittest.main()
和我mySpider.py
,看起来像:
import scrapy
class runSpider(scrapy.Spider):
name = 'blogspider'
start_urls = ['http://blog.scrapinghub.com']
def parse(self, response):
for url in response.css('ul li a::attr("href")').re(r'.*/\d\d\d\d/\d\d/$'):
yield scrapy.Request(response.urljoin(url), self.parse_titles)
def parse_titles(self, response):
for post_title in response.css('div.entries > ul > li a::text').extract():
yield {'title': post_title}
在我UNITEST .py文件,我怎么能打电话给我的蜘蛛? 我想在我的unitest.py文件中添加from toto.spiders import runSpider
,但是却没有...... 我得到这个错误:
Traceback (most recent call last): File "unitest.py", line 10, in from toto.spiders import runSpider ImportError: No module named toto.spiders
我怎样才能解决这个问题?
* “但却[原文]它不......” * - 什么?请更具体地了解正在发生的事情。请注意,测试目录通常会在您的包的顶层之外*参见http://www.jeffknupp.com/blog/2013/08/16/open-sourcing-a-python-project-the-right-way/ – jonrsharpe
runSpider在哪里? – Harsha
我编辑我的quatsion –