正在从返回的表单中检索 chrome 驱动程序元素数据
retrieving chrome driver element data from returned form
我正在尝试使用 selenium 构建一个简单的抓取工具,以从这个 USPS 工具中检索给定地址、城市、ST 的邮政编码:https://tools.usps.com/zip-code-lookup.htm?byaddress
这是我的代码,它适用于大多数步骤,但我正在努力获取我需要在最后检索的数据(邮政编码):
from selenium import webdriver
import time
import pandas as pd
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
# step 1: set path to chromedriver downloaded from here: https://chromedriver.chromium.org/downloads
PATH = "chromedriver" # modify to location on your system of above downloaded driver
driver = webdriver.Chrome(PATH)
driver.get("https://tools.usps.com/zip-code-lookup.htm?byaddress")
# step 2: specify the address to search for
street_address = "530 WILLIAM PENN PL"
city = "PITTSBURGH"
state = "PA"
# step 3: fill out the form with specified data in step 2
input_address = driver.find_element_by_id('tAddress')
input_city = driver.find_element_by_id('tCity')
drpState = Select(driver.find_element_by_id('tState'));
input_address.send_keys(street_address)
time.sleep(1)
input_city.send_keys(city)
time.sleep(1)
drpState.select_by_value(state)
# step 4: select "Find button" on USPS page to advance
button_find = driver.find_element_by_id('zip-by-address')
button_find.click()
time.sleep(2)
# step 5: retrieve zip code (the problem)
zipcode= driver.find_element(By.XPATH, '//*[@id="zipByAddressDiv"]/ul/li[1]/div[1]/p[3]/strong')
attrs=[]
for attr in zipcode.get_property('attributes'):
attrs.append([attr['name'], attr['value']])
print(attrs)
正如您在下面的屏幕截图中看到的,在最后我指定了一个 XPATH,它是通过检查邮政编码获得的。然后我尝试列出 zipcode WebDriver 对象的属性,但结果是空的,没有错误,只是对象的属性中没有任何内容 returns。
非常感谢任何帮助,提前致谢。
(参考图片)
您可以改为获取元素 zipcode-by-address
并获取其子元素 类 并找到 strong
driver = webdriver.Firefox()
... # navigation stuff here.
element = driver.find_element_by_class_name("zipcode-by-address")
all_children_by_xpath = header.find_elements_by_xpath(".//*") #
我不知道你为什么要尝试使用 get_attributes
。
要获取此页面的某些属性,需要 <strong name="..." value="...">
但它只有 <strong>
如果您想要标签名称,请使用 zipcode.tag_name
如果你想在 <strong> </strong>
中输入文本 15219-1820
,那么使用 zipcode.text
zipcode = driver.find_element(By.XPATH, '//*[@id="zipByAddressDiv"]/ul/li[1]/div[1]/p[3]/strong')
print(zipcode.tag_name)
print(zipcode.text)
我正在尝试使用 selenium 构建一个简单的抓取工具,以从这个 USPS 工具中检索给定地址、城市、ST 的邮政编码:https://tools.usps.com/zip-code-lookup.htm?byaddress
这是我的代码,它适用于大多数步骤,但我正在努力获取我需要在最后检索的数据(邮政编码):
from selenium import webdriver
import time
import pandas as pd
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
# step 1: set path to chromedriver downloaded from here: https://chromedriver.chromium.org/downloads
PATH = "chromedriver" # modify to location on your system of above downloaded driver
driver = webdriver.Chrome(PATH)
driver.get("https://tools.usps.com/zip-code-lookup.htm?byaddress")
# step 2: specify the address to search for
street_address = "530 WILLIAM PENN PL"
city = "PITTSBURGH"
state = "PA"
# step 3: fill out the form with specified data in step 2
input_address = driver.find_element_by_id('tAddress')
input_city = driver.find_element_by_id('tCity')
drpState = Select(driver.find_element_by_id('tState'));
input_address.send_keys(street_address)
time.sleep(1)
input_city.send_keys(city)
time.sleep(1)
drpState.select_by_value(state)
# step 4: select "Find button" on USPS page to advance
button_find = driver.find_element_by_id('zip-by-address')
button_find.click()
time.sleep(2)
# step 5: retrieve zip code (the problem)
zipcode= driver.find_element(By.XPATH, '//*[@id="zipByAddressDiv"]/ul/li[1]/div[1]/p[3]/strong')
attrs=[]
for attr in zipcode.get_property('attributes'):
attrs.append([attr['name'], attr['value']])
print(attrs)
正如您在下面的屏幕截图中看到的,在最后我指定了一个 XPATH,它是通过检查邮政编码获得的。然后我尝试列出 zipcode WebDriver 对象的属性,但结果是空的,没有错误,只是对象的属性中没有任何内容 returns。
非常感谢任何帮助,提前致谢。
(参考图片)
您可以改为获取元素 zipcode-by-address
并获取其子元素 类 并找到 strong
driver = webdriver.Firefox()
... # navigation stuff here.
element = driver.find_element_by_class_name("zipcode-by-address")
all_children_by_xpath = header.find_elements_by_xpath(".//*") #
我不知道你为什么要尝试使用 get_attributes
。
要获取此页面的某些属性,需要 <strong name="..." value="...">
但它只有 <strong>
如果您想要标签名称,请使用 zipcode.tag_name
如果你想在 <strong> </strong>
中输入文本 15219-1820
,那么使用 zipcode.text
zipcode = driver.find_element(By.XPATH, '//*[@id="zipByAddressDiv"]/ul/li[1]/div[1]/p[3]/strong')
print(zipcode.tag_name)
print(zipcode.text)