我正在尝试从网站复制 href 值,html 代码如下所示:
I am trying to copy the href value from a website, and the html code looks like this:
<p class="sc-eYdvao kvdWiq">
<a href="https://www.iproperty.com.my/property/setia-eco-park/sale-
1653165/">Shah Alam Setia Eco Park, Setia Eco Park
</a>
</p>
我试过 driver.find_elements_by_css_selector(".sc-eYdvao.kvdWiq").get_attribute("href") 但它返回 'list' 对象没有属性 'get_attribute'代码>.使用 driver.find_element_by_css_selector(".sc-eYdvao.kvdWiq").get_attribute("href") 返回 None.但我不能使用 xpath,因为该网站有 20+ href 我需要全部复制.使用 xpath 只会复制一个.
I've tried driver.find_elements_by_css_selector(".sc-eYdvao.kvdWiq").get_attribute("href") but it returned 'list' object has no attribute 'get_attribute'. Using driver.find_element_by_css_selector(".sc-eYdvao.kvdWiq").get_attribute("href") returned None. But i cant use xpath because the website has like 20+ href which i need to copy all. Using xpath would only copy one.
如果有帮助,所有 20 多个 href 都归入同一类,即 sc-eYdvao kvdWiq.
If it helps, all the 20+ href are categorised under the same class which is sc-eYdvao kvdWiq.
最终我想复制所有 20+ href 并将它们导出到 csv 文件.
Ultimately i would want to copy all the 20+ href and export them out to a csv file.
感谢任何可能的帮助.
如果有多个元素,您需要 driver.find_elements.这将返回一个列表.对于 CSS 选择器,您要确保选择那些具有子 href 的类
You want driver.find_elements if more than one element. This will return a list. For the css selector you want to ensure you are selecting for those classes that have a child href
elems = driver.find_elements_by_css_selector(".sc-eYdvao.kvdWiq [href]")
links = [elem.get_attribute('href') for elem in elems]
您可能还需要一个等待条件来等待由 css 选择器定位的所有元素.
You might also need a wait condition for presence of all elements located by css selector.
elems = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".sc-eYdvao.kvdWiq [href]")))
这篇关于Python Selenium - 获取href值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在python中的感兴趣区域周围绘制一个矩形How to draw a rectangle around a region of interest in python(如何在python中的感兴趣区域周围绘制一个矩形)
如何使用 OpenCV 检测和跟踪人员?How can I detect and track people using OpenCV?(如何使用 OpenCV 检测和跟踪人员?)
如何在图像的多个矩形边界框中应用阈值?How to apply threshold within multiple rectangular bounding boxes in an image?(如何在图像的多个矩形边界框中应用阈值?)
如何下载 Coco Dataset 的特定部分?How can I download a specific part of Coco Dataset?(如何下载 Coco Dataset 的特定部分?)
根据文本方向检测图像方向角度Detect image orientation angle based on text direction(根据文本方向检测图像方向角度)
使用 Opencv 检测图像中矩形的中心和角度Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 检测图像中矩形的中心和角度)