Python Examples: Extract Data With BeautifulSoup

Python Zac 2 years ago (2018-02-19) 3817 views 0 comments

Description:

In order to develop Web Scarper, BeautifulSoup is a useful module to extract data.

BeautifulSoup is easy to use. The code is not hard to understand.

Today, I will show you the examples about how to use BeautifulSoup to extract images.

 

 

Example 1: Extract one element.

 

You can use the mouse Right-Click to inspect element that you can find the image URL.

BeautifulSoup extract image

 

You might think the URL is not complete. So what should you do?

It is simple.

Why short_url[6:]? If you can not understand the meaning, you can view my last article.

Hope this is helpful to you: Python Slice: Start, Stop and Step

 

Example 2: Extract more than one element.

 

 

Target Analysis

BeautifulSoup image Object Analysis

 

Why use “try & except”

Both of Videos and Images are inside the <div class=”post-content”>, so when I use “item.img” to extract all images, the program cannot fin the <img> in Video post-content. It will occurred the NoneType error. In order to continue the program, I use the “try & except” to solve that error.

 

 

 

More BeautifulSoup examples

 

Scraping Tumblr Photos Under 50 Lines of Python Code

Description: I will show you how can I crawl Tumblr photos and save to local storage under 50 lines of Python code. I hope this article is useful for someone who is new to Python Web Crawling. You can know how to use modules bs4(Beautifulsoup) and requests to develop a Web Crawler. If you like […]

Download Instagram Photo Through Python

Description: As you know, there are many websites can let you download Instagram photos online. What if we use Python to achieve the same effect? It is not difficult.     Showing results For example, we want to download Ariana Grande’s photos. Now we can to go her personal Instagram home page and click one of […]

If you have any questions about my articles, please comment it.

Thanks.

Like (3)
Comment
Cancel comment

emoji Image Bold Strilethrough Center Italic

Your email and address

  • Name (Required)
  • Email (Required)
  • Website(Optional)