Web Scraping Beautifulsoup 4



Tremendous Boost for Quantum Research in the Amsterdam Ecosystem. The financial injection of 615 million euros from the National Growth Fund of the Dutch Ministry of Economic Affairs and Climate Policy gives a tremendous boost to quantum research and development in the Amsterdam ecosystem. Tremendous Boost for Quantum Research in the Amsterdam Ecosystem. The financial injection of 615 million euros from the National Growth Fund of the Dutch Ministry of Economic Affairs and Climate Policy gives a tremendous boost to quantum research and development in the Amsterdam ecosystem.

  1. Web Scraping Beautiful Soup 4th Grade
  2. Web Scraping Beautifulsoup 4 Pro
  3. Web Scraping Beautifulsoup 4 -
  4. Web Scraping Using Beautifulsoup
Latest version

Released:

Screen-scraping library

Project description

Beautiful Soup is a library that makes it easy to scrape informationfrom web pages. It sits atop an HTML or XML parser, providing Pythonicidioms for iterating, searching, and modifying the parse tree.

To go beyond the basics, comprehensive documentation is available.

Since 2012, Beautiful Soup has been developed as a Python 2 librarywhich is automatically converted to Python 3 code as necessary. Thismakes it impossible to take advantage of some features of Python3.

For this reason, I plan to discontinue Beautiful Soup's Python 2support at some point after December 31, 2020: one year after thesunset date for Python 2 itself. Beyond that point, new Beautiful Soupdevelopment will exclusively target Python 3. Of course, olderreleases of Beautiful Soup, which support both versions, will continueto be available.

If you use Beautiful Soup as part of your professional work, please consider aTidelift subscription.This will support many of the free software projects your organizationdepends on, not just Beautiful Soup.

If you use Beautiful Soup for personal projects, the best way to saythank you is to readTool Safety, a zine Iwrote about what Beautiful Soup has taught me about softwaredevelopment.

The bs4/doc/ directory contains full documentation in Sphinxformat. Run make html in that directory to create HTMLdocumentation.

Beautiful Soup supports unit test discovery from the project root directory:

If you checked out the source tree, you should see a script in thehome directory called test-all-versions. This script will run the unittests under Python 2, then create a temporary Python 3 conversion ofthe source and run the unit tests again under Python 3.

Release historyRelease notifications | RSS feed

4.9.3

4.9.2

4.9.1

4.9.0

4.8.2

Web Scraping Beautiful Soup 4th Grade

4.8.1

4.8.0

4.7.1

4.7.0

4.6.3

4.6.2

4.6.1

4.6.0

4.5.3

4.5.2

4.5.1

4.5.0

4.4.1

4.4.0

4.3.2

4.3.1

4.3.0

4.2.1

4.2.0

4.1.3

4.1.2

4.1.1

4.1.0

4.0.5

4.0.4

4.0.3

4.0.2

4.0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for beautifulsoup4, version 4.9.3
Filename, sizeFile typePython versionUpload dateHashes
Filename, size beautifulsoup4-4.9.3-py2-none-any.whl (115.9 kB) File type Wheel Python version py2 Upload dateHashes
Filename, size beautifulsoup4-4.9.3-py3-none-any.whl (115.8 kB) File type Wheel Python version py3 Upload dateHashes
Filename, size beautifulsoup4-4.9.3.tar.gz (376.0 kB) File type Source Python version None Upload dateHashes
Close

Hashes for beautifulsoup4-4.9.3-py2-none-any.whl

Hashes for beautifulsoup4-4.9.3-py2-none-any.whl
AlgorithmHash digest
SHA2564c98143716ef1cb40bf7f39a8e3eec8f8b009509e74904ba3a7b315431577e35
MD57131ff441c704c172ae022881d9d5998
BLAKE2-2561cd98c507915ff962b9e854b477b203c171074f59cce9465dac9f71c2b57ebd6
Close

Hashes for beautifulsoup4-4.9.3-py3-none-any.whl

