Python uses the Beautiful Soup module to modify the content method example, pythonsoup

Source: Internet
Author: User

Python uses the Beautiful Soup module to modify the content method example, pythonsoup

Preface

In fact, in addition to searching and navigation, the Beautiful Soup module can also modify the content of HTML/XML documents. This means that you can add or delete tags, modify tag names, change tag attribute values, and modify text content. This article introduces in detail how to use the Beautiful Soup module to modify the content of Python. I will not talk about it below. Let's take a look at the detailed introduction.

Modify tags

The example HTML document is as follows:

html_markup=""" <div class="ecopyramid"> <ul id="producers">  <li class="producerlist">  <div class="name">plants</div>  <div class="number">100000</div>  </li>  <li class="producerlist">  <div class="name">algae</div>  <div class="number">100000</div>  </li> </ul> </div> """

Modify Tag Name

soup = BeautifulSoup(html_markup,'lxml')producer_entries = soup.ulprint producer_entries.nameproducer_entries.name = "div"print producer_entries.prettify()

Modify tag attribute values

# Modifying tag attributes # updating the existing tag attribute value producer_entries ['id'] = "producers_new_value" print producer_entries.prettify () # adding a new property value to a tag: producer_entries ['class'] = "newclass" print producer_entries.pret#() # deleting the tag property value del producer_entries ['class'] print producer_entries.pret ()

Add a new tag

We can use the new_tag method to generate a new tag, and then useappend() ,insert(),insert_after(),insert_before()Method to add tags to the HTML tree.

For example, add a li tag to the ul tag of the preceding HTML document. First, you need to generate a new li tag and insert it into the HTML tree structure. Insert the corresponding div tag in the li tag.

# Add a new tag # new_tag generates a tag object new_li_tag = soup. new_tag ("li") # new_atag = soup. new_tag ("a", href = "www.example.com" rel = "external nofollow") new_li_tag.attrs = {'class': 'producerlist'} soup = BeautifulSoup (html_markup, 'lxml ') producer_entries = soup. ul # Use the append () method to add producer_entries.append (new_li_tag) print producer_entries.pret#() # generate two div labels and insert them into the li label new_div_name_tag = soup. new_tag ("div") new_div_name_tag ['class'] = "name" new_div_number_tag = soup. new_tag ("div") new_div_number_tag ["class"] = "number" # insert new_li_tag.insert (0, new_div_name_tag) new_li_tag.insert (1, priority) using the insert () method) print new_li_tag.pret.pdf ()

Modify string content

You can usenew_string(),append(),insert() Method.

# Modifying string content # Use. string attribute to modify the content of the string new_div_name_tag.string = 'new _ div_name '# Use. the append () method adds the string content new_div_name_tag.append ("producer") # Use the new_string () method of the soup object to generate the string new_string_toappend = soup. new_string ("producer") new_div_name_tag.append (new_string_toappend) # Use the insert () method to insert new_string_toinsert = soup. new_string ("10000") new_div_number_tag.insert (0, new_string_toinsert) print producer_entries.prettify ()

Delete a label Node

The Beautiful Soup module providesdecompose()Andextract()Method to delete a node.

decompose()The method to delete a node not only deletes the current node, but also deletes its child nodes.

extract()This method is used to delete a node or string from the HTML tree.

# Delete the node third_producer = soup. find_all ("li") [2] # Use the decompose () method to delete the div_name = partition () print third_producer.prettify () # Use the extract () method to delete the node third_producer_removed = partition () print soup. prettasks ()

Delete TAG content

A Tag may have a NavigableString object or a Tag object as its sub-node. To remove all these sub-nodes, you can useclear()Method. This will remove all. content of the tag.

Other methods for modifying content

In addition to the methods mentioned above, there are other methods used to modify the content.

insert_after()Andinsert_before()Method

The preceding two methods can insert a tag or string before or after a tag or string. The method can only receive one parameter, either the NavigableString object or the Tag object.

replace_with()Method

This method replaces the original tag or string with a new tag or string, and can receive a tag or string as the input.

wrap()Andunwrap() Method

wrap() You can use another tag to enclose a tag or string.

unwrap() Method andwrap()The method is the opposite.

# Wrap () method li_tags = soup. find_all ('lil') for li in li_tags: new_div_tag = soup. new_tag ('div ') li. wrap (new_div_tag) print soup. pretiterator () # unwrap () method li_tags = soup. find_all ("li") for li in li_tags: li. div. unwrap () print soup. prettasks ()

Summary

The above is all about Python's use of the Beautiful Soup module to modify the content. I hope the content in this article will help you learn or use python. If you have any questions, please leave a message, thank you for your support.

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.