首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python爬虫进阶(一)使用Selenium进行网页抓取

Python爬虫进阶(一)使用Selenium进行网页抓取

作者头像
小歪
发布2018-04-04 14:25:14
2.1K0
发布2018-04-04 14:25:14
举报
萌新要学习Selenium了,安装是个坑。还要下载相关配件,可以参考python 安装selenium环境(https://my.oschina.net/hyp3/blog/204347)

1、使用Firefox实例

from selenium import webdriver
import time
firefox = webdriver.Firefox()  #初始化Firefox浏览器
url = 'https://www.zhihu.com'
firefox.get(url)  #调用get方法抓取
time.sleep(10)  #10s用于观察
with open('zhihu.html','w',encoding='utf-8') as f:
    f.write(firefox.page_source)  #保存网页到本地
firefox.quit()

上图为调用Firefox获得的网页。使用page_source可以获得网页源代码,就和requests.get是一样的,不用加headers之类的。

2、对Selenium的profile的配置

简单说,就是使用selenium修改浏览器相关参数,让浏览器不加载JS、不加载图片,会提高很多速度。代码如下:

from selenium import webdriver
import time
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("permissions.default.stylesheet",2)  #禁用样式表文件
firefox_profile.set_preference("permissions.default.image",2)  #不加载图片
firefox_profile.set_preference("javascript.enabled",False)  #禁止JS
firefox_profile.update_preferences()  #更新设置
firefox = webdriver.Firefox(firefox_profile)

url = 'https://www.zhihu.com'
print("开始加载")
t_start = time.time()
firefox.get(url)
t_end = time.time()
print("加载时间是:",t_end-t_start)
time.sleep(10)
firefox.quit()

加载网页如下

firefox_profile.set_preference("permissions.default.stylesheet",1)
firefox_profile.set_preference("permissions.default.image",1)
firefox_profile.set_preference("javascript.enabled",True)

把2改为1,False改为True,看看

返回正常网页

4s与10s的差别,在爬取多网页就会有体现了。

注意,页面加载与实际网络环境有关。

3、画图

禁用JS,页面加载是否更快,可以在每种方式下运行相同的次数,然后取平均值来对比。

绘图使用matplotlib

from selenium import webdriver
import time
import matplotlib.pyplot as plt
def performance(n,css_val,image_val,js_val):
    loading_time = []
    for i in range(0,n):
        firefox_profile = webdriver.FirefoxProfile()
        firefox_profile.set_preference("permissions.default.stylesheet",css_val)
        firefox_profile.set_preference("permissions.default.image",image_val)
        firefox_profile.set_preference("javascript.enabled",js_val)
        firefox_profile.update_preferences()
        firefox = webdriver.Firefox(firefox_profile)

        url = 'https://zhangslob.github.io/'
        print("开始加载")
        t_start = time.time()
        firefox.get(url)
        t_end = time.time()
        print("加载时间是:",t_end-t_start)
        loading_time.append(t_end-t_start)
        print('=========================')
        #time.sleep(10)
        firefox.quit()
    return [x for x in range(1,n+1)],loading_time

if __name__ == '__main__':
    x1_lst,y1_lst = performance(10,1,1,True)
    x2_lst,y2_lst = performance(10,2,2,False)

    ava_y1 = sum(y1_lst)/len(x1_lst)
    ava_y2 = sum(y2_lst)/len(x2_lst)

    plt.title("Compare loading time")
    plt.xlabel("Test number")
    plt.ylabel("Loading time")
    plt.plot(x1_lst,y1_lst,'go:',label=str(ava_y1))
    plt.plot(x2_lst,y2_lst,'rs:',label=str(ava_y2))
    plt.legend()
    plt.show()

我把网址换成了我的博客:崔斯特的博客(zhangslob.github.io/),这样更符合实际。

绿色的线是正常网页,红色是修改后的

为了确保准确,换成别的网址测试一下,可是为什么会这样?

问题:

为什么禁用JS、不加载图片,时间和原来相比差别不大?

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python爬虫与算法进阶 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、使用Firefox实例
  • 2、对Selenium的profile的配置
  • 3、画图
  • 问题:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档