Hashes for beautifulsoup4-4.9.3-py3-none-any.whl
AlgorithmHash digest
SHA256fff47e031e34ec82bf17e00da8f592fe7de69aeea38be00523c04623c04fb666
MD5bc7da27acae18d1907995f602ee572fe
BLAKE2-256d141e6495bd7d3781cee623ce23ea6ac73282a373088fcd0ddc809a047b18eae
Close

Hashes for beautifulsoup4-4.9.3.tar.gz

Hashes for beautifulsoup4-4.9.3.tar.gz
AlgorithmHash digest
SHA25684729e322ad1d5b4d25f805bfa05b902dd96450f43842c4e99067d5e1369eb25
MD557fd468ae3eb055f6871106e8f7813e2
BLAKE2-2566bc3d31704ae558dcca862e4ee8e8388f357af6c9d9acb0cad4ba0fbbd350d9a

Web Scraping means navigating structured elements on a website, and deeply going to next layers. Incoming big data will be retrieved and formated in desired styles. We apply Python BeautifulSoup to a simple example for scraping with step-by-step tutorials.

All codes here are not complicated, so you can easily understand even though you are still students in school. To benefit your learning, we will provide you download link to a zip file thus you can get all source codes for future usage.

Estimated reading time: 10 minutes

EXPLORE THIS ARTICLE
TABLE OF CONTENTS


1Python Web Scraping

3Scraping By Beautifulsoup

FINALConclusion

BONUS
Source Code Download

We have released it under the MIT license, so feel free to use it in your own project or your school homework.

Download Guideline

  • Prepare Python environment for Windows by clicking Python Downloads, or search a Python setup pack for Linux.
  • The pack of Windows version also contains pip install for you to obtain more Python libraries in the future.

THE BASICS
Python Web Scraping

Web pages are written using HTML to be structured texts. Therefore, people can obtain organized information of websites by scraping. Because Python is good at that, we will introduce its library BeautifulSoup in the article.

What is BeautifulSoup?

BeautifulSoup version 4 is a famous Python library for web scraping. In addition, there was BeautifulSoup version 3, and support for it will be dropped on or after December 31, 2020. People had better learn newer versions. Below is the definition from BeautifulSoup Documentation.

BeautifulSoup Installation

If you already download and setup Python and its tool pip in Windows or Linux, you can install BeautifulSoup 4 package bs4 by pip install command lines, and then check the result by pip show.

Scraping An Example Website

We should choose an example website to start. Focusing only on the content about laptop computer, thus the portion of an Indian shopping website, Snapdeal Laptops, will be suitable to our target.

There are two layers, top layer for a product list and bottom layer for details in specification. For safety, we suggest a process of saving pages and then retrieve data from them. Therefore, the page contents along with images are downloaded. Below is the running process.

Next, you will learn how to scaping web pages by BeautifulSoup. The result will be saved as JSON format in a file named result.json.

STEP 1
Finding How Web Pages Link

The previous section let us know what type of data we will meet, so we need to inspect these HTML structured texts to find entry points for BeautifulSoup scraping to start.

Finding The Relationship

Probably, the website you want to crawl has more layers than that in this example. As there is no general rule for various web pages, it is better to get a entry point that BeautifulSoup can start from by finding how web pages link.

Below lists the JSON-styled data related to a specific laptop computer. Except that the fields under 'highlight' is found from layer 2, other fields come from layer 1. For example, 'title' indicates the product name with brief specification, and 'img_url' can be used to download product pictures.

What to Inspect in Layer 1

From the view of HTML document, let us continue inspecting Layer 1 in file laptop.html. In other words, by going through HTML structured text, BeautifulSoup can locate the key feature of class='product-tuple-image' for scraping items like <a pogId=, <a href=, <source srcset=, and <img title= to be pogid, href, img_url, and title, respectively. Where href directs to url in Layer 2 for that product.

Web Scraping Beautifulsoup 4 Pro

