How to combine several similar commands using “for match in soup.find_all”?

前端 未结 1 1608
予麋鹿
予麋鹿 2021-01-25 00:48

I have below code in which there are similar commands involved for match in soup.find_all. I would like to ask if it\'s possible to merge them and thus have cleaner

相关标签:
1条回答
  • 2021-01-25 01:43

    You can combine the various loops with .find_all() into the first .select().

    For example:

    import requests
    from bs4 import BeautifulSoup
    
    
    url = 'https://www.collinsdictionary.com/dictionary/french-english/aimanter'
    headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
    soup = BeautifulSoup(requests.get(url, headers = headers).content, 'html.parser')
    
    entry_name = soup.h2.text
    
    for tag in soup.select('''
            script,
            .hcdcrt,
            #ad_contentslot_1,
            #ad_contentslot_2,
            div.copyright,
            div.example-info,
            div.share-overlay,
            div.popup-overlay'''):
        tag.extract()
    
    content1 = ''.join(map(str, soup.select_one('.cB.cB-def.dictionary.biling').contents))
    content2 = ''.join(map(str, soup.select_one('.cB.cB-e.dcCorpEx').contents))
    
    format = open('aimer.html', 'w+', encoding = 'utf8')
    format.write(entry_name + '\n' + str(content1) + str(content2) + '\n</>\n' )
    format.close()
    
    0 讨论(0)
提交回复
热议问题