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

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

4.28

from selenium import webdriver
import time

option = webdriver.ChromeOptions()
option.binary_location = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
option.add_argument("--disable-blink-features=AutomationControlled")
#option.add_argument('headless')	

driver = webdriver.Chrome(executable_path=r'C:\Program Files\Google\Chrome\Application\chromedriver.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 Files\Google\Chrome\Application\chrome.exe'
option.add_argument("--disable-blink-features=AutomationControlled")
#option.add_argument('headless')	

driver = webdriver.Chrome(executable_path=r'C:\Program Files\Google\Chrome\Application\chromedriver.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()

取 脚本执行下面的命令,项目目录下会创建产品目录,所有的取到的商品图片和信息总体出现在里面。 python crawl.py supreme https://www.supremecommunity.com/season/spring-summer2020/droplist/2020-02-27/ 其他依赖 为了使用正确使用nike虫,你还需要: Chrome浏览器(chrome 85版) ChromeDriver 85.0.4183.87 缺失它们不会影响其他虫的使用。 配置问题 通过修改IMAGES_STORE可以自定义文件的存储位置。 默认开启了AUTOTHROTTLE,可以通过设置AUTOTHROTTLE_ENABLED为False关闭。 基本使用 项目下执行命令: python crawl.py brand start_url... 把brand替换为品牌名。 把start_url替换为要开始取的网虫 最高 取某一季所有周的商品 python crawl.py supreme https://www.supremecommunity.com/season/spring-summer2020/droplists/ 浏览某一周所有的商品 python crawl.py supreme https://www.supremecommunity.com/season/spring-summer2020/droplist/2020-02-27/ 游走取一些周的商品 python crawl.py supreme https://www.supremecommunity.com/season/spring-summer2020/droplist/2020-02-27/ https://www.supremecommunity.com/season/spring-summer2020/droplist/2020-05-21/ 资本 去取某一特定下的所有商品 python crawl.py kapital https://www.kapital-webshop.jp/category/W_COAT/ 耐克 取当前搜索的商品(包括所有颜色) python crawl.py nike https://www.nike.com/cn/w?q=CU6525&vst=CU6525 熊砖 去取当前分类的所有商品 python crawl.py bearbrick http://www.bearbrick.com/product/12_0 已知问题:BearBrickLoader 的category_in无法达到预期的行为。 United Arrows 网上商店 取当前商品 python crawl.py uastore https://store.united-arrows.co.jp/shop/mt/goods.html?gid=52711245 特拉维斯·斯科特 取所有商品 python crawl.py ts https://shop.travisscott.com/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值