{"id":1228,"date":"2013-06-27T13:27:05","date_gmt":"2013-06-27T13:27:05","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=1228"},"modified":"2013-06-27T13:27:05","modified_gmt":"2013-06-27T13:27:05","slug":"finding-all-images-in-html-files-over-a-certain-size-with-python-beautifulsoup","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/finding-all-images-in-html-files-over-a-certain-size-with-python-beautifulsoup\/","title":{"rendered":"Finding all images in HTML files over a certain size with Python BeautifulSoup"},"content":{"rendered":"<p>This example shows how to use the Beautiful Soup library to find all images referenced in a bunch of html files, then filter to a particular size range &#8211; this works well to take out header images, logos, tracking pictures, etc. This assumes a system where you mirrored a website&#8217;s directory structure with wget. Unlike most examples this handles some real data &#8211; e.g. 404&#8217;ed images and different URL structures.<\/p>\n<pre lang=\"Python\">\nfrom bs4 import BeautifulSoup\nimport Image\nimport urllib2\nimport os, re\n \ndomain = \"www.example.com\"\nindir = \"<file directory>\"\nfre = re.compile(\".*html$\")\n\nfor root, dirs, filenames in os.walk(indir):\n    for f in filenames:\n        if fre.match(f):\n            htmlfile = os.path.join(root, f)\n            print \"Parsing \"  + htmlfile\n \n            soup = BeautifulSoup(open(htmlfile), \"html5lib\")\n \n            images = soup.find_all('img')\n            def f(x) : \n                if (x['src'] is None):\n                    return False\n\n                imgSrc = x['src']\n\n                if (\"http:\/\/\" in imgSrc):\n                    imgSrc = imgSrc[7:]\n\n                if domain not in imgSrc:\n                    imgSrc = os.path.join(root, imgSrc)\n\n                imgFile = imgSrc\n\n                try: \n                    img = Image.open(imgFile)\n                    width, height = img.size\n                    return width > 150 and height > 150\n                except IOError:\n                    return False\n\n            art = filter(f, images)\n            print(art)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This example shows how to use the Beautiful Soup library to find all images referenced in a bunch of html files, then filter to a particular size range &#8211; this works well to take out header images, logos, tracking pictures, etc. This assumes a system where you mirrored a website&#8217;s directory structure with wget. Unlike &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/finding-all-images-in-html-files-over-a-certain-size-with-python-beautifulsoup\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Finding all images in HTML files over a certain size with Python BeautifulSoup&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[447,495],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1228"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=1228"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1228\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=1228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=1228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=1228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}