¿Cómo extraer este contenido renderizado por javascript?
Estoy usando requests_htmlpara extraer el elemento <div id="TranslationsHead">...</div>en esta URL en el que <span id="LangBar"> ... </span>está representado por JavaScript.
from requests_html import HTMLSession
session = HTMLSession()
from bs4 import BeautifulSoup
url = 'https://www.thefreedictionary.com/love'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
r = session.get(url, headers = headers)
soup = BeautifulSoup(r.content, 'html.parser')
soup.select_one('#TranslationsHead')
y su resultado es <div id="TranslationsHead"><span id="TranslationsTitle">Translations</span></div>. Lamentablemente, todavía no captura <span id="LangBar"> ... </span>.
¿Podría explicarnos cómo capturar dicho contenido?
¡Muchas gracias por tu ayuda!
Respuestas
Necesita llamar r.html.render()para renderizar la página con JavaScript:
from requests_html import HTMLSession
url = 'https://www.thefreedictionary.com/love'
session = HTMLSession()
r = session.get(url)
r.html.render()
lang_bar = r.html.find('#LangBar', first=True)
print(lang_bar.html)
Si desea embellecer la salida, importe BeautifulSoup y use:
soup = BeautifulSoup(lang_bar.html, 'html.parser')
print(soup.prettify())
Si quieres los idiomas:
for lcd in lang_bar.find('div.lcd'):
print(lcd.text)
Salidas:
Afrikaans / Afrikaans
Arabic / العربية
Bulgarian / Български
Chinese Simplified / 中文简体
Chinese Traditional / 中文繁體
Croatian / Hrvatski
Czech / Česky
Danish / Dansk
Dutch / Nederlands
Esperanto / Esperanto
Estonian / eesti keel
Farsi / فارسی
Finnish / Suomi
etc
Si desea obtener todas las traducciones, la nota eses la predeterminada:
from requests_html import HTMLSession
url = 'https://www.thefreedictionary.com/love'
session = HTMLSession()
r = session.get(url)
r.html.render()
for span in r.html.find('span.trans'):
print(span, span.text)
Salidas:
<Element 'span' class=('trans',) lang='af' style='display: none;'> liefde
<Element 'span' class=('trans',) lang='ar' style='display: none;'> حُب
<Element 'span' class=('trans',) lang='bg' style='display: none;'> любов
<Element 'span' class=('trans',) lang='br' style='display: none;'> amor
<Element 'span' class=('trans',) lang='cs' style='display: none;'> láska
<Element 'span' class=('trans',) lang='de' style='display: none;'> die Liebe
<Element 'span' class=('trans',) lang='da' style='display: none;'> kærlighed
<Element 'span' class=('trans',) lang='el' style='display: none;'> αγάπη
<Element 'span' class=('trans',) lang='es' style='display: inline;'> amor
Si desea simular un clic en un idioma y mostrar los resultados:
from requests_html import HTMLSession
url = 'https://www.thefreedictionary.com/love'
session = HTMLSession()
r = session.get(url)
script = """
() => {
if ( document.readyState === "complete" ) {
document.getElementsByClassName("fl_ko")[0].click();
}
}
"""
r.html.render(script=script, timeout=10, sleep=2)
for span in r.html.find('span.trans[style="display: inline;"]'):
print(span, span.text)
Salidas:
<Element 'span' class=('trans',) lang='ko' style='display: inline;'> 애정
<Element 'span' class=('trans',) lang='ko' style='display: inline;'> 연애
<Element 'span' class=('trans',) lang='ko' style='display: inline;'> 사랑하는 사람
<Element 'span' class=('trans',) lang='ko' style='display: inline;'> (테니스) 영점
<Element 'span' class=('trans',) lang='ko' style='display: inline;'> 사랑하다
ACTUALIZADO EN RESPUESTA AL COMENTARIO
Jupyter, Spyder, etc., usan un bucle de eventos bajo el capó y las llamadas de request-html loop.run_until_complete que generan esa excepción cuando el bucle ya se está ejecutando. ¿Ha intentado utilizar AsyncHTMLSession?
from requests_html import AsyncHTMLSession
url = 'https://www.thefreedictionary.com/love'
asession = AsyncHTMLSession()
async def get_results():
r = await asession.get(url)
await r.html.arender()
return r
r = asession.run(get_results)
lang_bar = r[0].html.find('#LangBar', first=True)
print(lang_bar.html)
O:
from requests_html import AsyncHTMLSession
url = 'https://www.thefreedictionary.com/love'
asession = AsyncHTMLSession()
script = """
() => {
if ( document.readyState === "complete" ) {
document.getElementsByClassName("fl_ko")[0].click();
}
}
"""
async def get_results():
r = await asession.get(url)
await r.html.arender(script=script, timeout=10, sleep=2)
return r
r = asession.run(get_results)
for span in r[0].html.find('span.trans[style="display: inline;"]'):
print(span, span.text)
from bs4 import BeautifulSoup
from requests_html import HTMLSession
url = 'https://www.thefreedictionary.com/love'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
session = HTMLSession()
resp = session.get(url, headers = headers)
resp.html.render()
soup = bs(resp.html.html, "lxml")
soup.find("div", {"id": "TranslationsHead"})