python+selenium+PhantomJS抓取网页动态加载内容
环境搭建
准备工具:pyton3.5,selenium,phantomjs
我的电脑里面已经装好了python3.5
安装Selenium
pip3installselenium
安装Phantomjs
按照系统环境下载phantomjs,下载完成之后,将phantomjs.exe解压到python的script文件夹下
使用selenium+phantomjs实现简单爬虫
fromseleniumimportwebdriver driver=webdriver.PhantomJS() driver.get('http://www.baidu.com')#加载网页 data=driver.page_source#获取网页文本 driver.save_screenshot('1.png')#截图保存 print(data) driver.quit()
selenium+phantomjs的一些使用方法
设置请求头里的user-Agent
fromseleniumimportwebdriver fromselenium.webdriver.common.desired_capabilitiesimportDesiredCapabilities dcap=dict(DesiredCapabilities.PHANTOMJS)#设置useragent dcap['phantomjs.page.settings.userAgent']=('Mozilla/5.0(Macintosh;IntelMacOSX10.9;rv:25.0)Gecko/20100101Firefox/25.0')#根据需要设置具体的浏览器信息 driver=webdriver.PhantomJS(desired_capabilities=dcap)#封装浏览器信息 driver.get('http://www.baidu.com')#加载网页 data=driver.page_source#获取网页文本 driver.save_screenshot('1.png')#截图保存 print(data) driver.quit()
请求超时设置
webdriver类中有三个和时间相关的方法:
1.pageLoadTimeout设置页面完全加载的超时时间,完全加载即完全渲染完成,同步和异步脚本都执行完
2.setScriptTimeout设置异步脚本的超时时间
3.implicitlyWait识别对象的智能等待时间
fromseleniumimportwebdriver driver=webdriver.PhantomJS() driver.set_page_load_timeout(5)#设置超时时间 driver.get('http://www.baidu.com') print(driver.title) driver.quit()
设置浏览器窗口大小
调用启动的浏览器不是全屏的,有时候会影响我们的某些操作,所以我们可以设置全屏
driver.maximize_window()#设置全屏
driver.set_window_size('480','800')#设置浏览器宽480,高800
元素定位
fromseleniumimportwebdriver driver=webdriver.PhantomJS() driver.set_page_load_timeout(5) driver.get('http://www.baidu.com') try: driver.get('http://www.baidu.com') driver.find_element_by_id('kw')#通过ID定位 driver.find_element_by_class_name('s_ipt')#通过class属性定位 driver.find_element_by_name('wd')#通过标签name属性定位 driver.find_element_by_tag_name('input')#通过标签属性定位 driver.find_element_by_css_selector('#kw')#通过css方式定位 driver.find_element_by_xpath("//input[@id='kw']")#通过xpath方式定位 driver.find_element_by_link_text("贴吧")#通过xpath方式定位 print(driver.find_element_by_id('kw').tag_name)#获取标签的类型 exceptExceptionase: print(e) driver.quit()
操作浏览器前进或后退
fromseleniumimportwebdriver driver=webdriver.PhantomJS() try: driver.get('http://www.baidu.com')#访问百度首页 driver.save_screenshot('1.png') driver.get('http://www.sina.com.cn')#访问新浪首页 driver.save_screenshot('2.png') driver.back()#回退到百度首页 driver.save_screenshot('3.png') driver.forward()#前进到新浪首页 driver.save_screenshot('4.png') exceptExceptionase: print(e) driver.quit()
到此这篇关于python+selenium+PhantomJS抓取网页动态加载内容的文章就介绍到这了,更多相关pythonPhantomJS抓取内容内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!