Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

452
Views
Dar formato a CSS en línea con Python

Tengo un archivo HTML con el siguiente código dentro:

 <...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...>

Quiero cambiar el ancho de todas las áreas de texto a 200 px mediante el uso de Python 2.0. Mi primera idea fue usar BeautifulSoup y encontré este fragmento de código:

 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)

Desafortunadamente, esto solo cambia el estilo de una etiqueta. Quiero cambiar todas las etiquetas de área de texto

Probé el siguiente código:

 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

Esto genera un error "NoneType object is not subscriptable" en la línea "style = ..."

¿Alguien tiene una idea de cómo puedo realizar el formateo requerido?

Gracias

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Pruebe el siguiente enfoque:

 from bs4 import BeautifulSoup from cssutils import parseStyle with open('input.html') as f_html: soup = BeautifulSoup(f_html, 'html.parser') for textarea in soup.find_all('textarea', style=True): style = parseStyle(textarea['style']) style['width'] = '200px' textarea['style'] = style.cssText.replace('\n', ' ') with open('output.html', 'w', encoding='utf-8') as f_html: f_html.write(str(soup))

Entonces, si su HTML es:

 <html> <body> <textarea id="123" attributeX="4" attributeY="5" style="width:159px; height:50px;"></textarea> <textarea id="456" attributeX="4" attributeY="5" style="width:135px; height:50px;"></textarea> <textarea id="789" attributeX="4" attributeY="5" style="width:177px; height:50px;"></textarea> <textarea id="789" attributeX="4" attributeY="5"></textarea> </body> </html>

La salida se convertiría en:

 <html> <body> <textarea attributex="4" attributey="5" id="123" style="width: 200px; height: 50px"></textarea> <textarea attributex="4" attributey="5" id="456" style="width: 200px; height: 50px"></textarea> <textarea attributex="4" attributey="5" id="789" style="width: 200px; height: 50px"></textarea> <textarea attributex="4" attributey="5" id="789"></textarea></body> </html>

Aquí el <textarea> final no ha cambiado ya que no había estilo. Si es necesario, esto podría agregarse de la siguiente manera:

 from bs4 import BeautifulSoup from cssutils import parseStyle with open('input.html') as f_html: soup = BeautifulSoup(f_html, 'html.parser') for textarea in soup.find_all('textarea'): if 'style' in textarea.attrs: # Update existing style style = parseStyle(textarea['style']) style['width'] = '200px' textarea['style'] = style.cssText.replace('\n', ' ') else: # Add missing style textarea['style'] = 'width: 200px; height: 50px' with open('output.html', 'w', encoding='utf-8') as f_html: f_html.write(str(soup))
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!