我試圖加載一個網頁。然後滾動到這個頁面的最底部(有一個無限滾動)並獲得一個頁面源代碼。驅動程序不會返回適當的頁面源
滾動和加載似乎正常工作,但driver.page_source
返回非常短html
這只是整個page source
的一小部分。
def scroll_to_the_bottom(driver):
old_html = ''
new_html = driver.page_source
while old_html != new_html:
print 'SCROLL'
old_html = driver.page_source
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
new_html = driver.page_source
driver.get("http://www.citypaper.com/music/short-list/bcpnews-the-short-list-weird-al-the-heartless-bastards-chastity-belt-more-20150609-story.html")
scroll_to_the_bottom(driver)
print driver.page_source
控制檯:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" data-role="base navhead resizescroll imgsize metrics oopadloader socialshare panelmod transporter"><head><script type="text/javascript" async="" src="//ml314.com/tag.aspx?2972015"></script><script type="text/javascript" async="" src="//ml314.com/tag.aspx?2972015"></script><script async="" src="http://b.scorecardresearch.com/beacon.js"></script><script async="" src="//www.google-analytics.com/analytics.js"></script><script type="text/javascript" src="http://beacon.krxd.net/optout_check?callback=Krux.ns._default.kxjsonp_optOutCheck"></script><script charset="UTF-8" type="text/javascript" src="http://cdn.taboola.com/libtrc/impl.174-RELEASE.js"></script><script async="" src="//widget.perfectmarket.com/tribunedigital-network/load.js"></script><script async="" src="http://b.scorecardresearch.com/beacon.js"></script>
<title>Music Boxes - Baltimore City Paper</title>
<link rel="dns-prefetch" href="//www.trbimg.com" /><link rel="dns-prefetch" href="//static.chartbeat.com" /><link rel="dns-prefetch" href="//loggingservices.tribune.com" /><link rel="dns-prefetch" href="//m.trb.com" /><link rel="dns-prefetch" href="//b.scorecardresearch.com" /><link rel="dns-prefetch" href="//www.google-analytics.com" /><link rel="dns-prefetch" href="http://pubads.g.doubleclick.net" /><link rel="dns-prefetch" href="https://securepubads.g.doubleclick.net" /><link rel="dns-prefetch" href="//secure-us.imrworldwide.com" /><link rel="dns-prefetch" href="//www.googletagservices.com" /><link rel="dns-prefetch" href="http://ssor.tribdss.com" /><link rel="dns-prefetch" href="//cdn.krxd.net" /><link rel="dns-prefetch" href="//cdn.gigya.com" /><link rel="dns-prefetch" href="//cdn.taboola.com" /><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
<meta charset="utf-8" />
<meta name="x-servername" content="i10latisrapp02" />
<meta name="robots" content="noodp, noydir" />
我用chromedriver
這樣我就可以清楚地看到,它滾動至底部。請問哪裏可以解決問題?
編輯:
我已經添加了一個網頁抓取。
謝謝您的回答。現在我看到它:)我有兩個問題:1.重點是,我試圖找到網頁上的增加。但是有多個網頁,所以我想使它具有普遍性。你有什麼想法如何使它通用,腳本可以指望這樣的情況? 2.你的意思是像driver.find_element_by_css ...等命令嗎?它更快?謝謝 –
1.你需要對你的問題更具體。可能在做一些研究之後開始一個新的問題,併發布你正在嘗試做什麼,你嘗試過什麼,等等。2)是的......是的,速度要快得多。你真的不想嘗試解析HTML並找到你正在尋找的東西。你可能應該花一些時間學習Selenium,以及它可以做什麼。閱讀他們的網站/ wiki,做一些教程等。 – JeffC