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.