Python plug-in miscellaneous (4) -- beautifulsoup, a Web analysis tool in Python

Source: Internet
Author: User

Well, meego's Chinese core site, miqu.com, has published a new blog.
I have introduced youPyqueryNext we will introduceBeautifulsoupBeautiful soup is a built-in Python web analysis tool named beautiful butterfly. Oh, sometimes it's as beautiful as a butterfly.
The first section introduces:
Beautiful soupIs a python html/XML processor designed to quickly convert web page capture. The following features supportBeautiful soup:

    • Beautiful soupNo, even if you give him a broken tag. He generates a conversion DOM tree and tries to make it consistent with the meaning of your original document. This measure can usually meet your data collection needs.
    • Beautiful soupProvides some simple methods and Python-like syntax to find, search, and modify a conversion tree: A toolset helps you parse a tree and release what you need. You do not need to create your own parsing tools for each application.
    • Beautiful soup AutomaticDocuments to be sent inConvert to unicode encodingAnd in the output to the UTF-8 ,. Unless this document does not specify the encoding method or the beautiful Soup does not automatically detect the encoding method, you need to manually specify the encoding method, otherwise you do not need to consider the encoding problem.

Beautiful soupConvert anything you give him, and then do those conversion tasks for you. You can run the command to "find all links", "Find all links whose classes are externallink", or "Find all links whose URLs match" foo.com ", even "find the headers in bold and return them to me".
Valuable data in poorly designed websites can be locked once by you. It would have taken several hours to work, and you can use beautiful Soup within minutes.
Let's get started quickly:
First, reference the package:

    1. From beautifulsoup import beautifulsoup # for processing html
    2. From beautifulsoup import beautifulstonesoup # for processing XML
    3. Import beautifulsoup # To get everything [/font] [/color]

CopyCode

The following code demonstrates the basic usage of beautiful Soup. You can copy and paste the code and run it yourself.

  1. From beautifulsoup import beautifulsoup
  2. Import re
  3. Doc = ['<HTML>
  4. '<Body> <p id = "firstpara" align = "center"> This is paragraph <B> one </B> .',
  5. '<P id = "secondpara" align = "blah"> This is paragraph <B> two </B> .',
  6. '</Html>']
  7. Soup = beautifulsoup (''. Join (DOC ))
  8. Print soup. pretloads ()
  9. # <HTML>
  10. # <Head>
  11. # <Title>
  12. # Page title
  13. # </Title>
  14. # </Head>
  15. # <Body>
  16. # <P id = "firstpara" align = "center">
  17. # This is paragraph
  18. # <B>
  19. # One
  20. # </B>
  21. #.
  22. # </P>
  23. # <P id = "secondpara" align = "blah">
  24. # This is paragraph
  25. # <B>
  26. # Two
  27. # </B>
  28. #.
  29. # </P>
  30. # </Body>
  31. # </Html>

Copy code

The following is a method to parse the document:

    1. Soup. Contents [0]. Name
    2. # U'html'
    3. Soup. Contents [0]. Contents [0]. Name
    4. # U'head'
    5. Head = soup. Contents [0]. Contents [0]
    6. Head. Parent. Name
    7. # U'html'
    8. Head. Next
    9. # <Title> page title </title>
    10. Head. nextsibling. Name
    11. # U'body'
    12. Head. nextsibling. Contents [0]
    13. # <P id = "firstpara" align = "center"> This is paragraph <B> one </B>. </P>
    14. Head. nextsibling. Contents [0]. nextsibling
    15. # <P id = "secondpara" align = "blah"> This is paragraph <B> two </B>. </P>

Copy code

The next step is to search for tags contained in a document in a dozen ways or tags containing specified attributes.

    1. Titletag = soup.html. Head. Title
    2. Titletag
    3. # <Title> page title </title>
    4. Titletag. String
    5. # U'page title'
    6. Len (soup ('P '))
    7. #2
    8. Soup. findall ('P', align = "center ")
    9. # [<P id = "firstpara" align = "center"> This is paragraph <B> one </B>. </P>]
    10. Soup. Find ('P', align = "center ")
    11. # <P id = "firstpara" align = "center"> This is paragraph <B> one </B>. </P>
    12. Soup ('P', align = "center") [0] ['id']
    13. # U'firstpara'
    14. Soup. Find ('P', align = Re. Compile ('^ B. *') ['id']
    15. # U'secondpara'
    16. Soup. Find ('P'). B. String
    17. # U'one'
    18. Soup ('P') [1]. B. String
    19. # U'two'

Copy code

You can also modify the document

  1. Titletag ['id'] = 'thetitle'
  2. Titletag. Contents [0]. replacewith ("New title ")
  3. Soup.html. Head
  4. # <Head> <title ID = "thetitle"> New title </title>
  5. Soup. P. Extract ()
  6. Soup. prettasks ()
  7. # <HTML>
  8. # <Head>
  9. # <Title ID = "thetitle">
  10. # New title
  11. # </Title>
  12. # </Head>
  13. # <Body>
  14. # <P id = "secondpara" align = "blah">
  15. # This is paragraph
  16. # <B>
  17. # Two
  18. # </B>
  19. #.
  20. # </P>
  21. # </Body>
  22. # </Html>
  23. Soup. P. replacewith (soup. B)
  24. # <HTML>
  25. # <Head>
  26. # <Title ID = "thetitle">
  27. # New title
  28. # </Title>
  29. # </Head>
  30. # <Body>
  31. # <B>
  32. # Two
  33. # </B>
  34. # </Body>
  35. # </Html>
  36. Soup. Body. insert (0, "This page used to have ")
  37. Soup. Body. insert (2, "& lt; P & gt; tags! ")
  38. Soup. Body
  39. # <Body> This page used to have <B> two </B> & lt; P & gt; tags! </Body>

Copy code

Finally, we will provide you with the beautiful Soup documentation. Hope to help you.

 

ReprintedArticle, Please indicate fromMiquyi

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.