import requests, re, csv
from bs4 import BeautifulSoup
url = 'http://se.fotoalbum.eu/woc2016'
r  = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
file = open('test.csv', 'w', newline='')
writer = csv.writer(file)

def album(link):
    i = 1
    while i < 10:
        r = requests.get(link + '/' + str(i))
        # all pages outside the scope gets redirected...
        if not r.history:
            soup = BeautifulSoup(r.text, 'lxml')
            for plink in soup.findAll('a', {'class', 'thumbnail'}):
                photo(plink['href'])
        else:
            break
        i += 1
        
def photo(link):
    r = requests.get(link)
    if 'Klas Bringert' in r.text:
        soup = BeautifulSoup(r.text, 'lxml')
        filelink = 'http://se.fotoalbum.eu/' + soup.select('img[name="image"]')[0]['src'] # image url
        description = re.sub('\n', '', re.sub('<br>', '', soup.select('#imageDescription')[0].text)) # image description
        # remove bullshit whitespace
        title = re.sub(r'\s+[\s]', '', soup.select('#imageTitle')[0].text) # image title
        album = soup.select('#bcGalleryTitle')[0]['title'] # image album
        
        writer.writerow([title, album, filelink, description, link])
    else:
        print('skipping photo: unknown author')
    

for link in soup.find('div', {'id': 'galleries'}).findAll('a'):
    try:
        # all relevant links got a class attribute...
        link['class']

        album(link['href'])
        
    except(KeyError):
        print('skipping album link')

file.close()
print('done')
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping album link
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping album link
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping album link
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping album link
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping album link
skipping photo: unknown author
skipping album link
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping album link
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping album link
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping album link
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping photo: unknown author
skipping album link
done