A small piece of Python code with HTML format-word released 2

Source: Internet
Author: User

# Coding:utf8

?

from bs4 import beautifulsoup

Import Re

?

Html_doc = "" "

<body>

<p class= "title" ><b>the dormouse ' s story</b></p>

?

<p class= "Story" >once upon a time there were three Little sisters; and their names were

<a href= "Http://example.com/elsie" class= "sister" id= "Link1" >ELSIE</A>

<a href= "Http://example.com/lacie" class= "sister" id= "Link2" >Lacie</a> and

<a href= "Http://example.com/tillie" class= "sister" id= "Link3" >Tillie</a>;

And they lived at the bottom of a well.</p>

?

<p class= "Story" >...</p>

"""

Soup = beautifulsoup(html_doc,' Html.parser ', from_encoding=' utf-8 ' )

?

Print ' links '

Links = soup. find_all(' a ' )

for link in links :

#print Link

Print link. name, link[' href '],link. get_text( )

?

print ' Get a separate link "

link_code = Soup . find ( ' a ' , href = ' Http://example.com/lacie ' )

Print link_code. name, link_code[' href '],link_code. get_text( )

?

print ' Regular expression matches "

link_code = Soup . find ( ' a ' , Href = re . compile ( r "ill" Span style= "Color:navy" > )

#< ctrl+1> According to re direct import modules not imported

Print link_code. name, link_code[' href '],link_code. get_text( )

?

print ' Get p Paragraph text '

p_class = Soup . find ( ' P ' , Class_ = ' title ' )

Print p_class. name,p_class. get_text()

?

Yesterday

A small piece of Python code with HTML format-word released 2

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.