我試圖從谷歌地圖中獲取 sotre 的位置,但我的代碼有時會為商店獲取它,有時它不會為另一個商店獲取它。這里是谷歌 colab 的鏈接
https://colab.research.google.com/drive/1ncrffQMGyeudUkMiGSrCfssifVScfYa-?usp=sharing
最后你可以看到它是為“blaze”而不是“apple”或“ferrari”獲得的
為什么以及怎么會這樣?
注意:這與必須加載頁面無關,我讓它等到 20 秒后它仍然無法正常作業。
我希望獲得我給它的每個鏈接的位置
uj5u.com熱心網友回復:
您正在使用 Xpath 來查找您的元素,因此根據頁面的結構,它可能會發生變化。我已經使用帶有 Selenium 的 BeautifulSoup 庫對您的資料進行了一些測驗。
我認為使用 CSS Selector 查找地址更可靠。為了幫助您,請考慮以下檔案: https ://saucelabs.com/resources/articles/selenium-tips-css-selectors
嘗試這個 :
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import time
blaze = 'https://www.google.com/maps/place/Blaze Pizza/@24.5014283,54.3896917,17z/data=!3m1!4b1!4m5!3m4!1s0x3e5e676982d20b17:0xe2c5b69e67e4c85d!8m2!3d24.5014283!4d54.3896917'
apple = 'https://www.google.com/maps/place/Apple Yas Mall/@24.4881123,54.6064438,17z/data=!3m1!4b1!4m5!3m4!1s0x3e5e457d92f94e27:0x5c1646b499917d03!8m2!3d24.4881123!4d54.6086325?authuser=0&hl=en'
ansam='https://www.google.com/maps/place/Ansam Building 3/@24.4833165,54.6020795,17z/data=!4m5!3m4!1s0x3e5e45db58e6a423:0x23953eb0c87dfd3c!8m2!3d24.4834477!4d54.5999224?authuser=0&hl=en'
ferrari='https://www.google.com/maps/place/Ferrari World Abu Dhabi/@24.4836388,54.6059205,17z/data=!4m5!3m4!1s0x3e5e457e2d394a05:0x6076df4876c470a9!8m2!3d24.4837634!4d54.6070066?authuser=0&hl=en'
yas='https://www.google.com/maps/place/Yass winter carnival/@24.4886382,54.6183841,17z/data=!4m5!3m4!1s0x3e5e4f9134f9bac3:0x68162aeae1d91d21!8m2!3d24.4898629!4d54.6217851?authuser=0&hl=en'
yas1='https://www.google.com/maps/place/Yas Links Abu Dhabi/@24.4756507,54.6019735,14.83z/data=!4m5!3m4!1s0x3e5e4582ecaaecab:0xb3e0f29a13cc00d5!8m2!3d24.4783288!4d54.5999317?authuser=0&hl=en'
links = [blaze, apple, ansam, ferrari, yas, yas1]
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("--incognito")
options.add_argument('--start-maximized')
options.add_argument('--start-fullscreen')
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options = options)
def get_location(links):
address_list = []
for link in links:
driver.get(link)
page_html= driver.page_source
soup = BeautifulSoup(page_html, 'lxml')
address = soup.select_one('div.rogA2c div.fontBodyMedium').string
address_list.append(address)
time.sleep(5)
return address_list
此致,
本杰明
uj5u.com熱心網友回復:
絕對xpath總是脆弱的,而不是使用相對xpath
而不是這個
location = driver.find_element('xpath','//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[11]/div[3]/button/div[1]/div[2]/div[1]').text
嘗試這個
location = driver.find_element('xpath','(//div[@]//div[contains(@class,"fontBodyMedium")])[1]').text
uj5u.com熱心網友回復:
每個頁面都有不同的頁面結構,因此您需要使用相對 xpath 來指向元素。所以,改變這一行
location = driver.find_element('xpath','//*[@id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[11]/div[3]/button/div[1]/div[2]/div[1]').text
有了這個
location = driver.find_element('xpath','//button[@data-item-id="address"]').text
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/523628.html
上一篇:在回圈中按類名查找元素,硒
