Python uses BeautifulSoup to analyze Web page information, pythonsoup

Source: Internet
Author: User

Python uses BeautifulSoup to analyze Web page information, pythonsoup

This example describes how python uses BeautifulSoup to analyze Web page information. Share it with you for your reference. The details are as follows:

This python code finds all links on the web page, analyzes all span labels, and finds the span content of the class containing titletext.
Copy codeThe Code is as follows: # import the library used to query a website
Import urllib2

# Specify the url you want to query
Url = "http://www.python.org"

# Query the website and return the html to the variable 'page'
Page = urllib2.urlopen (url)

# Import the Beautiful soup functions to parse the data returned from the website
From BeautifulSoup import BeautifulSoup

# Parse the html in the 'page' variable, and store it in Beautiful Soup format
Soup = BeautifulSoup (page)

# To print the soup. head is the head tag and soup. head. title is the title tag
Print soup. head
Print soup. head. title

# To print the length of the page, use the len function
Print len (page)

# Create a new variable to store the data you want to find.
Tags = soup. findAll ('A ')

# To print all the links
Print tags

# To get all titles and print the contents of each title
Titles = soup. findAll ('span ', attrs = {'class': 'titletext '})
For title in allTitles:
Print title. contents

I hope this article will help you with Python programming.

Related Article

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.