【问题标题】:Can anyone show me how I can just extract and display the text in the image on Python谁能告诉我如何在 Python 上提取和显示图像中的文本
【发布时间】:2019-12-03 09:41:24
【问题描述】:

谁能告诉我如何只提取红色方块中的文本? 我一直在摆弄python并试图提取它但没有成功。 我正在编写一个脚本,要求您输入一个地址,然后启动 Firefox(或 Chrome)进入谷歌网站并搜索已保存在 python 脚本中的地址的旅行时间和距离。我只需要红色方块中的文本在命令屏幕中显示为纯文本即可。

任何帮助将不胜感激,到目前为止我尝试过的内容如下,我只是不知道如何访问该元素。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time




print("Please type in address")
address = input()

driver = webdriver.Firefox()
url = r"https://www.google.com"
driver.get(url)

ara = driver.find_element_by_name("q")

ara.send_keys("Navigate to "+ address + " "+"from terror st keilor park" + Keys.RETURN)

x = driver.get_element_by_xpath("//div[@id='exp0']/div[1]/div/div[@class='BbbuR uc9Qxb uE1RRc']")

print(x.text)

google maps

【问题讨论】:

  • 这不是免费的代码服务。请展示您的尝试、发生的情况与预期,我们可以提供帮助。
  • 我是该网站的新手,如所述尝试学习 python。我很抱歉没有提供太多信息或代码。我已经编辑了这篇文章,现在包含了我正在尝试的完整代码,

标签: html python-3.x selenium text display


【解决方案1】:

使用WebDriverWait 并等待元素到element_to_be_clickable 并使用以下xpath

element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//div[@data-rre-id="exp0"]')))
print(element.text)

要执行上述代码,您需要导入以下代码。

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

【讨论】:

    【解决方案2】:

    研究使用 google maps API 在 python 中直接从 google 提取数据,而无需打开浏览器,获取图像,然后处理该图像以读取文本,然后...

    或者,虽然不推荐但可能可行,通过 python 代码发送请求以检索网页并解析响应。

    谷歌“python 请求示例”以了解如何在 python 代码中发出网络请求。

    然后谷歌“python parse html”并学习用代码解析网页以提取您要查找的信息。

    有很多方法可以获取您正在寻找的信息,但最简单的方法肯定不是在图像上使用光学字符识别。但是,如果你死心塌地,谷歌“python 光学字符识别”。

    我希望这会有所帮助:)

    【讨论】:

    • 我会考虑您的建议。我确实查看了谷歌地图 api,但因为我需要显示旅行时间和距离,所以我不得不使用 Directions api,经过一些搜索后需要你开始付费。我编写了一个脚本,您可以在其中输入地址并同时搜索来自 4 个不同位置的旅行时间,因此它会对 Google 给您的“奖励积分”造成影响(除非我遗漏了什么)整个想法是让旅行我将添加到脚本中以便比较的 4 个不同位置的时间和距离。
    猜你喜欢
    • 2016-03-09
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 2018-01-21
    • 2012-12-23
    相关资源
    最近更新 更多