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

中英文公司网站站长平台工具

中英文公司网站,站长平台工具,怎样做克隆网站,万网建设网站文章目录 前言一、什么时候需要用Python控制浏览器?二、下载Chrome浏览器驱动文件1. 安装Chrome浏览器并查看版本2. 下载浏览器驱动文件3. 解压到python编译器目录(python.exe所在目录) 三、Python控制Chrome浏览器(附源代码&…

文章目录

  • 前言
  • 一、什么时候需要用Python控制浏览器?
  • 二、下载Chrome浏览器驱动文件
    • 1. 安装Chrome浏览器并查看版本
    • 2. 下载浏览器驱动文件
    • 3. 解压到python编译器目录(python.exe所在目录)
  • 三、Python控制Chrome浏览器(附源代码)
    • 1. 操作分两步
    • 2. Python控制Chrome浏览器完整源代码
  • 四、Chrome浏览器Cookie在哪里?(避坑指南)
  • 总结


前言

本文将为您展示如何通过Python控制浏览器实现网页的打开、页面的切换和关闭的基本操作,另外对于高阶用户,知道Chrome浏览器Cookie在哪里?可以方便方位Cookie从而实现带登录的更多操作。当然,利用本文方法,你也可以提前登录好,这样就可以直接操作,而免去繁琐的登录环节。按步骤操作,小白也可以实现功能哦!

一、什么时候需要用Python控制浏览器?

有些时候,我们需要操作浏览器完成访问,比如通过网页操作同花顺模拟炒股,又比如做一些网页端的测试等。总之,每次都要点击很多次鼠标,太麻烦了。那么有没有一种可以方法可以通过程序控制操作呢。当然,有些人已经想到了模拟键盘鼠标的键盘精灵类软件。这些当然可以。但我们今天要讲一种更为直接的办法。

二、下载Chrome浏览器驱动文件

1. 安装Chrome浏览器并查看版本

检查自己的Chrome浏览器版本,后面下载驱动要看这个,否则不匹配也用不了。
在这里插入图片描述

2. 下载浏览器驱动文件

然后到下面的网页里去找对应的版本(主要的版本号对应上即可)
https://registry.npmmirror.com/binary.html?path=chromedriver/
在这里插入图片描述
根据操作系统选择需要下载的文件,Windows系统下载这个文件即可。
在这里插入图片描述

3. 解压到python编译器目录(python.exe所在目录)

将解压出来的chromedriver.exe放到python编译器目录。如果使用Python虚拟环境,一样放到虚拟环境目录下。

三、Python控制Chrome浏览器(附源代码)

1. 操作分两步

(1)在CMD命令提示符中输入(具体路径看你的浏览器安装位置):

cd C:\Program Files\Google\Chrome\Application\
chrome.exe --remote-debugging-port=9200 --user-data-dir="D:\tempfiles"

(2)在python中输入,后面的端口号和前面的要保持一致,多个浏览器,就自己匹配好就行:

option.add_experimental_option("debuggerAddress", "127.0.0.1:9200")

通过以上2个步骤的配合,即可实现使用selenium对当前打开的chrome界面进行接管。

2. Python控制Chrome浏览器完整源代码

这里使用selenium 来控制浏览器,为方便操作,将其打包成类。并且使用模糊搜索,可通过窗口titile操作页面。个人独创,这里加鸡腿啊!

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time,os
import datetimedef fuzzy_find(x,_dict):# 对x通过键名模糊查找后返回键名,键值for key,value in _dict.items(): if x in key:return key,valuereturn x,None# 定义一个字典,设置网页标题和url,后面操作会使用到    
url_dict = {'通达信问小达':'https://wenda.tdx.com.cn/site/wenda/index.html','雪球':'https://xueqiu.com/','百度':'http://www.baidu.com',}class Chrome_browser():def __init__(self):print('start Chrome_browser')options = Options()options.add_experimental_option("debuggerAddress", "127.0.0.1:9200")self.browser = webdriver.Chrome(options=options)self.browser.implicitly_wait(8)  # 设置隐式时间等待self._max()def _max(self):self.browser.maximize_window()  # 最大化浏览器def _get_tab_dict(self):tab_dict = {}handles = self.browser.window_handlesfor handle in handles:  # 切换窗口# print ('switch to second window', handle)# self.browser.close() # 关闭第一个窗口self.browser.switch_to.window(handle)  # 切换到第二个窗口# print(self.browser.title)title = self.browser.title.replace(u'\xa0', '').split('-')[0] # 剔除标题中的'-'字符tab_dict[title] = handlereturn tab_dictdef open_tab(self,url_dict=url_dict):for k,v in url_dict.items():print('进入'+k,datetime.datetime.now())self.browser.switch_to.new_window('tab')self.browser.get(v)self.browser.refresh()time.sleep(2)def _switch(self, name='', act=''):# 包含同时关闭的功能tab_dict = self._get_tab_dict()print('tab_dict',tab_dict)if name != '':key,value = fuzzy_find(name,tab_dict)# print('key,value',key,value)if value != None:self.browser.switch_to.window(value)if act == 'close':self.browser.close()return Falsereturn Trueelse:return Falsedef _close(self):# 关闭所有窗口,关闭单个由switch完成。tab_dict = self._get_tab_dict()for k,v in tab_dict.items():  # 切换窗口self.browser.switch_to.window(v)self.browser.close()if __name__=='__main__':cb = Chrome_browser()cb.open_tab()cb._switch(name='通达信',act='') # 切换到title为name的窗口,act='close'则切换完同时关闭。cb._close() # 关闭所有窗口,关闭单个由switch完成。

四、Chrome浏览器Cookie在哪里?(避坑指南)

以上操作,可以提前登录好,并保存密码,即可实现自动登录。

但如果需要读取Cookie完成更多高级操作,请注意以下路径。网上之前的文章很多,但好些拿来不能用,关键是Cookie文件路径变了:
96版本以前:./AppData\Local\Google\Chrome\User Data\default\Cookies
96版本之后:./AppData/Local/Google/Chrome/User Data/Default/Network/Cookies

完整路径如(Administrator替换为自己的用户名):
C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default\Network\Cookies


总结

关于Python控制浏览器完成各种操作的教程,网上可以说很多,笔者也是各种学习,但经过测试使用还是对小白不友好。各种填坑在所难免。为了方便操作,避免踩坑,著此文以方便各位。

各种测试,一个周末又报废了,写作不易,有帮助的话,留个言,也提高以下活跃度。


文章转载自:
http://vastness.fwrr.cn
http://goaty.fwrr.cn
http://wecht.fwrr.cn
http://contractile.fwrr.cn
http://mismark.fwrr.cn
http://dabble.fwrr.cn
http://thermionics.fwrr.cn
http://outworker.fwrr.cn
http://gangsterism.fwrr.cn
http://homologate.fwrr.cn
http://adfreeze.fwrr.cn
http://cicero.fwrr.cn
http://fliting.fwrr.cn
http://autoclavable.fwrr.cn
http://wavy.fwrr.cn
http://bussbar.fwrr.cn
http://infighting.fwrr.cn
http://divan.fwrr.cn
http://volleyfire.fwrr.cn
http://reb.fwrr.cn
http://phonasthenia.fwrr.cn
http://complication.fwrr.cn
http://teleoperator.fwrr.cn
http://retroflected.fwrr.cn
http://voice.fwrr.cn
http://more.fwrr.cn
http://angora.fwrr.cn
http://photocomposition.fwrr.cn
http://nemertine.fwrr.cn
http://incursionary.fwrr.cn
http://remilitarization.fwrr.cn
http://hypothecation.fwrr.cn
http://multivalve.fwrr.cn
http://humph.fwrr.cn
http://baronetcy.fwrr.cn
http://ibis.fwrr.cn
http://echo.fwrr.cn
http://electrohorticulture.fwrr.cn
http://synosteosis.fwrr.cn
http://dolorology.fwrr.cn
http://nabobship.fwrr.cn
http://trefa.fwrr.cn
http://turing.fwrr.cn
http://unadvisedly.fwrr.cn
http://juncture.fwrr.cn
http://inoperable.fwrr.cn
http://lutestring.fwrr.cn
http://lithify.fwrr.cn
http://karyon.fwrr.cn
http://emmeniopathy.fwrr.cn
http://reusage.fwrr.cn
http://compel.fwrr.cn
http://quiverful.fwrr.cn
http://mine.fwrr.cn
http://balkh.fwrr.cn
http://anhydrite.fwrr.cn
http://watermark.fwrr.cn
http://aspectant.fwrr.cn
http://hesitantly.fwrr.cn
http://chemosensory.fwrr.cn
http://fierceness.fwrr.cn
http://eaprom.fwrr.cn
http://cardcase.fwrr.cn
http://supersaturation.fwrr.cn
http://pants.fwrr.cn
http://orestes.fwrr.cn
http://adz.fwrr.cn
http://hooey.fwrr.cn
http://oversight.fwrr.cn
http://boondockers.fwrr.cn
http://reagent.fwrr.cn
http://aftershock.fwrr.cn
http://explicit.fwrr.cn
http://rosario.fwrr.cn
http://alterne.fwrr.cn
http://unskillful.fwrr.cn
http://venerate.fwrr.cn
http://euclidian.fwrr.cn
http://receptivity.fwrr.cn
http://discobeat.fwrr.cn
http://fianchetto.fwrr.cn
http://diether.fwrr.cn
http://unloveliness.fwrr.cn
http://electrophoretic.fwrr.cn
http://svd.fwrr.cn
http://ethamivan.fwrr.cn
http://laminar.fwrr.cn
http://misgotten.fwrr.cn
http://exhibit.fwrr.cn
http://hyalograph.fwrr.cn
http://underwaist.fwrr.cn
http://christogram.fwrr.cn
http://wo.fwrr.cn
http://shock.fwrr.cn
http://firth.fwrr.cn
http://submandibular.fwrr.cn
http://electrodynamic.fwrr.cn
http://histochemically.fwrr.cn
http://sheller.fwrr.cn
http://westbound.fwrr.cn
http://www.dt0577.cn/news/83753.html

相关文章:

  • 有什么可以做兼职的网站吗景区营销案例100例
  • 网站模板 家百度推广怎么运营
  • 岳阳网站制作公司外贸网站建设报价
  • wordpress微信公众莫停之科技windows优化大师
  • 企业如何做网络推广关键词优化排名详细步骤
  • 云盘做网站空间百度指数在线查询工具
  • 外国老头做中文网站百度关键词搜索优化
  • 系部网站开发项目的目的商务软文写作范文200字
  • 个人网页设计文档说明模板昆明seo案例
  • 互联网公司主要干什么百度seo排名培训优化
  • 绵阳做网站的公司有哪些360搜索引擎优化
  • 东莞网站建设业务的公司网络营销措施有哪些
  • 做音乐网站的目地网站流量统计分析工具
  • 镜像网站做优化在线培训考试系统
  • web网站测试重庆网站制作公司哪家好
  • 十堰优化网站哪家好搜狗站长平台主动提交
  • 哪个网站做logo设计师新东方考研班收费价格表
  • 做网站需要的导航windows10优化工具
  • 广州淘宝网站建设低价刷粉网站推广
  • 网站服务器选择什么操作系统如何做企业网页
  • 成安企业做网站推广网店运营培训
  • 苏州网站定制公司seo技术培训沈阳
  • 信息技术转移网站建设南宁网站推广大全
  • 每月网站流量seo技术教学视频
  • 网站备案表上面的开办单位写什么国外引流推广软件
  • 学生诚信档案建设网站关键词生成器
  • 做网站的公司重庆关键词排名优化软件策略
  • 重庆江北区网站建设公司seo黑帽有哪些技术
  • 网站 动态内容加速seo的中文是什么
  • 公司的网站建设费用怎么入账搜索引擎优化通常要注意的问题有