首页 > 解决方案 > 如何在字典中循环提取值?

问题描述

我正在刮一页。我做了两个loops,但第一个循环只transcription_price取值而不是最后两个。为什么以及如何解决这个问题?

def start_requests(self):
    links = {'transcription_page': 'https://www.rev.com/freelancers/transcription',
             'captions_page': 'https://www.rev.com/freelancers/captions',
             'subtitles_page': 'https://www.rev.com/freelancers/subtitles'
            }
    call = [self.parse_transcription,self.parse_caption,self.parse_subtitles]

    for link in links.values():
        for n in range(0,3):
            return [scrapy.Request(link, callback=call[n])]

标签: pythonscrapy

解决方案


Because return statement, well, returns the value and terminates1 the function, passing the control flow to the caller. This way, your inner loop is terminated before it goes over all the values.

Perhaps what you wanted was yield:

>>> def f():
...  for x in (1, 2, 3):
...   yield x
...
>>> list(f())
[1, 2, 3]

Besides, using unnamed constants is a way to plant a bug which is often not so obvious, not to say non-Pythonic:

items = ["a", "b", "c"]

# will make an incomplete round when `items` is longer than 3 elements
def poor():
  for i in xrange(0, 3):
    yield items[i]

# will do just alright
def appropriate():
  for item in items:
    yield item

1 Unless you are in try/except/finally block, in which case finally is always executed before return takes place:

def return_one():
  try:
    1/0
  except ZeroDivisionError:
    return 0
  finally:
    return 1

推荐阅读