简体   繁体   中英

Create a lambda function with If inside a For Loop

I am working on some scraping on the web and I am trying to create a lambda function out of this.

The idea is that first I find all "td" in the BeautifulSoup variable I made (r_soup) and then I go deeper to search "a" and check the one that has "JPY" or "Depends on experience" in the text. That's the value I want:

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())

I have tried this:

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

But it is not working. I don't know much about lambda functions and I have been searching on the web but to no avail.

我建议你尝试列表理解:

[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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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