import requests
import mechanize
from lxml import html  
import sys  
import urlparse


######

def grab_my_pictures(final_link): #iterates through the urls and pulls down pictures

    response = requests.get(final_link)  
    parsed_body = html.fromstring(response.text)

    # Grab links to all images
    images = parsed_body.xpath('//img/@src')  
    if not images:  
        sys.exit("Found No Images")

    # Convert any relative urls to absolute urls
    images = [urlparse.urljoin(response.url, url) for url in images]  
    print 'Found %s images' % len(images)

    # Only download first 50 pictures on each page
    for url in images[0:50]:
        r = requests.get(url)
        f = open('pics2/%s' % url.split('/')[-1], 'w')
        f.write(r.content)
        f.close()

#####
        
website_name = raw_input("Please enter the name of the website ")

list_of_links = []
br = mechanize.Browser()
br.set_handle_robots(False)
final_website = "http://" + website_name 
print final_website
response = br.open("http://" + website_name) #mechanzie code

#checks and returns all the webpage links
for link in br.links():
    #print (link.url) #test for evey url link
    list_of_links.append(link.url) #adds the url linsk to a list


 ## removes number one not position one a random hhtp

list_of_links.pop(1)
list_of_links.pop(1)

for item in list_of_links:
    final_link = final_website + item
    print final_link
    grab_my_pictures(final_link)
    

#####


