BeautifulSoup:无法获取所有图像src

问题描述 投票:1回答:1

我试图从网站获取每个图像,有时BeautifulSoup没有从HTML中获取每个src属性。

例:

data = requests.get('https://www.qmedichealth.com/')
soup = BeautifulSoup(data.text, 'html.parser')
img = soup.find_all('img')

代码很简单,但我无法获得此网站上滑块的网址,它适用于除下面的图像之外的每个图像:

<img alt="image description" style="width: 1583px; margin-left: 0px; height: 1055.33px; margin-top: -0.166667px;" src="https://cdn.shopify.com/s/files/1/0970/0888/t/3/assets/img07.jpg">

我真正得到的是:<img alt="image description"/>

对这种行为有什么想法吗?

python web-scraping beautifulsoup
1个回答
1
投票

检查源代码,你会看到没有src给出..因为它在运行时被渲染,所以像selenium这样的东西会很有用

from bs4 import BeautifulSoup
from selenium import webdriver

browser = webdriver.Chrome('path to chrome driver') 
download chrome driver here

http://chromedriver.chromium.org/downloads

browser.get('https://www.qmedichealth.com/')
data = BeautifulSoup(browser.page_source)

#All the Src
for src in data.find_all('img'):
    print(src['src'])
© www.soinside.com 2019 - 2024. All rights reserved.