第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Xpath 沒有使用 Splinter/Selenium Python 3 選擇正確的元素

Xpath 沒有使用 Splinter/Selenium Python 3 選擇正確的元素

桃花長(zhǎng)相依 2021-09-01 14:46:17
不確定我是否在這里犯了一個(gè)愚蠢的錯(cuò)誤,我已經(jīng)搜索了所有內(nèi)容,但我無(wú)法弄清楚這一點(diǎn)。我真的很感激你的幫助。我正在嘗試制作一個(gè)抓取工具來(lái)抓取 Google Map Pack 數(shù)據(jù)。我正在使用 Splinter 來(lái)做到這一點(diǎn)。我已經(jīng)設(shè)法選擇了每個(gè)地圖包項(xiàng)目的 div,但我想然后遍歷并選擇每個(gè) div 的標(biāo)題(和其他元素)。但是,當(dāng)我嘗試這樣做時(shí),它總是選擇第一個(gè)元素的標(biāo)題,即使我在單個(gè)元素上運(yùn)行 find_by_xpath 也是如此。這是我的代碼:from splinter import Browserfrom selenium import webdriverimport timechrome_options = webdriver.ChromeOptions()browser = Browser('chrome', options=chrome_options)browser.visit("https://google.com")browser.fill('q', 'roofing laredo tx')# Find and click the 'search' buttontime.sleep(5)button = browser.find_by_name('btnK')# Interact with elementsbutton.click()time.sleep(5)maps_elements = browser.find_by_xpath("//div[contains(@class,'VkpGBb')]")for map_element in maps_elements:    # print(map_element.text)    title = map_element.find_by_xpath("//div[contains(@class,'dbg0pd')]/span").text    print(title)所以我想要的是:JJ Flores Roofing & Construction HBC Roofing McAllen Valley Roofing Co但我得到了JJ弗洛雷斯屋面和建筑 JJ弗洛雷斯屋面和建筑 JJ弗洛雷斯屋面和建筑
查看完整描述

3 回答

?
蠱毒傳說(shuō)

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊

編輯:


你得到了重復(fù)的結(jié)果,因?yàn)閺难h(huán)它選擇根元素//它應(yīng)該是相對(duì)的或選擇子元素,./但它仍然不起作用,并且可能是分裂錯(cuò)誤。但嘗試使用 CSS 選擇器


for map_element in maps_elements: 

    # select relative but failed

    #title = map_element.find_by_xpath("./div[contains(@class,'dbg0pd')]/span")

    title = map_element.find_by_css("div[class*='dbg0pd'] > span").text

    print(title)

變量中的錯(cuò)字,s從


title = maps_elements.....

#title = map_element.....


查看完整回答
反對(duì) 回復(fù) 2021-09-01
?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

這是正確的,因?yàn)槟荒茉?for 循環(huán)中聲明一個(gè)變量,然后在其中創(chuàng)建該變量。您需要在初始化循環(huán)之前創(chuàng)建變量才能使其工作。


title_elements = browser.find_by_xpath("//div[contains(@class,'dbg0pd')]/span")


for title_element in title_elements:

    title = title_element.text

    print(title)


查看完整回答
反對(duì) 回復(fù) 2021-09-01
?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊

更改您的代碼:


maps_elements = browser.find_by_xpath("//div[contains(@class,'VkpGBb')]")


for map_element in maps_elements:

    # print(map_element.text)

    title = maps_elements.find_by_xpath("//div[contains(@class,'dbg0pd')]/span").text

    print(title)


title_elements = browser.find_by_xpath("//div[contains(@class,'dbg0pd')]/span")


for title_element in title_elements:

    title = title_element.text

    print(title)


查看完整回答
反對(duì) 回復(fù) 2021-09-01
  • 3 回答
  • 0 關(guān)注
  • 251 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)