如何用unitest类测试我的scrapy方法

2024-03-29 01:50:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在我的蜘蛛身上尝试一些方法。 例如,在我的项目中,我有以下模式:

 toto/
├── __init__.py
├── items.py
├── pipelines.py
├── settings.py
├── spiders
│   ├── __init__.py
│   └── mySpider.py
└── Unitest
    └── unitest.py

我的unitest.py看起来像这样:

^{pr2}$

我的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}

在我的统一测试.py文件,我怎么能叫我的蜘蛛? 我试图在我的统一测试.py文件,但它不。。。 我有个错误:

Traceback (most recent call last): File "unitest.py", line 10, in from toto.spiders import runSpider ImportError: No module named toto.spiders

我怎么能修好它?在


Tags: inpyimportselftitleparseinitresponse