如何在字典中进行循环以提取值?

时间:2019-05-08 22:00:52

标签: python scrapy

我正在抓一页。我做了两个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])]

1 个答案:

答案 0 :(得分:2)

因为return语句可以返回值并终止 1 函数,将控制流传递给调用者。这样,您的内部循环会在遍历所有值之前终止。

也许您想要的是yield

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

此外,使用未命名的常量是一种植入通常不是那么明显的错误的方法,更不用说非Python了:

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 除非您处于try/except/finally块中,否则在发生finally之前总是执行return

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