seleniumwire简单使用

seleniumwire简单使用

import time
from seleniumwire import webdriver
from io import BytesIO
import gzip
options = webdriver.ChromeOptions()
# 防止出现报错:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte 
options.add_argument('lang=zh_CN.UTF-8')
# 更换头部
# 创建浏览器对象
driver = webdriver.Chrome(chrome_options=options)
# Go to the YouTube homepage.
driver.get('https://yanbaoke.com/search?tag=iot')
time.sleep(3)
# 下滑页面
# temp_height = 0
# while True:
#     # 循环将滚动条下拉
#     driver.execute_script("window.scrollBy(0,500)")
#     # sleep一下让滚动条反应一下
#     time.sleep(1)
#     # 获取当前滚动条距离顶部的距离
#     check_height = driver.execute_script(
#         "return document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;")
#     # 如果两者相等说明到底了
#     if check_height == temp_height:
#         print('到达底部')
#         break
#     temp_height = check_height

# Access requests via the `requests` attribute
for request in driver.requests:
    if request.response:
    	# 判断路径, 也可以.headers .host .url等,可以看看内置函数来
        if request.path == "/search3":
            print(request.path)
            print('--------------------------')
            # 解压数据,解决0x8b 错误,获取源代码
            buff = BytesIO(request.response.body)
            f = gzip.GzipFile(fileobj=buff)
            htmls = f.read().decode('utf-8')
            print(htmls.encode('utf-8').decode('utf-8'))
			# 正常情况
			# print(request.response.body.decode("utf-8"))
driver.quit()

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值