my first python script.

Questions about programming languages and debugging
Post Reply
User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

my first python script.

Post by l0ngb1t »

well i came a cross a site while i was searching for a pic to draw, but it have alotttt of pics
so i decided why not to make a script that download them all and then i can check them later, plus i have a friend interested in tattoo's that find the pics very useful
anyway this is the code written in python:

Code: Select all

import urllib2
import re
report = open("failed_DL.txt","w")
imgnb = 1
baseurl = "http://www.girlsinink.tumblr.com/page/" #this part of url does not change
for n in range(1,101):  #loop through the pages
        print "Page number: " + str(n)
        page = baseurl + str(n) #we add a number at the and of base url to get page url
        srcode = urllib2.urlopen(page) #get the page source code
        lines = srcode.readlines() #split them into a line list
        for i in lines:          #loop throu the page lines
                if i.find('photo2') > -1: #find the line that contain the image url, it have "photo2" in it
                        picline = i #get the line
                        picline.strip()
                        picurl = re.findall(r'http://[\'"]?([^\'" >]+)', picline)
                        imgurl = "http://" + picurl[0]
                        print imgurl
                        try:
                                opener1 = urllib2.build_opener()
                                page1 = opener1.open(imgurl)    #open the picture page
                                my_picture = page1.read()       #read it
                                imgname = str(imgnb) + ".jpg"
                                print imgname
                                fout = open(imgname, "wb")     #open the file for writing
                                fout.write(my_picture)          #write the picture to it
                                fout.close()                    #close the file
                                imgnb = imgnb+1
                        except Exception:
                                print Exception
                                report.write(imgurl+"\n")
report.close()
how ever i find out python really easy and powerful and make things very simple, but i guess am used to JAVA so my primary language will stay java. (it doesn't mean that i won't be learning python and other languages)
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
moudy
Technology Enthusiast
Technology Enthusiast
Posts: 688
Joined: 10 Feb 2009, 17:00
15
Location: Beirut, Lebanon

Re: my first python script.

Post by moudy »

For me python is my primary language, and I'm satisfied with it.
Right now I'm working on a script related to GIF images, and automating several process.
mahmoud_shihab@hotmail.com

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Re: my first python script.

Post by l0ngb1t »

here is the code via pastebin
more clear. and i will upload the pics archive soon as a proof :P
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

Post Reply