selenium 获取我当前的位置“位置"

编程入门 行业动态 更新时间:2024-10-26 02:25:05
本文介绍了selenium 获取我当前的位置“位置"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 selenium 抓取以下页面 mycurrentlocation/ 以检索我当前的位置(地区名称和地名),当我执行此代码时,有这个 google 页面(mycurrentlocation/ ) 在显示结果之前显示.

I am scraping for the following page mycurrentlocation/ by using selenium in order to retrieve my current position (region name & place name), when I execute this code there is this google page (mycurrentlocation/ ) which is displayed before displaying the result.

你有没有提供给我的解决方案,以便不显示我正在抓取的页面?

Do you have a solution to offer me so as not to display the page on which I am scraping?

代码:

from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait import time from geopy.geocoders import Nominatim import time from pprint import pprint # instantiate a new Nominatim client app = Nominatim(user_agent="tutorial") def getLocation(): #autoriser le naviagateur pour acceder à l'emplacement actuelle par defaut, # Si on essaye d'accéder à un site Web : « mycurrentlocation » via chrome, # il demande d'autoriser l'accès à la localisation. La commande « - use-fake-ui-for-media-stream » # accordera toutes les autorisations pour l'emplacement, le microphone, etc. automatiquement. options = Options() options.add_argument("--use-fake-ui-for-media-stream") # appelez la page Web mycurrentlocation/ et attendez 20 secondes que la page se charge. timeout = 20 #Pour chromedriver il faut avoir la meme version que google chrome driver = webdriver.Chrome(executable_path = './chromedriver.exe', chrome_options=options) #driver = webdriver.Chrome(executable_path = './chromedriver.exe', chrome_options=options) -> on peut mettre ça a la palce driver.get("mycurrentlocation/") wait = WebDriverWait(driver, timeout) time.sleep(3) #Trouvez le XPath des éléments de latitude et de longitude mentionnés sur la page Web puis copier le nom de la classe qu'on souhaite récupérer neighborhood = driver.find_elements_by_xpath('//*[@id="neighborhood"]') neighborhood = [x.text for x in neighborhood] neighborhood = str(neighborhood[0]) regionname = driver.find_elements_by_xpath('//*[@id="regionname"]') regionname = [x.text for x in regionname] regionname = str(regionname[0]) placename = driver.find_elements_by_xpath('//*[@id="placename"]') placename = [x.text for x in placename] placename = str(placename[0]) driver.quit() return (neighborhood,regionname,placename) neighborhood,regionname,placename=getLocation() print("le résultat est : \n ", neighborhood,regionname,placename)

推荐答案

我认为您不希望看到您正在寻找的浏览器(如果是这种情况)

I think u don't want the browser to be seen (if that's the case) you are looking for

使用 selenium 的无头模式.

headless mode with selenium .

将此参数添加到选项:

options.add_argument("--headless")

更多推荐

selenium 获取我当前的位置“位置"

本文发布于:2023-06-07 21:54:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   selenium   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!