如何提取< span>使用美丽汤来标记内容? [英] How to extract the <span> tag contents using the Beautiful Soup?
本文介绍了如何提取< span>使用美丽汤来标记内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从Google翻译网站中提取span标签的内容.内容是翻译后的结果,其中包含id ="result_box". 尝试打印内容时,它返回None值.
I'm trying to extract the span tag content from the google translate website. The content is the translated result which has the id="result_box". When tried to print the contents, it returns None value.
请在此处
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
推荐答案
请求未执行JavaScript,您可以使用 selenium
和 PhantomJS
进行这样的无头浏览:
Requests doesn't execute JavaScript, you could use selenium
and PhantomJS
for the headless browsing like this:
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 result:
>>> result
'Bonjour'
这篇关于如何提取< span>使用美丽汤来标记内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文