I was wondering if anyone knows how to add text to a tag (p, b is any tag where you can include character data). The documentation does not mention where you can do this.
I'm not sure what exactly you want, but maybe this is the beginning ...
from BeautifulSoup import BeautifulSoup, NavigableString html = "<p></p>" soup = BeautifulSoup(html) ptag = soup.find('p') ptag.insert(0, NavigableString("new")) print ptag
Outputs
<p>new</p>
The docs show some more similar examples: http://www.crummy.com/software/BeautifulSoup/documentation.html#Modifying%20the%20Parse%20Tree
>>> import BeautifulSoup >>> b=BeautifulSoup.BeautifulSoup("<p></p><p></p>") >>> for t,s in zip(b,[u'hello',u'world']): ... t.contents.append(BeautifulSoup.NavigableString(s)) ... >>> b <p>hello</p><p>world</p>
Source: https://habr.com/ru/post/1733288/More articles:What is the Java varargs (...) parameter data type? - javaLob returns null using MySQL and jpa - javaWhen will you need to change the primary key value? - sqlSDI vs MDI vs TDI vs? - mdiDesign Question - Food Application OO - designIs it possible to mix static, multi-threaded DLLs in one project? - dllNSPredicate that can recursively intersect an object graph? - objective-cRails: Basic Auth with Authlogic - authenticationFinding the best way to extract work time from a database - ruby โโ| fooobar.comDatabase for Inventory System in C # - c #All Articles