简化此python生成

2024-09-30 01:27:30 发布

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

我对Python还不熟悉。有什么方法可以简化这一点:

def getDivs():
    divs = soup.findAll(name = "div", attrs = {"class" : "resultCell"}, recursive = True)
    for div in divs:
        h2 = div.find("h2")
        a = h2.find("a")
        href = a["href"]
        yield (href)

divs = list(getDivs())

我觉得我应该能够创建一个匿名函数而不是getDivs。类似于(伪代码):

divs = 

  [
     divs = soup.findAll(name = "div", attrs = {"class" : "resultCell"}, recursive = True)
     for div in divs:
        h2 = div.find("h2")
        a = h2.find("a")
        href = a["href"]
        yield (href)
  ]

谢谢


Tags: namedivtrueforh2findattrsclass

热门问题