Question #43112

5. Write a code in Python to fetches all flag images on http://www.f-secure.com/en/web/corporation_global/contact/offices/europe and stores the images on disk, and save the image with file name based on the country name.

Expert's answer

# Answer on Question #43112 - Programming - Pythonimport requestsfrom lxml import htmltree = html.parse("https://www.f-secure.com/en/web/corporation_global/contact/offices/europe")flags = tree.xpath("//img[contains(@src, 'ico-active-lang')]")for flag in flags:    file_name = flag.attrib['src'].split('-')[-1]    src = 'https://www.f-secure.com' + flag.attrib['src']    r = requests.get(src)    with open(file_name, 'wb') as f:        f.write(r.content)flags = tree.xpath("//img[contains(@src, 'corporation_global')]")for flag in flags:    file_name = flag.attrib['src'].split('/')[-1]    src = 'https://www.f-secure.com' + flag.attrib['src']    r = requests.get(src)    with open(file_name, 'wb') as f:        f.write(r.content)

LATEST TUTORIALS
APPROVED BY CLIENTS