Similarly, with another feature of class='product-tuple-description', BeautifulSoup can continue scraping <span product-desc-price=, <span product-price=, and <div product-discount= to retrieve JSON fields price, price_discount, and discount. So far, all items in Layer 1 have been discovered.

What to Inspect in Layer 2

Further, BeautifulSoup traverses the HTML file of Layer 2 such as 638317853217.html. As mentioned previously, that is for detailed specification of laptops. The task in Layer 2 can be done by scraping items inside the feature of class='highlightsTileContent '.

STEP 2
Scraping By Beautifulsoup

Before scraping, we got to introduce a popular Python library PyPI requests to get contents from websites. Then, BeautifulSoup will perform iteratedly in layers to convert all information into JSON-style data.

PyPI requests

PyPI requests is an elegant and simple HTTP library for Python. In the following paragraph, we leverage it to read text from web pages and save as HTML files. Of course, you can install it by issuing pip install requests in command box.

PyPI requests requests.get() not only get web pages, but also pull down binary data for pictures like that in download_image().

Python Scraping in Layer 1

Let us start from getLayer_1() in which each web page has been saved before BeautifulSoup parsing proceed.

Saving contents as backup is helpful for debuging. Once exceptions happens while BeautifulSoup scraping, it is hard for you to find again the exact content you need from massive web pages. In addition to that, too frequent data scraping may trigger prohibition of some websites.

For BeautifulSoup, the very first expression like soup = BeautifulSoup(page, 'html.parser') need to select one kind of parsers. The parser could be html.parser or html5lib, whose difference can be found in Differences between parsers.

Based on understanding about what the text structure is in STEP 1, we find prices from the class of product-tuple-description by using attrs={} with which BeautifulSoup anchors a lot of locations in this example. Apart from prices, pictures are done in the same way.

Layer 1 can discover data about titles, images, and prices. Importantly, you should notice that BeautifulSoup uses find_all() or find() to gather information for all pieces or one piece, like the usage in database.

bs4crawler.py

For each page in Layer 2, 'highlight': getLayer_2() is called iteratedly to retrieve more. Finally, json.dump() save JSON-formated data as a file. Subsequently, let us go through steps for BeautifulSoup scraping in Layer 2 in next paragraph.

Python Scraping in Layer 2

Like the way in Layer 1, getLayer_2() find more product details by locating the class of highlightsTileContent. Then, in a loop, it store searched data in an array. You can check what is in this array in JSON style as discussed in STEP 1.

STEP 3
Handling Exception

BeautifulSoup scraping won’t be smooth continuously, because the input HTML elements may be partial missing. In that case, you have to take actions to avoid interrupt, and keep the entire procedure going on.

Why Will Exceptions Probably Occur?

In laptop.html, if every product has identical fields, there will be no exception. However, when any product has less fields than that of other normal products, it could happen like what the following Python scripts express.

Web

Always, you have to deal with every kind of exception, so as to prevent the scrapying process from interrupt. Imaging that programs suddenly exit due to errors after crawling several thousands of pieces, the loss won’t be little.

What Exceptions to Handle

Here list only two conditions. However, you may encounter extra ones when scrapying more and more web pages.

  • KeyError means the HTML element to search for does not exist. You may have been aware of an alternative HTML element to replace with, thus in this example, you can replace find('img')['src'] with find('source')['srcset']. However, if no alternation, just ignore it.
  • # h2#Final#Conclusion
  • FINAL
    Conclusion

    Web Scraping Beautifulsoup 4 -

    We didn’t write detailed skills in BeautifulSoup Documentation, but show many opinions and directions about scraping in Python. Therefore, if you are not familiar with skills, please refer to online resources for practices.

    Thank you for reading, and we have suggested more helpful articles here. If you want to share anything, please feel free to comment below. Good luck and happy coding!

    Suggested Reading

    Web Scraping Using Beautifulsoup

    • 4 Practices for Python File Upload to PHP Server
    • Compare Python Dict Index and Other 5 Ops with List