I have an HTML file with following code inside:
<...some tags...>
<textarea id="123" attributeX="4" attributeY="5" style="width:159px; height:50px; other styles;">
<textarea id="456" attributeX="4" attributeY="5" style="width:135px; height:50px; other styles;">
<textarea id="789" attributeX="4" attributeY="5" style="width:177px; height:50px; other styles;">
<...some other tags...>
I want to change the width of all textareas to 200px by the use of Python 2.0. My first idea was to use BeautifulSoup and I found this code snippet:
from bs4 import BeautifulSoup
from cssutils import parseStyle
html = '<td style="font-size: .8em; font-family: monospace; background-color: rgb(244, 244, 244);"></td>'
soup = BeautifulSoup(html, 'html.parser')
style = parseStyle(soup.td['style'])
style['background-color'] = 'red'
soup.td['style'] = style.cssText
print(soup.td)
Unfortunately, this only changes the style of one tag. I want to change all textarea tags
I tried the following code:
from bs4 import BeautifulSoup
from cssutils import parseStyle
soup = BeautifulSoup(sHTML,'html.parser')
for txt in soupfindAll('textarea'):
style = parseStyle(text.textarea['style'])
style['width'] = '200px'
txt.textarea['style'] = style.cssText
This generates a "NoneType object is not subscriptable" error on the line "style = ..."
Does anyone have an idea how I can perform the required formatting?
Thanks
soupfindAllbe defined? Is this reallysoup.findAll? (prefersoup.find_all). Also, preferlxmltohtml.parser.txtis different thantext; maybe you have a typo?