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

import requests

import bs4

result=requests.get("http://www.example.com")

type(result)

result.text

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

# +
#soup(using soup we convert the string into a readable html file)
# -

soup.select('title')

soup.select('p')

res=requests.get("https://en.wikipedia.org/wiki/Justin_Bieber")  

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

soup



# +
#soup
# -

soup.select('.image') #we are requesting the webpage for infos on images present inside the class=image   

jb=soup.select('.image')[0] #now we select just the first one for our use

jb

# <img src="//upload.wikimedia.org/wikipedia/commons/thumb/d/da/Justin_Bieber_in_2015.jpg/220px-Justin_Bieber_in_2015.jpg"

imge_link=requests.get("https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Justin_Bieber_in_2015.jpg/220px-Justin_Bieber_in_2015.jpg") #provide the link of the image that we want

imge_link.content #this is a raw image in binary format

f=open("jb's_image.jpg",'wb') #we create a file and convert the binary into readable

f.write(imge_link.content) #now we just  write on it

f.close() #close the file  and done

# +
#GOAL: SELECT THE TITLE OF THE BOOK WITH 2 STAR RATING
# -

base_url= "http://books.toscrape.com/catalogue/page-{}.html"

pagenum=10
base_url.format(pagenum)

res=requests.get(base_url.format(1))

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

soup.select(".product_pod")

example=soup.select(".product_pod")[0]

example.select(".star-rating.Three")

example.select('a')

example.select('a')[1]['title']

# +
# we can check if something is rating 2 starsby(string call in, example.select(rating))
#example.select('a')[1]['title'] to grab the book title

# +
book_titles=[]
for n in range(1,51):
    scrape_url=base_url.format(n)
    res=requests.get( scrape_url)
    soup=bs4.BeautifulSoup(res.text,'lxml')
    books=soup.select('.product_pod')
    for book in books:
        if len(book.select(".star-rating.Two"))!=0:
             book_title=book.select('a')[1]['title']
             book_titles.append(book_title)
        
            
               
        
# -

book_titles

# +
#TASK 1 TO GET THE NAMES OF AUTHOR ON THE FIRST PAGE
# -

import requests

import bs4

res=requests.get("https://quotes.toscrape.com/")

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

soup

author=soup.select('.author')

author

authors=set()
for name in soup.select('.author'):
    authors.add(name.text)

authors

quotes=soup.select(".text")

quotes

quotes=[]
for quote in soup.select(".text"):
    quotes.append(quote.text)

quotes

# +
#TASK: Inspect the site and use Beautiful Soup to extract the top ten tags from the requests text shown on the top right from the home page (e.g Love,Inspirational,Life, etc...). HINT: Keep in mind there are also tags underneath each quote, try to find a class only present in the top right tags, perhaps check the span.
# -

soup.select('.tag-item')

tag_items=[]
for tag_item in soup.select('.tag-item'):
    tag_items.append(tag_item.text)

tag_items



StarcoderdataJupyterScriptsDedupFiltered