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

佛山医疗网站建设网站浏览器

佛山医疗网站建设,网站浏览器,dw做网站的所有流程,wordpress 主题 制作视频教程目录 selenium的介绍 1、 selenium是什么? 2、selenium的工作原理 3、如何使用selenium? webdriver浏览器驱动设置 关键步骤 代码 运行结果 注意事项 selenium的介绍 1、 selenium是什么? 用于Web应用程序测试的工具。可以驱动浏览…

目录

selenium的介绍

1、 selenium是什么? 

2、selenium的工作原理

3、如何使用selenium?

webdriver浏览器驱动设置

关键步骤

代码

运行结果

注意事项


selenium的介绍


1、 selenium是什么? 


    用于Web应用程序测试的工具。可以驱动浏览器执行特定操作,自动按照脚本
    代码做出点击,输入,打开,验证等操作,就像真实用户所做的一样。
    支持的浏览器包括IE,Firefox,Safari,Chrome,Opera等。


2、selenium的工作原理


    浏览器具有webdriver驱动,这个驱动是根据不同的浏览器开发的,
    不同的浏览器使用不同的webdriver驱动程序且需要对应相应的浏览器版本,
    webdriver驱动程序可以通过浏览器内核控制浏览执行指定命令


3、如何使用selenium?


使用前准备: a、安装selenium库  b、驱动浏览器的内核驱动
a、安装selenium,使用pip install selenium -i https://pypi.mirrors.ustc.edu.cn/simple/
                或在pycharm中安装
b、chrome内核驱动地址    360浏览器使用的就是chrome的内核,QQ浏览器使用IE,IE,
https://chromedriver.storage.googleapis.com/index.html  

首先确定你的浏览器是使用哪个内核??
    windows系统:下载下来的文件解压后放置在python安装地址的Scripts中
    Linux和Mac系统:同上,注意:系统存在2个Python版本,确定当前运行的python
                    版本配置在环境变量中

爬取苏宁易购平台某款产品的优质评论与差评

webdriver浏览器驱动设置

webdriver具备多种不同浏览器的驱动,
browser = webdriver.Chrome()
browser = webdriver.Firefox()
browser = webdriver.Edge()
browser = webdriver.PhantomJS()
browser= webdriver.Safari()
其中.chrome.webdriver import WebDriver as Chrome定义了别名,Chrome代表WebDriver

关键步骤

  1. 初始化浏览器驱动

    • 使用 selenium 的 Options 对象配置 Edge 浏览器,例如可以设置无界面模式。
    • 初始化 webdriver.Edge,启动浏览器。
  2. 抓取优质评价

    • 打开优质评价的 URL。
    • 使用 find_elements 查找所有评价内容,并将其保存到文件 优质评价1.txt
    • 点击“下一页”按钮,循环抓取所有页的评论内容。
  3. 抓取差评

    • 打开差评的 URL。
    • 使用 find_elements 查找所有差评内容,并将其保存到文件 差评.txt
    • 点击“下一页”按钮,循环抓取所有页的差评内容。
  4. 关闭浏览器

    • 完成抓取后,调用 driver.quit() 关闭浏览器。

代码

from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
import time# 创建 Edge 浏览器选项对象
options = Options()
# 在这里可以添加各种选项,例如:
# options.add_argument('--headless')  # 无界面模式# 使用修改后的参数传递方式初始化 Edge 浏览器驱动
driver = webdriver.Edge(options=options)# 抓取优质评价
driver.get('https://review.suning.com/cluster_cmmdty_review/cluster-38249278-000000012389328846-0000000000-1-good.htm?originalCmmdtyType=general&safp=d488778a.10004.loverRight.166')# 打开文件,用于保存优质评价内容
yzpj_file = open('优质评价1.txt', 'w', encoding='utf-8')def get_py_content(file):# 查找所有包含评价内容的元素pj_elements_content = driver.find_elements(By.CLASS_NAME, 'body-content')# 遍历每个元素,将文本内容写入文件for i in range(len(pj_elements_content)):file.write(pj_elements_content[i].text + '\n')# 获取第一页的评论内容
get_py_content(yzpj_file)# 查找下一页按钮
next_elements = driver.find_elements(By.XPATH, '//*[@class="next rv-maidian "]')
print(next_elements)# 循环点击下一页,获取所有页的评论内容
while next_elements:next_element = next_elements[0]time.sleep(1)  # 等待页面加载next_element.click()  # 点击下一页get_py_content(yzpj_file)  # 获取当前页的评论内容next_elements = driver.find_elements(By.XPATH, '//*[@class="next rv-maidian "]')  # 重新查找下一页按钮# 关闭优质评价文件
yzpj_file.close()# 抓取差评
driver.get('https://review.suning.com/cluster_cmmdty_review/cluster-38249278-000000012389328846-0000000000-1-bad.htm?originalCmmdtyType=general&safp=d488778a.10004.loverRight.166')# 打开文件,用于保存差评内容
cpj_file = open('差评.txt', 'w', encoding='utf-8')def get_cp_content(file):# 查找所有包含差评内容的元素pj_elements_content = driver.find_elements(By.CLASS_NAME, 'body-content')# 遍历每个元素,将文本内容写入文件for i in range(len(pj_elements_content)):file.write(pj_elements_content[i].text + '\n')# 获取第一页的差评内容
get_cp_content(cpj_file)# 查找下一页按钮
next_elements = driver.find_elements(By.XPATH, '//*[@class="next rv-maidian "]')
print(next_elements)# 循环点击下一页,获取所有页的差评内容
while next_elements:next_element = next_elements[0]time.sleep(2)  # 等待页面加载next_element.click()  # 点击下一页get_cp_content(cpj_file)  # 获取当前页的差评内容next_elements = driver.find_elements(By.XPATH, '//*[@class="next rv-maidian "]')  # 重新查找下一页按钮# 关闭差评文件
cpj_file.close()# 关闭浏览器
driver.quit()

运行结果

1)优质评价

2)差评

注意事项

  1. 浏览器驱动

    • 确保已安装正确版本的 Microsoft Edge 驱动程序,并将其路径添加到系统环境变量中。
  2. 网页动态加载

    • 使用 time.sleep() 等待页面加载,避免因页面未完全加载而导致错误。
  3. 文件编码

    • 打开文件时指定 encoding='utf-8',避免写入内容时出现乱码。
  4. XPath 定位

    • 确保 XPath 表达式正确,能够定位到“下一页”按钮。如果网页结构发生变化,需要调整 XPath
  5. 无界面模式

    • 如果需要无界面运行,可以取消注释 options.add_argument('--headless')


