Python 如何在商店送货的地方得到邮政编码的值?我想在可以送货的地方得到邮政编码的值
获取邮政地址的代码。 在上面的代码中,我使用的是selenium,它是镀铬无头的。Python 如何在商店送货的地方得到邮政编码的值?我想在可以送货的地方得到邮政编码的值,python,selenium,xpath,web-scraping,webdriverwait,Python,Selenium,Xpath,Web Scraping,Webdriverwait,获取邮政地址的代码。 在上面的代码中,我使用的是selenium,它是镀铬无头的。 仅需要代码才能获取可送货商店的邮政编码。完全加载页面需要时间,因此您无法获取所需的值 获取所有邮政编码 导入WebDriverWait()并等待所定位的所有元素的可见性() 要从元素中获取最后一个子元素,可以诱导javascript执行器或拆分行获取唯一的邮政编码 # importing package from selenium import webdriver # setting the path PA
仅需要代码才能获取可送货商店的邮政编码。完全加载页面需要时间,因此您无法获取所需的值 获取所有邮政编码 导入
WebDriverWait
()并等待所定位的所有元素的可见性()
要从元素中获取最后一个子元素,可以诱导javascript执行器或拆分行获取唯一的邮政编码
# importing package
from selenium import webdriver
# setting the path
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
options = webdriver.ChromeOptions
options.headless = True
driver.get("https://www.craispesaonline.it/provincia/treviso")
# x path for Address and Postal Code
x = ('//address//p[@class="text-lowercase m-0 ng-binding"]')
search = driver.find_elements_by_xpath(x)
# retrieving the output in a text file
with open("Italy_Scrape.txt", "a") as f:
for i in search:
print("PostalCode :" + i.text, file=f)
print("----------------------------------------------------------------------------", file=f)
driver.quit()
您需要导入以下库
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))
控制台输出:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
使用拆分行进行相同的输出
该页面完全加载需要时间,因此您无法获取所需的值
获取所有邮政编码
导入WebDriverWait
()并等待所定位的所有元素的可见性()
要从元素中获取最后一个子元素,可以诱导javascript执行器或拆分行获取唯一的邮政编码
# importing package
from selenium import webdriver
# setting the path
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
options = webdriver.ChromeOptions
options.headless = True
driver.get("https://www.craispesaonline.it/provincia/treviso")
# x path for Address and Postal Code
x = ('//address//p[@class="text-lowercase m-0 ng-binding"]')
search = driver.find_elements_by_xpath(x)
# retrieving the output in a text file
with open("Italy_Scrape.txt", "a") as f:
for i in search:
print("PostalCode :" + i.text, file=f)
print("----------------------------------------------------------------------------", file=f)
driver.quit()
您需要导入以下库
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))
控制台输出:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
使用拆分行进行相同的输出
要仅提取可送货的门店的邮政编码,您需要导出所有元素的可见性()
,并且您可以使用以下方法:
- 使用
CSS\u选择器
:
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
- 控制台输出:
driver.get("https://www.craispesaonline.it/provincia/treviso")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='cl-accept']"))).click()
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//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, "//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))
- 注意:您必须添加以下导入:
['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']
要仅提取可送货的门店的邮政编码,您需要引导所有元素的可见性()
,并且您可以基于以下内容使用:
- 使用
:CSS\u选择器
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
- 控制台输出:
driver.get("https://www.craispesaonline.it/provincia/treviso") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='cl-accept']"))).click() driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//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, "//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))
- 注意:您必须添加以下导入:
['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']
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
硒代码:
//div[@class="row province-cms-content-store-row ng-scope"][./div[@ng-if="store.shippingEnabled == true"]]//meta[@itemprop="postalCode"]/@content
输出:29个邮政编码
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")
要完成前面的回答,您可以通过一个XPath表达式获取可以交付的商店的邮政编码:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
硒代码:
//div[@class="row province-cms-content-store-row ng-scope"][./div[@ng-if="store.shippingEnabled == true"]]//meta[@itemprop="postalCode"]/@content
输出:29个邮政编码
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")