【发布时间】:2019-01-10 15:29:07
【问题描述】:
我想从丝芙兰网站上抓取评论。评论是动态更新的。
经过检查,我发现评论在 HTML 代码中。
<div class="css-eq4i08 " data-comp="Ellipsis Box">Honestly I never write
reviews but this is a must if you have frizzy after even after straightening
it! It smells fantastic and it works wonders definitely will be restocking once
I’m done this one !!</div>
我想写一个 python selenium 代码来阅读评论。
我写的代码在这里……
from selenium import webdriver
chrome_path = (r"C:/Users/Connectm/Downloads/chromedriver.exe")
driver = webdriver.Chrome(chrome_path)
driver.implicitly_wait(20)
driver.get("https://www.sephora.com/product/crybaby-coconut-oil-shine-serum-P439093?skuId=2122083&icid2=just%20arrived:p439093")
reviews = driver.find_element_by_xpath('//*[@id="ratings-reviews"]/div[4]/div[2]/div[2]/div[1]/div[3][@data-comp()='Elipsis Box'])
print(reviews.text)
如果我写 find_element_by_class 它会给我空白。
什么是最好的选择?
我正在尝试使用带有属性的 xpath。代码不起作用。 有人请帮助我最好的解决方案是什么?
【问题讨论】:
-
有人有建议吗?
-
@data-comp()应该是@data-comp... 没有括号。 -
@JeffC 在我进行更改后不起作用。请指导我如何访问上述评论?任何人都可以指导我的路径吗?
-
仅仅将类作为路径是行不通的..
-
这里的问题是,当您在页面上向下滚动时会加载评论。只需将页面向下滚动到评论所在的位置,明确等待评论加载到页面上,然后调用
find_element_by_xpath即可获得评论文本。此外,您问题中的 xpath 有问题。//div[@id='ratings-reviews']//div[@data-comp='Ellipsis Box']应该可以解决问题。它的Ellipsis带有 2 个“l”
标签: javascript python selenium-webdriver xpath webdriverwait