您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Selenium如何實(shí)現(xiàn)模擬瀏覽器動(dòng)態(tài)加載頁面”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Selenium如何實(shí)現(xiàn)模擬瀏覽器動(dòng)態(tài)加載頁面”這篇文章吧。
相信爬取大公司的數(shù)據(jù)時(shí),常常會(huì)遇到頁面信息動(dòng)態(tài)加載的問題,
如果僅僅使用content = urllib2.urlopen(URL).read(),估計(jì)信息是獲取不全的,這時(shí)候就需要模擬瀏覽器加載頁面的過程,
selenium提供了方便的方法,我也是菜鳥,試了很多種方式,下面提供覺得最靠譜的(已經(jīng)證明對于爬取新浪微博的topic、twitter under topic完全沒問題)。
至于下面的browser變量是什么,看前面的幾篇文章。
首先是請求對應(yīng)的URL:
right_URL = URL.split("from")[0] + "current_page="+str(current_page) + "&since_id="+str(since_id) + "&page="+str(page_index) + "#Pl_Third_App__"+str(Pl_Third_App) print right_URL try: browser.get(right_URL) print "loading more, sleep 3 seconds ... 0" time.sleep(3) # NO need for this sleep, but we add ... browser = selenuim_loading_more(browser, method_index=0) except: print "one exception happen ==> get_tweeter_under_topic 2 ..." pass
然后模擬瀏覽器,加載更多(推薦使用method_index=0,已經(jīng)證明比其他好用很多):
def selenuim_loading_more(browser, method_index=0): if method_index==0: browser.implicitly_wait(3) # 為了快速滑動(dòng),先設(shè)置超時(shí)時(shí)間為1秒 # while True: for i in range(1, 4): # at most 3 times print "loading more, window.scrollTo bettom for the", i,"time ..." browser.execute_script("window.scrollTo(0,document.body.scrollHeight);") try: # 定位頁面底部的換頁tab browser.find_element_by_css_selector("div[class='W_pages']") break # 如果沒拋出異常就說明找到了底部標(biāo)志,跳出循環(huán) except NoSuchElementException: pass # 拋出異常說明沒找到底部標(biāo)志,繼續(xù)向下滑動(dòng) browser.implicitly_wait(4) # 將超時(shí)時(shí)間改回10秒 elif method_index==1: browser.find_element_by_css_selector("div[class='empty_con clearfix']").click() # loading more print "loading more, sleep 4 seconds ... 1" time.sleep(4) browser.find_element_by_css_selector("div[class='empty_con clearfix']").click() # loading more print "loading more, sleep 3 seconds ... 2" time.sleep(2) elif method_index==2: load_more_1 = browser.find_element_by_css_selector("div[class='empty_con clearfix']") # loading more ActionChains(browser).click(load_more_1).perform() print "loading more, sleep 4 seconds ... 1" time.sleep(4) load_more_2 = browser.find_element_by_css_selector("div[class='empty_con clearfix']") # loading more ActionChains(browser).click(load_more_2).perform() print "loading more, sleep 3 seconds ... 2" time.sleep(2) elif method_index==3: print "loading more, sleep 4 seconds ... 1" element = WebDriverWait(browser, 4).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class='empty_con clearfix']")) ) element.click() print "loading more, sleep 2 seconds ... 2" WebDriverWait(browser, 2).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class='empty_con clearfix']")) ).click() return browser
以上是“Selenium如何實(shí)現(xiàn)模擬瀏覽器動(dòng)態(tài)加載頁面”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。