(last_url)
return latest_url
Download all XKCD comics released after the given URL.
Download all XKCD comics released after the given URL.
Download all XKCD comics released after the given URL .
def check_xkcd(last_url):
    """Download all XKCD comics released after the given URL."""
    url = 'https://xkcd.com'
    # Get latest comics URL with comic number for comparison/file update
    res = requests.get(url)
    res.raise_for_status()
    soup = bs4.BeautifulSoup(res.text, 'lxml')
    prev_link = soup.select('a[rel="prev"]')[0]
    href = prev_link.get('href')
    number = href.strip('/')
    url = 'https://xkcd.com/{}/'.format(str(int(number) + 1))
    latest_url = url

    if url == last_url:
        print('No new XKCD comics have been published since last check')
    else:
        while url != last_url:
            res = requests.get(url)
            res.raise_for_status()

            soup = bs4.BeautifulSoup(res.text, 'lxml')

            comic_elem = soup.select('#comic img')
            if comic_elem == []:
                print('could not find comic image.')
            else:
                try:
                    comic_url = 'https:' + comic_elem[0].get('src')
                    # Download the image.
                    print('Downloading image {}'.format(comic_url))
                    res = requests.get(comic_url)
                    res.raise_for_status()
                except requests.exceptions.MissingSchema:
                    # Skip this comic.
                    prev_link = soup.select('a[rel="prev"]')[0]
                    url = 'https://xkcd.com' + prev_link.get('href')
                    continue

                # Save the image to ./xkcd.
                image = open(os.path.join('Web Comics', os.path.basename(comic_url)), 'wb')
                for chunk in res.iter_content(100000):
                    image.write(chunk)
                image.close()

            # Get the Prev button's url.
            prev_link = soup.select('a[rel="prev"]')[0]
            url = 'https://xkcd.com' + prev_link.get('href')

    return latest_url
def check_xkcd ( last_url ) : url = 'https://xkcd.com' # Get latest comics URL with comic number for comparison/file update res = requests . get ( url ) res . raise_for_status ( ) soup = bs4 . BeautifulSoup ( res . text , 'lxml' ) prev_link = soup . select ( 'a[rel="prev"]' ) [ 0 ] href = prev_link . get ( 'href' ) number = href . strip ( '/' ) url = 'https://xkcd.com/{}/' . format ( str ( int ( number ) + 1 ) ) latest_url = url if url == last_url : print ( 'No new XKCD comics have been published since last check' ) else : while url != last_url : res = requests . get ( url ) res . raise_for_status ( ) soup = bs4 . BeautifulSoup ( res . text , 'lxml' ) comic_elem = soup . select ( '#comic img' ) if comic_elem == [ ] : print ( 'could not find comic image.' ) else : try : comic_url = 'https:' + comic_elem [ 0 ] . get ( 'src' ) # Download the image. print ( 'Downloading image {}' . format ( comic_url ) ) res = requests . get ( comic_url ) res . raise_for_status ( ) except requests . exceptions . MissingSchema : # Skip this comic. prev_link = soup . select ( 'a[rel="prev"]' ) [ 0 ] url = 'https://xkcd.com' + prev_link . get ( 'href' ) continue # Save the image to ./xkcd. image = open ( os . path . join ( 'Web Comics' , os . path . basename ( comic_url ) ) , 'wb' ) for chunk in res . iter_content ( 100000 ) : image . write ( chunk ) image . close ( ) # Get the Prev button's url. prev_link = soup . select ( 'a[rel="prev"]' ) [ 0 ] url = 'https://xkcd.com' + prev_link . get ( 'href' ) return latest_url