当我打电话使用soup.select('img[data-image-large]')我不能让所有的图像使用美丽的汤和硒

当我打电话使用<code>soup.select('img[data-image-large]')</code>我不能让所有的图像使用美丽的汤和硒

问题描述:

属性我真的不知道如何从data-image-large得到公正的src图像我得到这个:当我打电话使用<code>soup.select('img[data-image-large]')</code>我不能让所有的图像使用美丽的汤和硒

[<img alt="Adidas Originals NMD C2" class="img-responsive" data-image-large="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fbb%2Fc2%2FShoes_1_1-a1b3aed478ffec80ba8c68494053.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=1300&amp;ixlib=php-1.1.0&amp;q=90&amp;w=1000&amp;s=38d17241f278aa174c57ff8ba35b94da" data-index="0" data-zoom-image="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fbb%2Fc2%2FShoes_1_1-a1b3aed478ffec80ba8c68494053.jpg?auto=compress%2Cformat&amp;fm=pjpg&amp;ixlib=php-1.1.0&amp;q=90&amp;s=963ec1279be64e03f4ac2b7f7a9b36d4" itemprop="image" src="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fbb%2Fc2%2FShoes_1_1-a1b3aed478ffec80ba8c68494053.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=832&amp;ixlib=php-1.1.0&amp;q=90&amp;w=640&amp;s=92dea597a56bcf9d38f1f9a5f4d3c590" srcset="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fbb%2Fc2%2FShoes_1_1-a1b3aed478ffec80ba8c68494053.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=1300&amp;ixlib=php-1.1.0&amp;q=90&amp;w=1000&amp;s=38d17241f278aa174c57ff8ba35b94da 2x"/>, <img alt="Adidas Originals NMD C2" class="img-responsive" data-image-large="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2F4e%2F44%2FShoes_1_2-11c2134891612312561d9d8e93f5.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=1300&amp;ixlib=php-1.1.0&amp;q=90&amp;w=1000&amp;s=27df63d14cf5183ccc3d825f6994a232" data-index="1" data-zoom-image="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2F4e%2F44%2FShoes_1_2-11c2134891612312561d9d8e93f5.jpg?auto=compress%2Cformat&amp;fm=pjpg&amp;ixlib=php-1.1.0&amp;q=90&amp;s=20dbba1f381441b6cf1654b8bb03a194" src="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2F4e%2F44%2FShoes_1_2-11c2134891612312561d9d8e93f5.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=832&amp;ixlib=php-1.1.0&amp;q=90&amp;w=640&amp;s=4fb51fcf7948eeb5622fb63c25d6944f" srcset="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2F4e%2F44%2FShoes_1_2-11c2134891612312561d9d8e93f5.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=1300&amp;ixlib=php-1.1.0&amp;q=90&amp;w=1000&amp;s=27df63d14cf5183ccc3d825f6994a232 2x"/>, <img alt="Adidas Originals NMD C2" class="img-responsive" data-image-large="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fa6%2Ff4%2FShoes_1_3-f30adb388e477e7fd0a7bc2d172a.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=1300&amp;ixlib=php-1.1.0&amp;q=90&amp;w=1000&amp;s=505e637d9b5a82bcf0d84a9d335e080c" data-index="2" data-zoom-image="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fa6%2Ff4%2FShoes_1_3-f30adb388e477e7fd0a7bc2d172a.jpg?auto=compress%2Cformat&amp;fm=pjpg&amp;ixlib=php-1.1.0&amp;q=90&amp;s=fb0019b2304ab3f970a03f8ec7faf7fb" src="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fa6%2Ff4%2FShoes_1_3-f30adb388e477e7fd0a7bc2d172a.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=832&amp;ixlib=php-1.1.0&amp;q=90&amp;w=640&amp;s=44ac890d49273e9d382f41e97ec70f3d" srcset="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fa6%2Ff4%2FShoes_1_3-f30adb388e477e7fd0a7bc2d172a.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=1300&amp;ixlib=php-1.1.0&amp;q=90&amp;w=1000&amp;s=505e637d9b5a82bcf0d84a9d335e080c 2x"/>, <img alt="Adidas Originals NMD C2" class="img-responsive" data-image-large="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fef%2F15%2FShoes_1_4-711b1e1627aaeb93e4b60395cc4a.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=1300&amp;ixlib=php-1.1.0&amp;q=90&amp;w=1000&amp;s=335ad8e02f38cf1315fd8d77bb25e773" data-index="3" data-zoom-image="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fef%2F15%2FShoes_1_4-711b1e1627aaeb93e4b60395cc4a.jpg?auto=compress%2Cformat&amp;fm=pjpg&amp;ixlib=php-1.1.0&amp;q=90&amp;s=a1a5b31af43c8ea5e125243dcebc9674" src="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fef%2F15%2FShoes_1_4-711b1e1627aaeb93e4b60395cc4a.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=832&amp;ixlib=php-1.1.0&amp;q=90&amp;w=640&amp;s=ef9389f17a6557b4932a55312eda61a1" srcset="https://hypebeast.imgix.net/http%3A%2F%2Fs3.store.hypebeast.com%2Fmedia%2Fimage%2Fef%2F15%2FShoes_1_4-711b1e1627aaeb93e4b60395cc4a.jpg?auto=compress%2Cformat&amp;fit=max&amp;fm=pjpg&amp;h=1300&amp;ixlib=php-1.1.0&amp;q=90&amp;w=1000&amp;s=335ad8e02f38cf1315fd8d77bb25e773 2x"/>] 

