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

138
Views
Buscar por estilo (color) con request_html

Tengo que usar requests_html para el contenido de JavaScript. Código:

 <td class="text-left worker-col truncated"><a href="/account/0x58e0ff2eb3addd3ce75cc3fbdac3ac3f4e21fa/38-G1x" style="color:red">38-G1</a></td>

Quiero encontrar todos los nombres (38-G1 en este caso) con color rojo. Quiero buscarlos por style="color:red" . ¿Es esto posible con requests_html ? ¿Como puedo hacer esto?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Uso tanto la sesión html como el selenio con bs4. Selenium funciona bien, pero la sesión html no puede procesar js.

Código con selenio. (Éxito)

 from bs4 import BeautifulSoup import time from selenium import webdriver driver = webdriver.Chrome('chromedriver.exe') url = URL driver.get(url) time.sleep(8) soup = BeautifulSoup(driver.page_source, 'html.parser') for t in soup.select('table.table.table-bordered.table-hover.table-responsive tr'): txt= t.select_one('td:nth-child(2) > a') text= txt.text if txt else None print(text)

Producción:

 38-G15 47_G15_2 47-G1 49-O15 90_GGX 91_ASF 105_MGPM_3 112-GG3 121-APRO 188-MGPM1 198-AP 248_MGPM_1 262-GUD 265_ASF 302-AD 355-GUD.2 Rig_3471855 rigEdge 107_MGPM_3 None None

Código con sesión html (sin renderizar js)

 from bs4 import BeautifulSoup from requests_html import HTMLSession session = HTMLSession() response = session.get(URL) soup = BeautifulSoup(response.content, 'html.parser') for t in soup.select('table.table.table-bordered.table-hover.table-responsive tr'): txt= t.select_one('td:nth-child(2) > a') text= txt.text if txt else None print(text)
about 4 years ago · Juan Pablo Isaza Report

0

Editar: en este caso, JavaScript agrega el estilo después de cargar la página, por lo que debe esperar a que se cargue toda la página antes de rasparla, por lo que Selenium es el camino a seguir.

Puedes agarrar la página de esta manera, tal como lo hizo Fazlul:

 from bs4 import BeautifulSoup as bs import time from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options) driver.get("URL") time.sleep(5) html = bs(driver.page_source, 'html.parser')

luego puede usar un selector de comodín CSS, luego imprimir su texto interno:

 anchors = html.select('a[style*="color:red"]') print([a.text for a in anchors])

O

Podría encontrar todas las etiquetas <a> y ponerlas en una lista si tienen ese atributo.

 anchors = html.select('a') names = [] for a in anchors: if 'style' in a.attrs and "color:red" in a.attrs['style']: names.append(a.text)

Editar: Veo que otro usuario le dio una solución con BeautifulSoup y me gustaría agregar que si es nuevo en webscraping, pero planea aprender más, también le recomiendo que aprenda a usar BeautifulSoup. No solo es más potente, sino que su base de usuarios es mucho más grande, por lo que es más fácil encontrar soluciones para su problema.

about 4 years ago · Juan Pablo Isaza 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!