为什么不在debian上用python编写time.sleep(x)代码呢?
我想从一个API网站上抓取很多页面,所以我必须等待加载元素,然后我的脚本才能点击它们。但是时间睡眠(5)不起作用,因为它不稳定地跳转到下一页 我怎么了 我的代码:为什么不在debian上用python编写time.sleep(x)代码呢?,python,selenium,time,web-scraping,sleep,Python,Selenium,Time,Web Scraping,Sleep,我想从一个API网站上抓取很多页面,所以我必须等待加载元素,然后我的脚本才能点击它们。但是时间睡眠(5)不起作用,因为它不稳定地跳转到下一页 我怎么了 我的代码: def scrape(urls): browser = webdriver.Firefox() datatable=[] for url in urls: browser.get(url) time.sleep(5) driver.find_element_by_
def scrape(urls):
browser = webdriver.Firefox()
datatable=[]
for url in urls:
browser.get(url)
time.sleep(5)
driver.find_element_by_xpath('//button[contains(text(), "Load earlier flights")]').click()
time.sleep(5)
html = browser.page_source
soup=BeautifulSoup(html,"html.parser")
table = soup.find('table', { "class" : "table table-condensed table-hover data-table m-n-t-15" })
soup2=BeautifulSoup(html,"html.parser")
name = soup2.h2.string
soup3=BeautifulSoup(html,"html.parser")
name2 = soup3.h1.string
soup4=BeautifulSoup(html,"html.parser")
name3 = soup4.h3.string
name4 = datetime.now()
for record in table.find_all('tr', class_="hidden-xs hidden-sm ng-scope"):
temp_data = []
temp_data.append(name4)
temp_data.append(name)
temp_data.append(name2)
temp_data.append(name3)
for data in record.find_all("td"):
temp_data.append(data.text.encode('latin-1'))
newlist = filter(None, temp_data)
datatable.append(newlist)
time.sleep(10)
browser.close()
return datatable
第一个URL:flightradar24.com/data/airports/ams/arrivals不要使用
时间。sleep
,使用提供的显式和隐式等待。我将使用selenium附带的“预期条件”
比如说
from selenium.webdriver.support import expected_conditions as EC
Def someFunc(self):
self.myDriver = webdriver.Chrome(executable_path="webDrivers\chromedriver.exe")
self.waitElement = WebDriverWait(self.myDriver,10)
self.waitElement.until(EC.presence_of_all_elements_located((By.CLASS_NAME,'page-link')))
#do something with page
这将等待10秒,等待selenium找到的所有类名“page link”。
您可以了解更多信息为什么要创建4个具有相同内容的汤(提示,您不需要!),依赖
time.sleep()
等待页面完全加载充其量是脆弱的。您可能想检查selenium中是否有更好的解决方案……我知道有更好的解决方案,如等待元素等,但我对为什么time.sleep()在我的代码中不起作用感兴趣。在我的代码中,我必须使用它?“driver=webdriver.Firefox()driver.implicitly_wait(10)driver.get(“url”)myDynamicElement=driver.find_element_by_name(“Load Early flight”)”?@Mr.D如果您想要隐式等待,可以。但是您可以优化代码并使用显式等待。