使用 Selenium Python 从 #shadow-root 中提取信息
在网页抓取领域,从 #shadow 中隐藏的元素中提取数据- 根可能会带来重大挑战。本文探讨了使用 Selenium Python 克服此障碍的技术。
问题:
考虑 URL https://www.tiendasjumbo.co/buscar?q=来自在线商店的玛尼。为了从此网站提取产品标签和其他字段,用户尝试了以下方法:
<code class="python">from selenium import webdriver import time from random import randint driver = webdriver.Firefox(executable_path="C:\Program Files (x86)\geckodriver.exe") driver.implicitly_wait(10) time.sleep(4) url = "https://www.tiendasjumbo.co/buscar?q=mani" driver.maximize_window() driver.get(url) driver.find_element_by_xpath('//h1[@class="impulse-title"]')</code>
但是,这种方法失败了,切换 iframe 也同样不成功。
解决方案:
从该站点提取数据的关键在于识别产品位于#shadow-root 中。为了访问这些元素,Selenium 提供了shadowRoot.querySelector() 方法。使用此方法,可以使用以下定位器策略提取产品标签:
<code class="python">driver.get('https://www.tiendasjumbo.co/buscar?q=mani') item = driver.execute_script("return document.querySelector('impulse-search').shadowRoot.querySelector('div.group-name-brand h1.impulse-title span.formatted-text')") print(item.text)</code>
运行此脚本输出产品标签:
<code class="text">La especial mezcla de nueces, maní, almendras y marañones x 450 g</code>
参考:
有关更多见解,请参阅以下资源:
- 无法使用 Selenium 和 Python 在 #shadow-root (open) 中找到登录元素
- 如何使用 Selenium 和 Python 在 https://www.virustotal.com 网站内的shadow-root(开放)中找到名字字段
注意:
关于 Microsoft Edge 和 Google Chrome 版本 96,引入了对 Selenium 影子根返回值的更改。请参阅解决方案中提供的链接,了解有关在不同编程语言中解决这些更改的更多信息。
以上是如何使用 Selenium Python 从 #shadow-roots 中提取隐藏信息?的详细内容。更多信息请关注PHP中文网其他相关文章!

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供应模拟性和可读性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境