我在python selenium中编写了一个脚本,使用反向搜索从网页上收集一些地址的邮政编码。如果我在每次点击后输入硬编码延迟,那就没问题了。虽然我使用Explicit Wait创建了我的刮刀,但我无法摆脱这个缺陷。发生这种情况只是因为只要页面加载,邮政编码就会根据我的生活地址自动更新,点击新搜索后,新记录需要1/2秒才能完成。我怎样才能从我的剧本中剔除被扼杀的延迟并仍能获得完美无瑕的结果?
以下是链接:URL
我尝试过的脚本:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
search_street =[
'43 Rue Caumartin',
'14 rue du Théatre',
'3 rue de Chevreuse',
]
def find_zip(driver, wait):
for item in range(len(search_street)):
driver.get("use_the_above_url")
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.ID, "address"))).send_keys(search_street[item],Keys.RETURN)
time.sleep(2) ##if i take out, the results get messy
item_val = wait.until(EC.presence_of_element_located((By.ID,"display_zip"))).text
print(item_val)
if __name__ == '__main__':
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
try:
find_address(driver, wait)
finally:
driver.quit()
答案 0 :(得分:2)
尝试遍历街道列表并等到每次迭代时更改邮政编码值:
for street in search_street:
driver.get("https://www.mapdevelopers.com/what-county-am-i-in.php")
wait = WebDriverWait(driver, 10)
zip_id = "display_zip"
current_item_val = wait.until(lambda driver: driver.find_element_by_id(zip_id).text)
wait.until(EC.presence_of_element_located((By.ID, "address"))).send_keys(street, Keys.RETURN)
wait.until(lambda driver: current_item_val != driver.find_element_by_id(zip_id).text)
item_val = driver.find_element_by_id(zip_id).text
print(item_val)