This article illustrates how Python uses BeautifulSoup to analyze Web page information. Share to everyone for your reference. Specifically as follows:
This Python code looks for all the links on the page, analyzes all the span tags, and finds the contents of the span that class contains TitleText
Copy Code code as follows:
#import the library used to query a website
Import Urllib2
#specify the URL 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 for your 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 your Python programming.