How to extract the content of a <span> tag using Beautiful Soup?

I am trying to extract the content of a span tag from google translate website. The content is the result of the translation, which has id = "result_box". It returns None when trying to print the content.

Please check the picture here

import requests
from bs4 import BeautifulSoup

r = requests.get("https://translate.google.co.in/?rlz=1C1CHZL_enIN729IN729&um=1&ie=UTF-8&hl=en&client=tw-ob#en/fr/good%20morning")

soup = BeautifulSoup(r.content, "lxml")
spanner = soup.find(id = "result_box")

result = spanner.text

      

+2


source to share


1 answer


No JavaScript is executed in the requests, you can use it for mute browsing too, for example: selenium

PhantomJS

from bs4 import BeautifulSoup
from selenium import webdriver

url = "https://translate.google.co.in/?rlz=1C1CHZL_enIN729IN729&um=1&ie=UTF-8&hl=en&client=tw-ob#en/fr/good%20morning"
browser = webdriver.PhantomJS()
browser.get(url)
html = browser.page_source

soup = BeautifulSoup(html, 'lxml')
spanner = soup.find(id = "result_box")
result = spanner.text

      



This gives our expected output:

>>> result
'Bonjour'

      

+2


source







All Articles