{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "\n", "\n", "___\n", "
Content Copyright by Pierian Data
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Web Scraping Exercises \n", "\n", "## Complete the Tasks Below" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**TASK: Import any libraries you think you'll need to scrape a website.**" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**TASK: Use requests library and BeautifulSoup to connect to http://quotes.toscrape.com/ and get the HMTL text from the homepage.**" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\n\\n\\n\\t\\n\\tQuotes to Scrape\\n \\n \\n\\n\\n
\\n
\\n
\\n

\\n Quotes to Scrape\\n

\\n
\\n
\\n

\\n \\n Login\\n \\n

\\n
\\n
\\n \\n\\n
\\n
\\n\\n
\\n “The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”\\n by Albert Einstein\\n (about)\\n \\n
\\n Tags:\\n \\n \\n change\\n \\n deep-thoughts\\n \\n thinking\\n \\n world\\n \\n
\\n
\\n\\n
\\n “It is our choices, Harry, that show what we truly are, far more than our abilities.”\\n by J.K. Rowling\\n (about)\\n \\n
\\n Tags:\\n \\n \\n abilities\\n \\n choices\\n \\n
\\n
\\n\\n
\\n “There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”\\n by Albert Einstein\\n (about)\\n \\n
\\n Tags:\\n \\n \\n inspirational\\n \\n life\\n \\n live\\n \\n miracle\\n \\n miracles\\n \\n
\\n
\\n\\n
\\n “The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”\\n by Jane Austen\\n (about)\\n \\n
\\n Tags:\\n \\n \\n aliteracy\\n \\n books\\n \\n classic\\n \\n humor\\n \\n
\\n
\\n\\n
\\n “Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.”\\n by Marilyn Monroe\\n (about)\\n \\n
\\n Tags:\\n \\n \\n be-yourself\\n \\n inspirational\\n \\n
\\n
\\n\\n
\\n “Try not to become a man of success. Rather become a man of value.”\\n by Albert Einstein\\n (about)\\n \\n
\\n Tags:\\n \\n \\n adulthood\\n \\n success\\n \\n value\\n \\n
\\n
\\n\\n
\\n “It is better to be hated for what you are than to be loved for what you are not.”\\n by André Gide\\n (about)\\n \\n
\\n Tags:\\n \\n \\n life\\n \\n love\\n \\n
\\n
\\n\\n
\\n “I have not failed. I've just found 10,000 ways that won't work.”\\n by Thomas A. Edison\\n (about)\\n \\n
\\n Tags:\\n \\n \\n edison\\n \\n failure\\n \\n inspirational\\n \\n paraphrased\\n \\n
\\n
\\n\\n
\\n “A woman is like a tea bag; you never know how strong it is until it's in hot water.”\\n by Eleanor Roosevelt\\n (about)\\n \\n
\\n Tags:\\n \\n \\n misattributed-eleanor-roosevelt\\n \\n
\\n
\\n\\n
\\n “A day without sunshine is like, you know, night.”\\n by Steve Martin\\n (about)\\n \\n
\\n Tags:\\n \\n \\n humor\\n \\n obvious\\n \\n simile\\n \\n
\\n
\\n\\n \\n
\\n
\\n \\n

Top Ten tags

\\n \\n \\n love\\n \\n \\n \\n inspirational\\n \\n \\n \\n life\\n \\n \\n \\n humor\\n \\n \\n \\n books\\n \\n \\n \\n reading\\n \\n \\n \\n friendship\\n \\n \\n \\n friends\\n \\n \\n \\n truth\\n \\n \\n \\n simile\\n \\n \\n \\n
\\n
\\n\\n
\\n \\n\\n'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**TASK: Get the names of all the authors on the first page.**" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Albert Einstein',\n", " 'André Gide',\n", " 'Eleanor Roosevelt',\n", " 'J.K. Rowling',\n", " 'Jane Austen',\n", " 'Marilyn Monroe',\n", " 'Steve Martin',\n", " 'Thomas A. Edison'}" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "authors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**TASK: Create a list of all the quotes on the first page.**" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#CODE HERE" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”',\n", " '“It is our choices, Harry, that show what we truly are, far more than our abilities.”',\n", " '“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”',\n", " '“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”',\n", " \"“Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.”\",\n", " '“Try not to become a man of success. Rather become a man of value.”',\n", " '“It is better to be hated for what you are than to be loved for what you are not.”',\n", " \"“I have not failed. I've just found 10,000 ways that won't work.”\",\n", " \"“A woman is like a tea bag; you never know how strong it is until it's in hot water.”\",\n", " '“A day without sunshine is like, you know, night.”']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "quotes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**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.**" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "love\n", "\n", "\n", "inspirational\n", "\n", "\n", "life\n", "\n", "\n", "humor\n", "\n", "\n", "books\n", "\n", "\n", "reading\n", "\n", "\n", "friendship\n", "\n", "\n", "friends\n", "\n", "\n", "truth\n", "\n", "\n", "simile\n", "\n" ] } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**TASK: Notice how there is more than one page, and subsequent pages look like this http://quotes.toscrape.com/page/2/. Use what you know about for loops and string concatenation to loop through all the pages and get all the unique authors on the website. Keep in mind there are many ways to achieve this, also note that you will need to somehow figure out how to check that your loop is on the last page with quotes. For debugging purposes, I will let you know that there are only 10 pages, so the last page is http://quotes.toscrape.com/page/10/, but try to create a loop that is robust enough that it wouldn't matter to know the amount of pages beforehand, perhaps use try/except for this, its up to you!**" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# CODE HERE" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are lots of other potential solutions that are even more robust and flexible, the main idea is the same though, use a while loop to cycle through potential pages and have a break condition based on the invalid page." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }