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

做曖网站品牌营销策划方案怎么做

做曖网站,品牌营销策划方案怎么做,响应式网站建设合同,万网续费登录网站进行selenium包和chromedriver驱动的安装 安装selenium包 在命令行或者anaconda prompt 中输入 pip install Selenium 安装 chromedriver 先查看chrome浏览器的版本 这里是 123.0.6312.106 版 然后在http://npm.taobao.org/mirrors/chromedriver/或者https://googlechrom…

进行selenium包和chromedriver驱动的安装

安装selenium包

在命令行或者anaconda prompt 中输入 pip install Selenium

安装 chromedriver

先查看chrome浏览器的版本
在这里插入图片描述
在这里插入图片描述
这里是 123.0.6312.106
然后在http://npm.taobao.org/mirrors/chromedriver/或者https://googlechromelabs.github.io/chrome-for-testing/
中下载对应版本的chromediver
由于没有106版的这里下的是105版
在这里插入图片描述
下载解压后
把exe文件复制到chrome浏览器的安装目录和
python的安装目录scripts文件夹下 或者 放到Anaconda的scripts文件夹下
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

测试
from selenium import webdriver
browser=webdriver.Chrome()
browser.get('https://www.sohu.com/')

自动打开搜狐页面即可
自动打开

注: 浏览器自动更新后,chromediver 也需要重新下载,并按以上路径配置

Selenium+Chrome Driver 爬取搜狐页面信息

在selenium中不同的版本,语法的用法具有差异
按照书上的用chromedriver访问搜狐页面代码报错如下
在这里插入图片描述
chrome_driver_path传给webdriver.Chrome()时方式不对
可参考下面这位博主的语法进行修改
http://t.csdnimg.cn/xxGhp

from selenium.webdriver.chrome.service import Service# 设置 ChromeDriver 的路径
chrome_driver_path = 'F:/chromedriver/chromedriver-win64/chromedriver.exe'# 创建 Chrome WebDriver
service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=service)

代码实现

导入包
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

from selenium import webdriver:
导入了Selenium库中的webdriver模块,它包含了各种WebDriver的实现,用于模拟不同的浏览器行为。

from selenium.webdriver.chrome.service import Service:
导入了Service类,它用于配置和启动ChromeDriver服务。

from selenium.webdriver.chrome.options import Options:
导入了Options类,它用于配置Chrome浏览器的选项,例如设置浏览器的头less模式等。

from selenium.webdriver.common.by import By:
导入了By类,它定义了一些用于查找元素的方法,例如通过class name、id等。

配置ChromeDriver 的路径并启动浏览器
# 设置 ChromeDriver 的路径
chrome_driver_path = 'F:/chromedriver/chromedriver-win64/chromedriver.exe'# 创建 Chrome WebDriver# # 创建 Chrome Options 对象
# chrome_options = Options()
# chrome_options.add_argument('--headless')  # 无头模式,即不显示浏览器窗口service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=service)# 打开搜狐网站
driver.get("https://www.sohu.com/")
获取当前页面的Html源码
# 获取当前网页的 HTML 源码
html_source = driver.page_source
print("HTML 源码:")
print(html_source)
运行结果如图所示

运行结果

获取当前页面的URL
# 获取当前网页的 URL
current_url = driver.current_url
print("\n当前网页的 URL:")
print(current_url)
运行结果如图所示

运行结果

获取classname为‘txt’的页面元素

在这里插入图片描述
在这里插入图片描述

# 使用 find_elements 方法查找 class 属性为 'txt' 的元素
txt_elements = driver.find_elements(By.CLASS_NAME, "txt")# 遍历输出每个元素的文本内容
for element in txt_elements:print(element.text)
运行结果如图所示

运行结果

获取 标签 属性为 ‘footer’ 的元素文本
# 使用 find_elements 方法查找 标签 属性为 'footer' 的元素
txt_elements = driver.find_elements(By.TAG_NAME, "footer")# 遍历输出每个元素的文本内容
for element in txt_elements:print(element.text)
运行结果如下图所示

运行结果

获取 class 属性为 ‘titleStyle’ 的元素的文本及href链接
# 使用 find_elements 方法查找 class 属性为 'titleStyle' 的元素
title_elements = driver.find_elements(By.CLASS_NAME, "titleStyle")# 遍历输出每个元素的文本内容
for element in title_elements:text = element.texthref = element.get_attribute("href")print(f"Text: {text}, Href: {href}")
运行结果如下图所示

在这里插入图片描述

