Python:beautifulsoup remove some unwanted properties

Source: Internet
Author: User

A long time ago, I saw a question that was probably:

He crawled a piece of HTML and he took the desired part (IMG tag section), but did not want to preserve some of the properties of the IMG tag,

Like what

怎么将img标签里边的 alt属性,width属性, 给去除掉啊

I am very lazy, can use the tool, do not write their own, he intended to use the RE module to deal with.

But I still want to use BeautifulSoup to deal with it. The following code is then available:

The main idea is to use Del to remove the Alt and Width properties of the img tag.

 fromBs4Importbeautifulsouphtml=' " src= [Http://127.0.0.1:80/admin/../upload/pimg1054_1.png]/>'Soup=beautifulsoup (HTML,"Html.parser")delsoup.img["alt"]delsoup.img["width"]Print(soup)

Results:

Here's the problem: someone has given you a way to work with re, and if you want to know it, you can look at it. The main idea is to match the unwanted string, and the string match succeeds after the empty string is replaced.

Attached-Ask address:

https://q.cnblogs.com/q/105540/

Python:beautifulsoup remove some unwanted properties

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.