在当今数字化时代,互联网上的数据资源丰富多样,其中动态网页和应用程序(App)中的图片数据尤为珍贵。这些图片可能用于数据分析、机器学习、内容推荐等多种场景。然而,由于许多 App 的图片加载是动态的,传统的爬虫方法往往难以直接获取。本文将介绍如何利用基于 Selenium 的 Python 爬虫技术来抓取动态 App 图片,详细阐述技术原理、实现步骤以及代码实现过程。
pip install selenium
)Pillow
,可选)假设我们要爬取某个图片社交App(如Instagram、Pinterest等)的公开图片,其特点包括:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os
# 设置ChromeDriver路径(根据实际情况修改)
driver_path = "chromedriver.exe" # 或指定绝对路径
service = Service(driver_path)
options = webdriver.ChromeOptions()
# 可选项:无头模式(不显示浏览器界面)
# options.add_argument("--headless")
# 初始化浏览器
driver = webdriver.Chrome(service=service, options=options)
dfrom selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# 设置代理信息
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"
# 配置代理
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = f"{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
proxy.ssl_proxy = f"{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
# 初始化 WebDriver,使用代理
options = webdriver.ChromeOptions()
options.proxy = proxy
driver = webdriver.Chrome(options=options)
def scroll_to_bottom(driver, max_scrolls=10, delay=2):
"""模拟滚动加载更多内容"""
last_height = driver.execute_script("return document.body.scrollHeight")
scroll_count = 0
while scroll_count < max_scrolls:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(delay) # 等待新内容加载
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break # 已到底部
last_height = new_height
scroll_count += 1
# 示例:访问 Pinterest(需替换为目标 App 的 URL)
url = "https://www.pinterest.com/search/pins/?q=cats"
try:
driver.get(url)
# 等待页面加载
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "img"))
)
# 模拟滚动加载更多图片
scroll_to_bottom(driver, max_scrolls=5)
except Exception as e:
print(f"加载网页时遇到问题:{e}")
print("请检查网页链接的合法性,确保网络连接正常。如果问题仍然存在,请稍后重试。")
finally:
driver.quit()
import requests
from PIL import Image
from io import BytesIO
def download_image(url, save_dir="images"):
"""下载图片并保存到本地"""
if not os.path.exists(save_dir):
os.makedirs(save_dir)
try:
response = requests.get(url, stream=True)
if response.status_code == 200:
img = Image.open(BytesIO(response.content))
img_name = url.split("/")[-1].split("?")[0] # 提取文件名
img_path = os.path.join(save_dir, img_name)
img.save(img_path)
print(f"下载成功: {img_path}")
except Exception as e:
print(f"下载失败: {url}, 错误: {e}")
# 获取所有图片元素
images = driver.find_elements(By.TAG_NAME, "img")
# 提取src并下载
for img in images:
img_url = img.get_attribute("src")
if img_url and "http" in img_url: # 过滤无效URL
download_image(img_url)
反爬虫机制
许多网站会设置反爬虫机制,如限制访问频率、检测用户代理等。在使用 Selenium 爬虫时,需要注意以下几点:
本文详细介绍了基于 Selenium 的 Python 爬虫技术抓取动态 App 图片的方法。通过模拟用户行为、提取图片 URL 和下载图片,我们成功实现了动态图片的抓取。Selenium 的强大功能使其能够应对复杂的动态网页环境,为数据采集提供了有力支持。然而,在实际应用中,我们还需要注意反爬虫机制和法律合规性,确保爬虫技术的合法、合理使用。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有