The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Python Beautiful Soup Example

TODO: Pull subtitle into page object
Code
from bs4 import BeautifulSoup

html = """
<html>
  <div class="target" style="color: #123434">Content</div>
</html>
"""

soup = BeautifulSoup(html, 'html.parser')
divs = soup.find_all('div', 'target')
div = divs[0]
style = div.attrs['style']

print(style)
Results
color: #123434

NOTE: The docs say you can use this:

style = div.attrs['style']

instead of this:

style = div['style']

but that doesn't work consistently for me.

I think that was because I was trying to do:

#+begin_example if 'thing' in element: print('here') #+end_example

and the `in` check doesn't work there. So, I'm just using `.attrs` all the time for consistency.

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title 

Python Beautiful Soup Example

-- code
-- python

from bs4 import BeautifulSoup

html = """
<html>
  <div class="target" style="color: #123434">Content</div>
</html>
"""

soup = BeautifulSoup(html, 'html.parser')
divs = soup.find_all('div', 'target')
div = divs[0]
style = div.attrs['style']

print(style)

-- results/

color: #123434

-- /results


-- p

NOTE: The docs say you can use this:


style = div.attrs['style']


instead of this:


style = div['style']

but that doesn't work consistently for me.

I think that was because I was trying to do:


#+begin_example
if 'thing' in element:
   print('here')
#+end_example

and the `in` check doesn't work there. 
So, I'm just using `.attrs` all the time
for consistency.


-- categories
-- Python
-- BeautifulSoup 

-- metadata
-- date: 2022-11-09 16:19:41
-- id: 2hklmfpb
-- status: draft 
-- type: post
-- site: aws