开发者

Can Pythons HTMLParser edit/change a HTML elements innerText or ONLY read it

开发者 https://www.devze.com 2023-04-10 23:37 出处:网络
I am using the python module HTMLParser.py I am able to parse HTML correctly but is there an option to change a HTML elements data(innerText)?

I am using the python module HTMLParser.py

I am able to parse HTML correctly but is there an option to change a HTML elements data(innerText)?

Do you know how I can 开发者_如何学Pythondo this with the module HTMLParser?


No, the HTMLParser does just that: it parses through your HTML.

You're probably looking for Beautiful Soup. It'll create a ParseTree--a Pythonic tree of objects representing the HTML elements of your document. Then, you can search up the object (element) you want, assign it a new value, and voila!

Stolen shamelessly from the documentation:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup("<b>Argh!</b>")
soup.find(text="Argh!").replaceWith("Hooray!")
print soup
# <b>Hooray!</b>
0

精彩评论

暂无评论...
验证码 换一张
取 消