当前位置: 首页 > news >正文

展览网站建设广州网络营销

展览网站建设,广州网络营销,内容相同的 网站,wordpress数据库分离selenium的官方不提供获取状态码,Conten-Type,以及重定向路径的方法,并且官方说这些功能将来也不会有。java - How to get HTTP Response Code using Selenium WebDriver - Stack Overflow 非官方的方法大概有下面几种 1.通过requests重新请…

selenium的官方不提供获取状态码,Conten-Type,以及重定向路径的方法,并且官方说这些功能将来也不会有。java - How to get HTTP Response Code using Selenium WebDriver - Stack Overflow

非官方的方法大概有下面几种

1.通过requests重新请求一遍url,获取response里面的状态码

2.通过中间代理服务器来获取,比如selenium-wire,selenium-wire里面内建了一个代理服务器,通过代理服务器可以获取各个请求的状态码

3.通过分析chrome的performance log来获取状态码以及重定向路径

1的方法会多访问一遍网络,并且不能支持mata或js的重定向。2的方法只访问一次网络,但同样不能支持mata或js的重定向,只有3的方法只访问一次网络可以获取所有的状态。

并且1的方法在python环境上打开一些https网站还有如下bug

ssl.SSLError: [SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled (_ssl.c:1131) · Issue #2653 · urllib3/urllib3 · GitHuby

 这个问题的原因是This error comes up when using OpenSSL 3 to connect to a server which does not support it. The solution is to downgrade the cryptography package in python:

  python - SSL error unsafe legacy renegotiation disabled - Stack Overflow

要么修改openssl.cnf,要么降级cryptography,要么设置ctx,根据我测试的结果只有修改cnf才能彻底解决,最简单的方法是在cnf后面加上Options = UnsafeLegacyRenegotiation

如果使用docker应该写成这样

RUN echo 'Options = UnsafeLegacyRenegotiation' >> /usr/lib/ssl/openssl.cnf

如何通过performance log来获取状态

performanceLog.py

import jsondef getHeader(headers,header):for key,value in headers.items():if key.lower()==header:return valuereturn ''
def getRedirectPath(driver):redirctPath=[]requestDic={}frameId=Nonefor entry_json in driver.get_log('performance'):entry = json.loads(entry_json['message'])# print(entry)if entry['message']['method'] == 'Network.requestWillBeSent':if entry['message']['params']['loaderId']==entry['message']['params']['requestId'] and (frameId is None or frameId==entry['message']['params']['frameId']):# print(entry)if frameId is None:frameId=entry['message']['params']['frameId']if entry['message']['params']['redirectHasExtraInfo']:# 设置重定向前的状态item=redirctPath[len(redirctPath)-1]item['status']=entry['message']['params']['redirectResponse']['status']item['reason']='location'item['contentType']=getHeader(entry['message']['params']['redirectResponse']['headers'],'content-type')item={'url':entry['message']['params']['request']['url'],'status':'','reason':'','contentType':''}requestDic[entry['message']['params']['requestId']]=itemredirctPath.append(item)elif entry['message']['method'] == 'Network.responseReceived' and entry['message']['params']['requestId'] in requestDic:# print(entry)item=requestDic[entry['message']['params']['requestId']]item['status']=entry['message']['params']['response']['status']item['contentType']=getHeader(entry['message']['params']['response']['headers'],'content-type')elif entry['message']['method'] == 'Page.frameRequestedNavigation' and frameId==entry['message']['params']['frameId']:# print(entry)# 设置重定向前的状态item=redirctPath[len(redirctPath)-1]item['reason']=entry['message']['params']['reason']return redirctPath

 后来发现一些网站不能获取contentType,原因是header需要忽略大小写,修改了一下代码。当然你自己修改代码后可以获取任何你需要的header。

各个事件的顺序大致如下

Page.frameStartedLoading
Network.requestWillBeSent
Network.responseReceived
Network.dataReceived
Page.frameNavigated
Network.requestServedFromCache
Network.loadingFinished
Network.resourceChangedPriority
Page.domContentEventFired
Network.loadingFailed
Page.loadEventFired
Page.frameStoppedLoading

注释:Page.frameScheduledNavigation已经被废弃,改成Page.frameRequestedNavigation

官方对performance log的文档很少,在网上也没有找到类似的讨论,上面的代码完全是我根据数据分析出来的,目前看来是对的,如果不对请告诉我。 

Chrome DevTools Protocol - version 1-2 - Page domain

 测试代码

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
from performanceLog import getRedirectPathcaps = DesiredCapabilities.CHROME
caps['goog:loggingPrefs'] = {'performance': 'ALL'}
options = webdriver.ChromeOptions()
# 必须是headless=new,否则download.default_directory不起作用
options.add_argument('--headless=new')
# 下面这些参数是必须的,否则可能出错
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')driver = webdriver.Chrome(options=options,desired_capabilities=caps)driver.get('http://localhost/redirect')
# 必须等待几秒钟,否则metaTagRefresh不起作用
time.sleep(3)print(getRedirectPath(driver))
driver.quit()

输出结果

[{
    'url': 'http://localhost/redirect',
    'status': 302,
    'reason': 'location',
    'contentType': 'text/html; charset=utf-8'
}, {
    'url': 'http://localhost/static/index3.html',
    'status': 200,
    'reason': 'scriptInitiated',
    'contentType': 'text/html'
}, {
    'url': 'http://localhost/static/redirect.html',
    'status': 200,
    'reason': 'metaTagRefresh',
    'contentType': 'text/html'
}, {
    'url': 'http://localhost/',
    'status': 200,
    'reason': '',
    'contentType': 'text/html; charset=utf-8'
}]

可以看出这里面有3种不同类型的重定向,location的通常的重定向,后面2种用requests或者代理都无法获取。

location:response header里面的location重定向

scriptInitiated:js重定向

metaTagRefresh:meta tag重定向

http://www.yayakq.cn/news/288910/

相关文章:

  • html中文美食网站模板有效的网络营销方式
  • 推荐网站建设服务商应用商城官网下载最新版
  • 怀柔网站制作公司上海进博会2022
  • app网站及其特色朝阳网站建设培训
  • 为什么要建设公司网站昆明网页设计公司排行榜
  • 代做ppt的网站网络服务大厅
  • 天津网站建设招标怎样创建网站快捷方式到桌面
  • 广州定制网站建设方案书石家庄公司做网站
  • 做电脑网站二维码制作方法
  • 重庆网站建设公司是什么dw网站模板下载
  • 凡科免费做的网站淘宝网站开始怎么做的
  • 平台网站怎么推广最好的网站设计公司
  • 晋州有做网站的吗asp服装商城网站源码
  • 可以看封禁网站的浏览器wordpress上传视频只有声音
  • 深圳外贸英文网站设计联系电话wordpress多站点建站
  • 株洲网站建设 公司图文生成二维码免费
  • 左右左布局网站建设网络推广最好的网站有哪些
  • 建网站做代理ip唐山路北网站建设
  • 土木特网站建设上线了 网站
  • wordpress做管理网站关键词排名优化软件价格
  • 最优惠的赣州网站建设如何做影视网站的标题
  • 胶州为企业做网站的公司高端网站建设域名注册
  • wordpress模板建站教程建设一个网站的好处
  • 如何快速提升网站流量科学规划网页的做法是
  • 国家建设部查询网站青岛网站建设方案外包
  • 商务网站建设体会制作网页游戏过程
  • 合肥国际网站建设正规平台网站结构是什么 怎么做
  • net开发网站建设网站建设经验做法和取得的成效
  • 迅雷黄冈网站推广软件爬墙专用加速器
  • 外国购物网站设计风格多商户商城app开发