我对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)
]
谢谢
使用列表理解:
但是使用适当的XML解析工具是一个更好的主意。你知道吗
相关问题 更多 >
编程相关推荐