Created
December 12, 2014 22:31
-
-
Save epochblue/f8b18e3391f8dbd9bda8 to your computer and use it in GitHub Desktop.
Get all the image tags in a given URL and print their URLs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import urllib2 | |
import contextlib | |
from bs4 import BeautifulSoup | |
if len(sys.argv) < 2: | |
print 'Error: Please provide a URL to scrape.' | |
sys.exit(1) | |
URL = sys.argv[1] | |
with contextlib.closing(urllib2.urlopen(URL)) as r: | |
html = r.read() | |
soup = BeautifulSoup(html) | |
og_image = soup.find('meta', attrs={'property': 'og:image'}) | |
if og_image: | |
print og_image.get('content') | |
else: | |
for img in soup.find_all('img'): | |
print img.get('src') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment