Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python scrapy在出现条件时停止scrapy_Python_Python 2.7_Scrapy - Fatal编程技术网

python scrapy在出现条件时停止scrapy

python scrapy在出现条件时停止scrapy,python,python-2.7,scrapy,Python,Python 2.7,Scrapy,我想从网站上提取所有数据 我使用的是scrapy 0.20.2 我的代码是 class MySpider(CrawlSpider): start_urls = ['TheWebsite'] rules = [Rule(SgmlLinkExtractor(allow=['/?page=\d+']), 'parse')] def parse(self, response): sites = sel.xpath('MyXPath') for s

我想从网站上提取所有数据

我使用的是scrapy 0.20.2

我的代码是

class MySpider(CrawlSpider):
    start_urls = ['TheWebsite']
    rules = [Rule(SgmlLinkExtractor(allow=['/?page=\d+']), 'parse')]

    def parse(self, response):
        sites = sel.xpath('MyXPath')
        for site in sites:
            if condition < 8:
                yield Request(Link, meta = {'date': Date},\
                   callback = self.MyFunction)
            else:
                # Code to stop scrapy goes here.
但当一种特殊情况发生时,我想停止爬行。在我的代码中,我想在
else
发生时这样做。请怎么做?

用于终止循环的
,或使用语句离开函数

for site in sites:
    if condition < 8:
        yield Request(Link, meta={'date': Date}, callback = self.MyFunction)
    else:
        break
对于站点中的站点:
如果条件<8:
产生请求(Link,meta={'date':date},callback=self.MyFunction)
其他:
打破

要在该点退出
循环的
,请使用
中断

for site in sites:
    if condition < 8:
        # ...
    else:
        break

这会阻止开始Url继续抓取吗?还是只保留for循环?@MarcoDinatsoli,它终止
for
循环=>不会导致更多的
屈服请求
=>防止出现额外的疤痕。但请查看开始url。即使发生了其他事情,它还会继续乱写吗?另外,请检查
+d
是否意味着
?page=1
?page=2
…ets?@MarcoDinatsoli,啊
/?
应该是
\?
以匹配
的字面意思,因为
在正则表达式中有特殊的含义。我将告诉您所有的业务逻辑。我有一个有数百万页的网站。每一页都有广告。我想放弃在过去7天内添加的所有广告。因此,我在每个语法为
?page=2
的页面上创建用于抓取的url,然后检查该页面的日期。如果少于7天。我在一个新函数中删除所有内容。如果不是,我想退出刮板。我的密码错了吗?
for site in sites:
    if condition < 8:
        # ...
    else:
        break
for ...:
    if something:
        break
# do something else before finishing