最近工作需要獲取網(wǎng)頁的截圖李滴,本來是使用selenium + phantomjs進(jìn)行截圖,但是卻發(fā)現(xiàn)一些沒有被封的網(wǎng)頁保存的截圖卻是404頁面找不到的畫面蛮瞄,猜測(cè)有可能是訪問網(wǎng)頁的響應(yīng)時(shí)間超過默認(rèn)值所坯,由于對(duì)上述兩個(gè)工具不太熟悉,沒有找到解決方法挂捅。如果某位大神路過芹助,還請(qǐng)指教。多謝!W赐痢无蜂!
故查閱資料轉(zhuǎn)戰(zhàn)調(diào)用Google訪問然后截圖,網(wǎng)頁找不到的問題解決了又出現(xiàn)了滾動(dòng)條的限制無法截取更多內(nèi)容的問題蒙谓。經(jīng)過多方查找資料和實(shí)驗(yàn)斥季,最終總結(jié)出了,如何調(diào)節(jié)滾動(dòng)窗口大小來截取圖片累驮。代碼如下(如果有更好的方法酣倾,希望能夠與我溝通,再次感謝):
#調(diào)用谷歌瀏覽器截圖
#now = time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time()))
image_path = data_path +'/image'
if not os.path.exists(image_path ):
os.makedirs(image_path )
#os.makedirs(datapath +'/image')
chrome_options = webdriver.ChromeOptions()
#ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'
#chrome_options.add_argument('user-agent="%s"' % ua)
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('window-size=2560x1440')
#chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path=r"D:\chromedriver.exe", chrome_options=chrome_options)
for i in range (0,len(user_table)):
j = 0
url2 = user_table.ix[i,1]
user_account = user_table.ix[i,0]
print (user_account)
chrome_flag = False
while not chrome_flag:
try:
driver.get(url2)
js="var q=document.documentElement.scrollTop=(350)"
driver.execute_script(js)
picName = image_path +'/'+ user_account + ".png"
driver.save_screenshot(picName)
#driver.close()
except:
j +=1
if j <= 5:
print ('[%s] HTTP請(qǐng)求失敯āT晡!置侍!正在準(zhǔn)備重發(fā)映之。。蜡坊。')
time.sleep(2)
continue
else:
break
chrome_flag = True
driver.quit()