繁体   English   中英

在 For 循环中使用 If 创建 lambda 函数

[英]Create a lambda function with If inside a For Loop

我正在网络上进行一些抓取工作,我正在尝试从中创建一个 lambda 函数。

这个想法是,首先我在我制作的 BeautifulSoup 变量 (r_soup) 中找到所有“td”,然后我更深入地搜索“a”并检查文本中包含“JPY”或“取决于经验”的那个。 这就是我想要的价值:

salary = r_soup.find_all('td')
for s in salary:
    if s.find('a') and 'JPY' in s.text or s.find('a') and 'Depends on experience' in s.text:
        print(s.text.strip())

我试过这个:

salary = list(map(lambda s: s.text if (s.find('a') and 'JPY' in s.text) or (s.find('a') and 'Depends on experience') in s.text ,r_soup.find_all('td')))
salary

但它不起作用。 我对 lambda 函数了解不多,我一直在网上搜索但无济于事。

我建议你尝试列表理解:

[print(s.text.strip()) for s in r_soup.find_all('td') if s.find('a') and 'JPY' in s.text or s.find('a') and 'Depends on experience' in s.text]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM