栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

玩转 python selenium---抓取某知名电商商品页的图片

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

玩转 python selenium---抓取某知名电商商品页的图片

练下手,爬一下某电商网站上的商品图片,还真爬到了。代码如下:

4.28

from selenium import webdriver
import time

option = webdriver.ChromeOptions()
option.binary_location = r'C:Program FilesGoogleChromeApplicationchrome.exe'
option.add_argument("--disable-blink-features=AutomationControlled")
#option.add_argument('headless')	

driver = webdriver.Chrome(executable_path=r'C:Program FilesGoogleChromeApplicationchromedriver.exe', options=option)
url  = 'https://detail.1688.com/offer/669667221688.html?spm=a26352.13672862.offerlist.227.34b51e62ZfC32f'
driver.get(url)
time.sleep(1)

parent = driver.find_element_by_class_name('content-detail')
images = parent.find_elements_by_tag_name('img')
#print(images.size)

from urllib import request
import urllib

for image in images:
    #找到图片的网址
    img_url = image.get_attribute("data-lazyload-src")
    #print(img_url)
    #获取图片名
    name = img_url.split('/')[-1]
    #print(name)
    #将图片存到imagess文件夹中
    request.urlretrieve(img_url,f'imagess/{name}')

driver.quit()

完善了一下,可以把抓取的图片自动保存到新建的产品目录下,一个产品一个目录:

4.29

from selenium import webdriver
import time

def mkdir(path):
    # 引入模块
    import os
    # 去除首位空格
    path=path.strip()
    # 去除尾部  符号
    path=path.rstrip("\")

    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists=os.path.exists(path)

    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path) 
        print(path+' 创建成功')
        return True
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print (path+' 目录已存在')
        return False


option = webdriver.ChromeOptions()
option.binary_location = r'C:Program FilesGoogleChromeApplicationchrome.exe'
option.add_argument("--disable-blink-features=AutomationControlled")
#option.add_argument('headless')	

driver = webdriver.Chrome(executable_path=r'C:Program FilesGoogleChromeApplicationchromedriver.exe', options=option)
url  = 'https://detail.1688.com/offer/669667221688.html?spm=a26352.13672862.offerlist.227.34b51e62ZfC32f'
driver.get(url)
time.sleep(1)

parent = driver.find_element_by_class_name('content-detail')
images = parent.find_elements_by_tag_name('img')

title = driver.find_element_by_class_name('title-first-column')
title_text = title.find_element_by_class_name("title-text")
detail_dir = 'imagess/'+title_text.get_attribute('innerText')[-10:]+'/details'
mkdir(detail_dir)
headimg_dir = 'imagess/'+title_text.get_attribute('innerText')[-10:]+'/headimgs'
mkdir(headimg_dir)

from urllib import request
import urllib

for image in images:
    #找到图片的网址
    img_url = image.get_attribute("data-lazyload-src")
    print(img_url)
    name = img_url.split('/')[-1]
    print(name)
    #将图片存到images文件夹中
    request.urlretrieve(img_url,f'{detail_dir}/{name}')

detail_gallery_imgs = driver.find_elements_by_class_name('detail-gallery-img')

for image in detail_gallery_imgs:
    #找到图片的网址
    img_url = image.get_attribute("src")
    print(img_url)
    name = img_url.split('/')[-1]
    print(name)
    #将图片存到images文件夹中
    request.urlretrieve(img_url,f'{headimg_dir}/{name}')

driver.quit()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/849042.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号