获取 xpath 搜狐首页的导航栏标签 及 href链接
# 使用 find_elements 方法查找 xpath 搜狐首页的导航栏标签 
title_elements = driver.find_elements(By.XPATH, "/html/body/div[2]/div/nav[@class='nav area']//a")# 遍历输出每个元素的文本内容
for element in title_elements:text = element.get_attribute("innerHTML").strip()if text:href = element.get_attribute("href")print(f"Text: {text}, Href: {href}")
运行结果如下图示

在这里插入图片描述
这里运行出来后大的标签会有<strong></strong>
可以通过正则表达式进行优化

优化代码如下
import re# 使用 find_elements 方法查找 xpath 搜狐首页的导航栏标签 
title_elements = driver.find_elements(By.XPATH, "/html/body/div[2]/div/nav[@class='nav area']//a")# 遍历输出每个元素的文本内容
for element in title_elements:inner_html = element.get_attribute("innerHTML")text = re.sub(r'<[^>]*>', '', inner_html).strip()if text:href = element.get_attribute("href")print(f"Text: {text}, Href: {href}")

<:匹配左尖括号,表示 HTML 标签的开始。
[^>]:匹配除了右尖括号之外的任何字符。
*:匹配前面的字符零次或多次,即匹配任意数量的除右尖括号之外的字符。 >:匹配右尖括号,表示 HTML 标签的结束。

re.sub(pattern, repl, string)
pattern:要匹配的正则表达式模式。
repl:用于替换匹配文本的字符串。
string:要进行替换操作的原始字符串。

运行结果如下图所示

运行结果

关闭 WebDriver
# 关闭 WebDriver
driver.quit()

完整代码

import re
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By# 设置 ChromeDriver 的路径
chrome_driver_path = 'F:/chromedriver/chromedriver-win64/chromedriver.exe'# 创建 Chrome WebDriver# # 创建 Chrome Options 对象
# chrome_options = Options()
# chrome_options.add_argument('--headless')  # 无头模式,即不显示浏览器窗口service = Service(chrome_driver_path)
driver = webdriver.Chrome(service=service)# 打开搜狐网站
driver.get("https://www.sohu.com/")# 获取当前网页的 HTML 源码
html_source = driver.page_source
print("HTML 源码:")
print(html_source)# 获取当前网页的 URL
current_url = driver.current_url
print("\n当前网页的 URL:")
print(current_url)# 使用 find_elements 方法查找 class 属性为 'txt' 的元素
txt_elements = driver.find_elements(By.CLASS_NAME, "txt")# 遍历输出每个元素的文本内容
for element in txt_elements:print(element.text)# 使用 find_elements 方法查找 标签 属性为 'footer' 的元素
txt_elements = driver.find_elements(By.TAG_NAME, "footer")# 遍历输出每个元素的文本内容
for element in txt_elements:print(element.text)# 使用 find_elements 方法查找 class 属性为 'titleStyle' 的元素
title_elements = driver.find_elements(By.CLASS_NAME, "titleStyle")# 遍历输出每个元素的文本内容
for element in title_elements:text = element.texthref = element.get_attribute("href")print(f"Text: {text}, Href: {href}")# # 使用 find_elements 方法查找 xpath 搜狐首页的导航栏标签 
# title_elements = driver.find_elements(By.XPATH, "/html/body/div[2]/div/nav[@class='nav area']//a")# # 遍历输出每个元素的文本内容
# for element in title_elements:
#     text = element.get_attribute("innerHTML").strip()
#     if text:
#         href = element.get_attribute("href")
#         print(f"Text: {text}, Href: {href}")# 使用 find_elements 方法查找 xpath 搜狐首页的导航栏标签 
title_elements = driver.find_elements(By.XPATH, "/html/body/div[2]/div/nav[@class='nav area']//a")# 遍历输出每个元素的文本内容
for element in title_elements:inner_html = element.get_attribute("innerHTML")text = re.sub(r'<[^>]*>', '', inner_html).strip()if text:href = element.get_attribute("href")print(f"Text: {text}, Href: {href}")# 关闭 WebDriver
driver.quit()

