Anyone know an elegant way to get the entire contents of a soup object as a single string?
At the moment I'm getting contents
, which is of course a list, and then iterating over it:
notices = soup.find("div", {"class" : "middlecontent"}) con = "" for content in notices.contents: con += str(content) print con
Thanks!
To convert a Tag object to a string in Beautiful Soup, simply use str(Tag) .
To extract text that is directly under an element in Beautiful Soup use the find_all(text=True, recursive=False) method.
What about contents = str(notices)
?
Or maybe contents = notices.renderContents()
, which will hide the div tag.
You can use the join() method:
notices = soup.find("div", {"class": "middlecontent"}) contents = "".join([str(item) for item in notices.contents])
Or, using a generator expression:
contents = "".join(str(item) for item in notices.contents)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With