web scraping - python beautifulsoup capture image -
using below script trying capture image , save on disk. , have save local path in db.
i have writting simple code capture image webpage:-
import urllib2 os.path import basename urlparse import urlsplit bs4 import beautifulsoup url = "http://www.someweblink.com/path_to_the_target_webpage" urlcontent = urllib2.urlopen(url).read() soup = beautifulsoup(''.join(urlcontent)) imgtags = soup.findall('img') imgtag in imgtags: imgurl = imgtag['src'] try: imgdata = urllib2.urlopen(imgurl).read() filename = basename(urlsplit(imgurl)[2]) output = open(filename,'wb') output.write(imgdata) output.close() except: pass
the page code image :-
<div class="single-post-thumb"> <img width="620" height="330" src="http://ccccc.com/wp-content/uploads/2016/05/weerewr.jpg"/>
if want download image using url of image can try this
import urllib img_url = "image url goes here" urllib.urlretrieve(img_url,'test.jpg')
it save image test.jpg name in current working directory.
note: mention full url of image "src" attribute of img tag contains relative urls.
Comments
Post a Comment