gpt4 book ai didi

python - 无法从我的脚本中消除硬编码延迟以获得准确的结果

转载 作者:行者123 更新时间:2023-12-01 02:23:04 24 4
gpt4 key购买 nike

我用 python selenium 编写了一个脚本,使用反向搜索从网页中收集一些地址的邮政编码。如果我在每次点击后添加硬编码的延迟,效果就很好。尽管我使用显式等待创建了我的抓取工具,但我无法摆脱这个缺陷。发生这种情况的原因只是因为页面加载后,邮政编码会根据我居住的地址自动更新,并且在单击新搜索后,需要 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()

最佳答案

尝试迭代街道列表并等待每次迭代的邮政编码值发生变化:

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)

关于python - 无法从我的脚本中消除硬编码延迟以获得准确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47753932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com