How Python uses BeautifulSoup to analyze Web page information _python

Source: Internet
Author: User

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.

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.