Sunday, November 2, 2014

Image parser in Python

Image Parser



import urllib.request

def parse_links(source):
    links=[]
    t=str(source).split('<img src=&quot')
    for i in t:
        r=str(i).split('&quot')
        links.append(r[0])
    return links
        

def download(links):
    name=0
    for i in links:
        try:
            v=urllib.request.urlopen(i)
            f=open(str(name)+&quot.jpg&quot,&quotwb&quot)
            f.write(v.read())
            f.close()
            name+=1
        except:print(&quotC&quot)

def load_source(website):
    s=urllib.request.urlopen(website)
    v=s.read()
    return v
    
def main():
    search=input(&quotStart:&quot)
    source=load_source(&quothttp://nzz.ch&quot)
    links=parse_links(source)
    download(links)
    for i in links:print(i)
    print(&quotEND&quot)
    
main()

Author: Marcin
Language: Python 3.4
Infos: 
A simple Script for filtering out Images of a website or just a html file. It's a simple parser that searches
for tags with Images in it and downloads these.
Video: https://www.youtube.com/watch?v=_-Lecym-BP0&feature=youtu.be

No comments:

Post a Comment