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

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

如何獲取商店送貨的郵政編碼值?我想獲取可以送貨的郵政編碼值

如何獲取商店送貨的郵政編碼值?我想獲取可以送貨的郵政編碼值

絕地?zé)o雙 2023-04-18 16:07:24
# importing packagefrom selenium import webdriver# setting the pathPATH = "C:\Program Files (x86)\chromedriver.exe"driver = webdriver.Chrome(PATH)options = webdriver.ChromeOptionsoptions.headless = Truedriver.get("https://www.craispesaonline.it/provincia/treviso")# x path for Address and Postal Codex = ('//address//p[@class="text-lowercase m-0 ng-binding"]')search = driver.find_elements_by_xpath(x)# retrieving the output in a text filewith open("Italy_Scrape.txt", "a") as f:    for i in search:        print("PostalCode :" + i.text, file=f)        print("----------------------------------------------------------------------------", file=f)driver.quit()獲取郵政地址的代碼。在上面的代碼中,我使用的是無頭 chrome 的 selenium。需要代碼以僅獲取可以送貨的商店的郵政編碼。
查看完整描述

3 回答

?
POPMUISE

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

該頁面需要時(shí)間才能完全加載,因此您無法獲得您想要的值。


獲取所有郵政編碼 Induce WebDriverWait() 和 wait for visibility_of_all_elements_located()


要從元素中獲取最后一個(gè)子元素,您可以誘導(dǎo) javascript 執(zhí)行程序或分割線來獲取唯一的郵政編碼。


driver.get("https://www.craispesaonline.it/provincia/treviso")

search=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,'//address//p[@class="text-lowercase m-0 ng-binding"]')))

for postcode in search:

    print(driver.execute_script('return arguments[0].lastChild.textContent;', postcode))

您需要導(dǎo)入以下庫。


from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By

控制臺(tái)輸出:


0422/710092

 0422 452388

 0422/958833

 0423/689003

 0422/853881

 0422/969047

 0423/564126

 0423/650073

 0423/723434

 0423/942150

 0438/500484

 0423/868496

 0438/898282

 0483801679

 0422/832603

 0423/470063

 0423/755164-23

 0438/492409

 0438/893369

 0422/791529

 0423/302959

 0423/301381

 0423-603754

 0423/609936

 0423/609151

 0423480340

 0438/781107

 0423/670593

 0423/81743

 0423/81534

 0423/972091

 0423/451941

 0422/912384

 0423/620803

 0423/621383

使用splitlines()的相同輸出


driver.get("https://www.craispesaonline.it/provincia/treviso")

search=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,'//address//p[@class="text-lowercase m-0 ng-binding"]')))

for postcode in search:

    print(postcode.text.splitlines()[-1].split("|")[-1].strip()) #last element which is postcode



查看完整回答
反對(duì) 回復(fù) 2023-04-18
?
開滿天機(jī)

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

要完成前面的答案,您可以使用一個(gè) XPath 表達(dá)式獲取可以送貨的商店的郵政編碼:


//div[@class="row province-cms-content-store-row ng-scope"][./div[@ng-if="store.shippingEnabled == true"]]//meta[@itemprop="postalCode"]/@content

硒代碼:


driver.get("https://www.craispesaonline.it/provincia/treviso")

postcodes = WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,'//div[@class="row province-cms-content-store-row ng-scope"][./div[@ng-if="store.shippingEnabled == true"]]//meta[@itemprop="postalCode"]'))).get_attribute("content")

輸出:29個(gè)郵政編碼


['31038']

['31038']

['31047']

['31050']

['31030']

...


查看完整回答
反對(duì) 回復(fù) 2023-04-18
?
郎朗坤

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

要僅提取可以送貨的商店的郵政編碼,您可以誘導(dǎo)WebDriverWait并且visibility_of_all_elements_located()您可以使用以下內(nèi)容xpath基于定位策略:

  • 使用CSS_SELECTOR

driver.get("https://www.craispesaonline.it/provincia/treviso")

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "http://a[@class='cl-accept']"))).click()

driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "http://h2[contains(., 'Potrai scegliere di ricevere la tua spesa in due modi:')]"))))

addresses = [my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "http://input[@value='Consegna']//preceding::address[1]//p[@class='text-lowercase m-0 ng-binding']")))]

for address in addresses:

? ? print(re.findall(r"\b\d{5}\b", address))

控制臺(tái)輸出:


['31038']

['31038']

['31047']

['31050']

['31030']

['31031']

['31034']

['31014']

['31035']

['31010']

['31010']

['31036']

['31037']

['31037']

['31050']

['31050']

['31044']

['31044']

['31044']

['31044']

['31044']

['31023']

['31058']

['31040', '81743']

['31049']

['31050']

['31020']

['31040']

['31040']

注意:您必須添加以下導(dǎo)入:


from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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