Skip to content Skip to sidebar Skip to footer

How To Combine Several Similar Commands Using "for Match In Soup.find_all"?

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 code. import req

Solution 1:

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

Post a Comment for "How To Combine Several Similar Commands Using "for Match In Soup.find_all"?"