【问题标题】:Is there any method to find img in divs有什么方法可以在div中找到img
【发布时间】:2023-01-17 15:00:36
【问题描述】:

以下代码我正在尝试获取 div 但我想要那个 div 中的 img

from typing import List

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome import options
from selenium.webdriver.common.keys import Keys
import time
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
`launch the browser`
driver = webdriver.Chrome(executable_path="/home/earth/Downloads/Python/chromedriver_linux64 (2)/chromedriver.exe")

driver.get("https://demo1.devmarketidiot.com/")`getting url`
driver.maximize_window()`maximize the window`
time.sleep(2)
html=driver.page_source`getting page source`
#file = html.read()
S= BeautifulSoup(html, 'lxml')``
print("........Divs...........")
mydivs=S.findAll('div')`find all divs present in that page source`
print(len(mydivs)) 
for i in mydivs:
    print("i:",i)

请给我找到 img 或那个 div 中的任何组件的解决方案`

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver


    【解决方案1】:

    你可以试试下面的代码,它会打印所有div标签中img标签的src属性的值:

    imgs = driver.find_elements(By.CSS_SELECTOR, "div img")
    
    for img in imgs:
        src = img.get_attribute("src")
        print(src)
    

    输出:

    data:image/svg+xml,%3csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20version=%271.1%27%20width=%27270%27%20height=%2758%27/%3e
    
    https://demo1.devmarketidiot.com/_next/image?url=%2F_next%2Fstatic%2Fmedia%2FMI-logo.e581cfae.png&w=640&q=75
    
    https://static.demo1.devmarketidiot.com/content_pages/85aa12e1-c2cd-4984-89a0-7981a2526bd41673622255509.jpg
    
    https://static.demo1.devmarketidiot.com/content_pages/fbbef982-4fc1-449c-91ee-0db3b6daa25f1673622653654.jpg
    

    【讨论】:

      猜你喜欢
      • 2014-12-27
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 2020-05-27
      • 1970-01-01
      • 2019-08-11
      • 2020-08-07
      • 2017-10-06
      相关资源
      最近更新 更多