# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# +
import requests 
from bs4 import BeautifulSoup

comic_dictionary = {} # We will add each comics urls and other text info into a dictionary 

comic_index = 2000 # loop from 2000 - 2490 

# -

base_url = "https://xkcd.com/" + str(comic_index) + '/'
page = requests.get(base_url)
# page
# page.status_code
# page.content
soup = BeautifulSoup(page.content, 'html.parser')
# print(soup.prettify())
# list(soup.children)
# [type(item) for item in list(soup.children)]
comic = list(soup.find_all(id="comic")) 
# comic
# print (base_url)

# +
comic_title = str(comic[0])
comic_title = comic_title.split(sep = "src")[0]
comic_title = comic_title.split(sep = "alt")[1]

comic_title = comic_title[2:len(comic_title)-2]
print (comic_title)

# +
comic_alt = str(comic[0])
comic_alt = comic_alt.split(sep = "src")[2]
print (comic_alt)
comic_alt = comic_alt.split(sep = "title")[1]

comic_alt = comic_alt[2:len(comic_alt)-15]
print (comic_alt)

# +
comic_url_src = str(comic[0])
comic_url_src = comic_url_src.split(sep = "src")[1]

comic_url_src = "http://" + comic_url_src[4:len(comic_url_src)-2]
# print (comic_url_src)
# -

comic_dictionary[comic_index] = [base_url, comic_title, comic_alt, comic_url_src]
comic_dictionary[comic_index]

# +
for comic_index in range(2000,2100):
    
    ## make the Url and wget it 
    base_url = "https://xkcd.com/" + str(comic_index) + '/'
    page = requests.get(base_url)
    # page
    # page.status_code
    # page.content
    soup = BeautifulSoup(page.content, 'html.parser')
    # print(soup.prettify())
    # list(soup.children)
    # [type(item) for item in list(soup.children)]
    comic = list(soup.find_all(id="comic")) 
    # comic
    # print (base_url)
    
    ## String processing steps 
    
    ## Step 1: Extract the Title
    comic_title = str(comic[0])
    comic_title = comic_title.split(sep = "src")[0]
    comic_title = comic_title.split(sep = "alt")[1]

    comic_title = comic_title[2:len(comic_title)-2]
    
    ## Step 2: Extract the comic's alt-text 
    comic_alt = str(comic[0])
    comic_alt = comic_alt.split(sep = "src")[2]
    print (comic_alt)
    comic_alt = comic_alt.split(sep = "title")[1]

    comic_alt = comic_alt[2:len(comic_alt)-15]

    ## Step 3: Extract the url for the image 
    comic_url_src = str(comic[0])
    comic_url_src = comic_url_src.split(sep = "src")[1]
    comic_url_src = "http://" + comic_url_src[4:len(comic_url_src)-2]
    
    ## Now add all this data into the dictionary 
    comic_dictionary[comic_index] = [base_url, comic_title, comic_alt, comic_url_src]
    
    
# -

comic_dictionary

prin

StarcoderdataJupyterScriptsDedupFiltered