Beautiful Soup Module

Source: Internet
Author: User

Beautiful soup is a python library that can extract data from HTML or XML files. It can implement the usual document navigation, searching, and modifying methods through your favorite converter. beautiful Soup helps you save hours or even days of work.

Quick Start. Use the following HTML as an example.

html_doc = """

Use beautifulsoup to parse this code and you can getBeautifulSoupAnd can output according to the structure of the standard indent format:

from bs4 import BeautifulSoupsoup = BeautifulSoup(html_doc,‘html.parser‘)print(soup.prettify())

Several simple methods to browse structured data:

soup.title<title>The Dormouse‘s story</title>soup.title.name‘title‘soup.title.string"The Dormouse‘s story"soup.title.strings<generator object _all_strings at 0x0000025B5572A780>soup.title.parent.name‘head‘soup.p<p class="title"><b>The Dormouse‘s story</b></p>soup.p[‘class‘][‘title‘]soup.a<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>soup.find_all(‘a‘)[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]soup.find(id=‘link3‘)<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

Find the links to all <A> labels in the document:

for link in soup.find_all(‘a‘):    print(link.get(‘href‘))    http://example.com/elsiehttp://example.com/laciehttp://example.com/tillie

Retrieve all text from the document:

print(soup.get_text())The Dormouse‘s storyThe Dormouse‘s storyOnce upon a time there were three little sisters; and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.

 

Beautiful Soup Module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.