python网页截图(不受滚动条限制)

最近工作需要获取网页的截图,本来是使用selenium + phantomjs进行截图,但是却发现一些没有被封的网页保存的截图却是404页面找不到的画面,猜测有可能是访问网页的响应时间超过默认值,由于对上述两个工具不太熟悉,没有找到解决方法。如果某位大神路过,还请指教。多谢!!!
故查阅资料转战调用Google访问然后截图,网页找不到的问题解决了又出现了滚动条的限制无法截取更多内容的问题。经过多方查找资料和实验,最终总结出了,如何调节滚动窗口大小来截取图片。代码如下(如果有更好的方法,希望能够与我沟通,再次感谢):

#调用谷歌浏览器截图
#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请求失败!!!正在准备重发。。。')
                time.sleep(2)
                continue
            else:
                break
        chrome_flag = True
driver.quit()

你可能感兴趣的:(python网页截图(不受滚动条限制))