我知道如何使用xpath得到1,然后使用这个命令:driver.find_element_by_xpath('//*[@id="gallery"]/div/div[2]/div/div/ul/li[1]/img但是,我也不知道如何使用硒来获取它们。

我的脚本:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import requests ; from bs4 import BeautifulSoup 
driver = webdriver.Chrome() 
from selenium.common.exceptions import NoSuchElementException 

#url target 
driver.get("https://hbx.com/categories/sneakers") 

#masuk 
driver.find_element_by_xpath('//*[@id="products-container"]/div[1]').click() 
url = driver.current_url 
soup = BeautifulSoup(requests.get(url).html, "html.parser") 
soup.select('img[data-image-large]') 

#color 
def check_exists_by_xpath(xpath): 
try: 
    driver.find_element_by_xpath('//*[@id="product-form"]/div[1]/div[1]/div/select').text 
except: NoSuchElementException 

#size 
def check_exists_by_xpath(xpath): 
    try: 
     driver.find_element_by_xpath('//*[@id="product-form"]/div[1]/div[2]/div[2]').text 
    except: NoSuchElementException 

#back 
driver.back() 

我想就是从这里https://hbx.com/brands/adidas-originals/nmd-c2

+1

如果你可以直接从浏览器实例中打开的页面中抓取所有需要的数据,为什么要做'requests.get(url)'? – Andersson

+0

请将单独的问题作为实际单独的问题发布。这并没有什么坏处,因为你只是发帖,每个问题有一个问题。 – Iguananaut

+0

@Andersson我以为只是开着硒而不能检测为人类。没有它就没关系了? – Filmar

得到所有data-image-large图像源的。如果你只是想凑所有图像源,你可以试试下面的代码:

driver.get(https://hbx.com/brands/adidas-originals/nmd-c2) 
pictures = [picture.get_attribute('data-image-large') for picture in driver.find_elements_by_css_selector('div#gallery li>img.img-responsive')] 

print(pictures) 
+0

对不起。我不清楚我以前想要什么。我会再次编辑我的帖子。在我使用这个命令'driver.find_element_by_xpath('// * [@ id =“products-container”]/div [1]')后,点击()'让我的浏览器进入:'https://hbx.com/brands/adidas-originals/nmd-c2' – Filmar

+0

是的。那就对了。我也是这样做的,但没有点击每个图像,但用'get()'一个接一个地打开它。 – Andersson

+0

感谢您的尝试!真的非常喜欢!我会等待,也许有人可以得到他们所有人。对于临时只是让他们一个接一个。 – Filmar