如果存在子字符串,如何从列表中获取字符串

时间:2019-07-26 07:49:11

标签: python selenium

由于它们具有更高的分辨率,因此我试图在单击产品时获取它们的图像。到目前为止,我有以下代码:

start = soup(d.page_source, 'html.parser') # d is the driver
while start.find('div', {'class': 'gallery-images'}) is None:
            start = soup(d.page_source, 'html.parser')

        product_images = [i.find('img', {'alt': 'Mirror Embellished Scuba Skirt'}).src for i in
                    start.find_all('div', {'class': 'gallery-images'})]

但是,它说nonetype没有对象src。 start.find_all返回所有级联在其中的元素的列表。

编辑:网页: https://www.michaelkors.com/mirror-embellished-scuba-skirt/_/R-US_MU97EYCBGL

编辑2: 我有alt的硬编码值。 Page scrapy调用没有具有此特定值的alt。但是,即使我删除了alt,它也没有返回任何内容

1 个答案:

答案 0 :(得分:1)

这是代码。它返回我src个值。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup

driver = webdriver.Chrome()
driver.get("https://www.michaelkors.co.uk/")
search_ele=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.search-link[title="Search"]')))
search_ele.click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'input#search-box'))).send_keys('mirror-embellished-scuba-skirt')
element=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'button.search-icon-btn')))
driver.execute_script("arguments[0].click();", element)
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'a[title="Mirror Embellished Scuba Skirt"]'))).click()
time.sleep(3)
soup=BeautifulSoup(driver.page_source,'html.parser')
product_images=[]
for i in soup.find_all('div', class_='gallery-images'):
    for img in i.select('img[alt="Mirror Embellished Scuba Skirt"]'):
        product_images.append(img['src'])

print(product_images)

输出:

['//michaelkors.scene7.com/is/image/MichaelKors/MU97EYCBGL-0001_1?wid=558&hei=748&op_sharpen=1&resMode=sharp2&qlt=90', '//michaelkors.scene7.com/is/image/MichaelKors/MU97EYCBGL-0001_2?wid=558&hei=748&op_sharpen=1&resMode=sharp2&qlt=90', '//michaelkors.scene7.com/is/image/MichaelKors/MU97EYCBGL-0001_3?wid=558&hei=748&op_sharpen=1&resMode=sharp2&qlt=90', '//michaelkors.scene7.com/is/image/MichaelKors/MU97EYCBGL-0001_1?wid=1300', '//michaelkors.scene7.com/is/image/MichaelKors/MU97EYCBGL-0001_2?wid=1300', '//michaelkors.scene7.com/is/image/MichaelKors/MU97EYCBGL-0001_3?wid=1300']