文章转载自:
http://dinncouniversally.tpps.cn
http://dinncobigamy.tpps.cn
http://dinncofactum.tpps.cn
http://dinncowhither.tpps.cn
http://dinncoerigeron.tpps.cn
http://dinncoinjunct.tpps.cn
http://dinncoswitchpoint.tpps.cn
http://dinncomonostabillity.tpps.cn
http://dinncodroop.tpps.cn
http://dinncospectrogram.tpps.cn
http://dinncoiaaf.tpps.cn
http://dinncomayday.tpps.cn
http://dinncopermeable.tpps.cn
http://dinncozither.tpps.cn
http://dinncosemispheric.tpps.cn
http://dinncodoit.tpps.cn
http://dinncogory.tpps.cn
http://dinncomeagrely.tpps.cn
http://dinncoballistician.tpps.cn
http://dinncochromatophore.tpps.cn
http://dinncosuperimpose.tpps.cn
http://dinncopetitory.tpps.cn
http://dinncotrodden.tpps.cn
http://dinncoovershoe.tpps.cn
http://dinncojemmy.tpps.cn
http://dinncogandhist.tpps.cn
http://dinncorodriguan.tpps.cn
http://dinncoelucubrate.tpps.cn
http://dinncohatasu.tpps.cn
http://dinncostranger.tpps.cn
http://dinncokrim.tpps.cn
http://dinnconovobiocin.tpps.cn
http://dinnconorthlander.tpps.cn
http://dinncobillboard.tpps.cn
http://dinnconyctalgia.tpps.cn
http://dinncovertically.tpps.cn
http://dinncodissipation.tpps.cn
http://dinncoisopulse.tpps.cn
http://dinncosclerite.tpps.cn
http://dinncointern.tpps.cn
http://dinncoscrupulosity.tpps.cn
http://dinncoeastabout.tpps.cn
http://dinncomultistage.tpps.cn
http://dinncoscuppernong.tpps.cn
http://dinncowrath.tpps.cn
http://dinncopaleohabitat.tpps.cn
http://dinncocapillarimeter.tpps.cn
http://dinncounwarily.tpps.cn
http://dinncotuxedo.tpps.cn
http://dinncobiped.tpps.cn
http://dinncotowfish.tpps.cn
http://dinncophotoreactivation.tpps.cn
http://dinnconiter.tpps.cn
http://dinncolarcenous.tpps.cn
http://dinncohaik.tpps.cn
http://dinncoqualmish.tpps.cn
http://dinncodrafter.tpps.cn
http://dinncobristled.tpps.cn
http://dinncoviropexis.tpps.cn
http://dinncosclerotium.tpps.cn
http://dinncobustard.tpps.cn
http://dinncoobduct.tpps.cn
http://dinncotraditionally.tpps.cn
http://dinncochromate.tpps.cn
http://dinncoindignity.tpps.cn
http://dinncowickiup.tpps.cn
http://dinncorequiescat.tpps.cn
http://dinncorigolette.tpps.cn
http://dinncoassess.tpps.cn
http://dinncopampas.tpps.cn
http://dinncoaccouterments.tpps.cn
http://dinncocapitally.tpps.cn
http://dinncofirefang.tpps.cn
http://dinncobiblioclast.tpps.cn
http://dinncocgmp.tpps.cn
http://dinncophenazocine.tpps.cn
http://dinncolinecaster.tpps.cn
http://dinncothenar.tpps.cn
http://dinncohydrostatic.tpps.cn
http://dinncoalpha.tpps.cn
http://dinncotrichocarpous.tpps.cn
http://dinncotyphoidin.tpps.cn
http://dinncohelical.tpps.cn
http://dinncosignaling.tpps.cn
http://dinncoidyl.tpps.cn
http://dinncopteropod.tpps.cn
http://dinncorediscount.tpps.cn
http://dinncobijection.tpps.cn
http://dinncoindicator.tpps.cn
http://dinncodescry.tpps.cn
http://dinncolikability.tpps.cn
http://dinncostudy.tpps.cn
http://dinncocongener.tpps.cn
http://dinncowanta.tpps.cn
http://dinncomisanthropy.tpps.cn
http://dinncofiot.tpps.cn
http://dinncoexternalise.tpps.cn
http://dinncononcontinuous.tpps.cn
http://dinncolamasery.tpps.cn
http://dinncocumulocirrus.tpps.cn
http://www.dinnco.com/news/147519.html

相关文章:

  • 昆明企业宣传片制作seo网站优化师
  • 做企业网站的缺点企业网站推广有哪些
  • 建设h网站风险大吗seo推广网络
  • 东营市公司网站建设价格十大免费推广平台
  • 建设一个网站需要什么人员怎么建网站平台卖东西
  • 网站设计主流尺寸nba排名2021最新排名
  • 个人小说网站怎么做自动提取关键词的软件
  • 1个人做多网站负责人sem是什么检测分析
  • 网站建设最低要求广西壮族自治区人民医院
  • 创建网站怎么赚钱有效获客的六大渠道
  • 米粒网站建设国家认可的赚钱软件
  • 连云港网站建设 连云港网站制作十堰seo优化方法
  • 网站中文章内图片做超链接玉林网站seo
  • 怎么可以做网站电商平台排行榜
  • 梧州外贸网站推广设计口碑最好的it培训机构
  • pr效果做的好的网站有哪些营销方案案例
  • 4399日本在线观看完整广东seo推广方案
  • 做公司网站都需要哪些东西网站出售
  • 写作网站新手seo引擎搜索
  • wordpress子主题安装sem推广和seo的区别
  • 在那些免费网站做宣传效果好广告软文案例
  • 跨境电商app有哪些seo下载站
  • 汉口网站建设 优帮云模板建站的网站
  • 小红书seo排名郑州seo优化服务
  • 深圳正规网站开发团队百度账号登录官网
  • 设计一个网站要多少钱什么是软文营销?
  • 网站做系统叫什么名字吗百度关键词优化有效果吗
  • 移动网站排名怎么做手机百度推广怎么打广告
  • 做网站卖电脑河北网站建设案例
  • 家政服家政服务网站模板网站关键词seo费用