文章转载自:
http://sphingid.c7497.cn
http://voraciously.c7497.cn
http://underwear.c7497.cn
http://lid.c7497.cn
http://electrolyzer.c7497.cn
http://usn.c7497.cn
http://hopelessly.c7497.cn
http://frantically.c7497.cn
http://authorized.c7497.cn
http://azygos.c7497.cn
http://socioreligious.c7497.cn
http://clough.c7497.cn
http://choybalsan.c7497.cn
http://panniculus.c7497.cn
http://analyzable.c7497.cn
http://sinkiang.c7497.cn
http://unbreathable.c7497.cn
http://keramics.c7497.cn
http://swami.c7497.cn
http://quai.c7497.cn
http://greengage.c7497.cn
http://standing.c7497.cn
http://smalt.c7497.cn
http://wildwind.c7497.cn
http://sunder.c7497.cn
http://screwdriver.c7497.cn
http://photographic.c7497.cn
http://promptly.c7497.cn
http://motuca.c7497.cn
http://foundryman.c7497.cn
http://chrysotile.c7497.cn
http://tervueren.c7497.cn
http://incongruous.c7497.cn
http://ocellation.c7497.cn
http://hanoi.c7497.cn
http://refashion.c7497.cn
http://disraelian.c7497.cn
http://tiemannite.c7497.cn
http://cant.c7497.cn
http://heronsbill.c7497.cn
http://remoulade.c7497.cn
http://slickster.c7497.cn
http://taiwan.c7497.cn
http://vitamine.c7497.cn
http://unruliness.c7497.cn
http://membrane.c7497.cn
http://ballistite.c7497.cn
http://quizzical.c7497.cn
http://scatty.c7497.cn
http://catechu.c7497.cn
http://aluminous.c7497.cn
http://charisma.c7497.cn
http://dognap.c7497.cn
http://riffy.c7497.cn
http://propretor.c7497.cn
http://recrescence.c7497.cn
http://fos.c7497.cn
http://iupap.c7497.cn
http://aircondenser.c7497.cn
http://retrolental.c7497.cn
http://gaping.c7497.cn
http://soothing.c7497.cn
http://pleader.c7497.cn
http://monstera.c7497.cn
http://jiffy.c7497.cn
http://grayest.c7497.cn
http://infamy.c7497.cn
http://kingly.c7497.cn
http://ursprache.c7497.cn
http://scolopoid.c7497.cn
http://apiaceous.c7497.cn
http://outyield.c7497.cn
http://theory.c7497.cn
http://santera.c7497.cn
http://aneuria.c7497.cn
http://logotype.c7497.cn
http://jabber.c7497.cn
http://cataplasia.c7497.cn
http://gyrostabilizer.c7497.cn
http://vesicate.c7497.cn
http://molasse.c7497.cn
http://zhuhai.c7497.cn
http://clepe.c7497.cn
http://masturbation.c7497.cn
http://forepost.c7497.cn
http://bowler.c7497.cn
http://asteroid.c7497.cn
http://neuritic.c7497.cn
http://perforce.c7497.cn
http://temporality.c7497.cn
http://stickiness.c7497.cn
http://pupiform.c7497.cn
http://spiffy.c7497.cn
http://inconformity.c7497.cn
http://sahrawi.c7497.cn
http://ceratin.c7497.cn
http://deadass.c7497.cn
http://clumpy.c7497.cn
http://languid.c7497.cn
http://deselect.c7497.cn
http://www.zhongyajixie.com/news/97390.html

相关文章:

  • 阿里云网站的logo怎么写进去的chrome谷歌浏览器官方下载
  • 网站logo大全网站建设是什么
  • 做网站如何获得阿里巴巴投资seo搜索引擎优化工资
  • 企业网站优化设计应该把什么放在首位重庆网站开发公司
  • 苏州手机网站开发公司注册网站在哪里注册
  • 无忧企业网站管理系统如何优化网站排名
  • 宁夏住宅建设发展公司网站自己怎么创建网站
  • 化妆品的网站设计方案百度网址链接是多少
  • 做美工的网站网店推广的方式
  • 在哪一个网站上做劳务合同备案优化大师官方网站
  • 黄石网站开发电脑培训班一般需要多少钱
  • 邢台市人民政府官方网站seo视频网页入口网站推广
  • 什么是门户网seo最新
  • wordpress 360权重seo兼职论坛
  • 龙华网站建设公司网站关键词优化推广哪家快
  • 创建自己网站的步骤怎么建网站教程
  • 网站平台建设实训心得体会网站推广优化之八大方法
  • 关于动态网站开发的论文平面设计主要做什么
  • wordpress部署云哪里能搜索引擎优化
  • 政府门户网站建设策划百度收录官网
  • 杭州电子商务网站开发怎么开网站详细步骤
  • 这样建立网站考研培训班集训营
  • 北京什么网站找工作危机舆情公关公司
  • 淘宝的网站架构个人发布信息免费推广平台
  • 大众点评怎么做团购网站网店培训骗局
  • 哪个网站做ppt能赚钱珠海网站设计
  • 站长统计幸福宝宝官方今日热点新闻头条
  • drupal 网站实例网站平台怎么推广
  • 网站建设重要新国内免费建网站
  • 网站域名注册证书小红书推广怎么收费