item_search_shop - 获得店铺的所有商品

本文主要介绍了如何使用API`item_search_shop`来获取特定店铺的所有商品信息,包括测试网址的提供,便于读者进行实际操作和测试。

item_search_shop - 获得店铺的所有商品

在这里插入图片描述

测试网址:点击注册联系客服

Result Object:
---------------------------------------
{
	"user": null,
	"items": {
		"item": [
			{
				"num_iid": 5631858520,
				"pic_url": "https://cf.shopee.com.my/file/05db4b1f181a50e1ae54e803955f04cd",
				"title": "🌈Korean House🌸Spring Autumn Korean Long-sleeve baju T-shirt Lengan Panjang Women Clothes Blouse Loose Clothing Tops Murah",
				"orginal_price": 27.8,
				"price": 12.9,
				"sales": 21133,
				"detail_url": "https://shopee.com.my/product/239487269/5631858520"
			},
			{
				"num_iid": 5622785570,
				"pic_url": "https://cf.shopee.com.my/file/d9d9eca414f7567f440b933768d2e8aa",
				"title": "🌈Korean House🌸Ready Stock Women Clothes and Clothing Korean Cartoon Blouse Printing Plus Size Loose Short Sleeve baju T-shirt/ Tees MURAH",
				"orginal_price": 25.8,
				"price": 12.9,
				"sales": 9633,
				"detail_url": "https://shopee.com.my/product/239487269/5622785570"
			},
			{
				"num_iid": 5422770275,
				"pic_url": "https://cf.shopee.com.my/file/741082d6c1976b940350d882d7f0e002",
				"title": "🌈Korean House🌸  Women's Blouse Summer Mickey Mouse Short-sleeve baju T-shirt Fashion Clothing Round Neck Student Tees/ Clothes MURAH",
				"orginal_price": 25.8,
				"price": 12.9,
				"sales": 7000,
				"detail_url": "https://shopee.com.my/product/239487269/5422770275"
			},
			{
				"num_iid": 4622788836,
				"pic_url": "https://cf.shopee.com.my/file/6582b77fdc24b68645edadbf19a3f852",
				"title": "🌈Korean House🌸Summer Blouse Clothing Short-sleeved baju T-shirt / Tees Loose Student Casual Shirt Tide Women MURAH Mickey Mouse Clothes",
				"orginal_price": 23.8,
				"price": 11.9,
				"sales": 5246,
				"detail_url": "https://shopee.com.my/product/239487269/4622788836"
			},
			{
				"num_iid": 6435266865,
				"pic_url": "https://cf.shopee.com.my/file/ec9c8c46d7d4fa5d7cfa9ef5f3202886",
				"title": "🌈Korean House🌸Spring and summer women's baju Lengan panjang t-shirt Tops clothing autumn clothes long sleeve blouse murah",
				"orginal_price": 29.8,
				"price": 14.9,
				"sales": 5563,
				"detail_url": "https://shopee.com.my/product/239487269/6435266865"
			},
			{
				"num_iid": 4233174051,
				"pic_url": "https://cf.shopee.com.my/file/9b33203e9c0339ef5cb632cfe3dd761a",
				"title": "🌈Korean House🌸 🚀Random Pattern POS 🚀  Loose Short sleeve T-shirt Women Blouse/Clothes/Baju",
				"orginal_price": 15.6,
				"price": 9.9,
				"sales": 1534,
				"detail_url": "https://shopee.com.my/product/239487269/4233174051"
			},
			{
				"num_iid": 3827977695,
				"pic_url": "https://cf.shopee.com.my/file/fc85205fc50f1235fd96039ff3a0dfec",
				"title": "🌈Korean House🌸2020 Summer Tops and Blouse New Korean loose short sleeve baju t shirt women murah clothes",
				"orginal_price": 25.8,
				"price": 12.9,
				"sales": 2374,
				"detail_url": "https://shopee.com.my/product/239487269/3827977695"
			},
			{
				"num_iid": 4332323170,
				"pic_url": "https://cf.shopee.com.my/file/fd7ba8f5ec61b483ab9fadff07d2b2b4",
				"title": "🌈Korean House🌸Rainbow short-sleeve t-shirt Women loose large size blouse murah baju clothes",
				"orginal_price": 27.8,
				"price": 13.9,
				"sales": 2789,
				"detail_url": "https://shopee.com.my/product/239487269/4332323170"
			},
			{
				"num_iid": 5332996121,
				"pic_url": "https://cf.shopee.com.my/file/21aa11def8d530ad4942d453c89cf74d",
				"title": "🌈Korean House🌸Pajamas/Pyjamas Women Summer Cotton Nightwear Short-sleeve Clothes Two-piece Suits Sleepwear",
				"orginal_price": 39.8,
				"price": 22.9,
				"sales": 1674,
				"detail_url": "https://shopee.com.my/product/239487269/5332996121"
			},
			{
				"num_iid": 7332409231,
				"pic_url": "https://cf.shopee.com.my/file/ec30b3dcff0e7b4ce6ad0e64f28e89a2",
				"title": "🌈Korean House🌸Tops New Korean loose Clothes short-sleeve Women t-shirt baju clothing blouse",
				"orginal_price": 27.8,
				"price": 13.9,
				"sales": 2424,
				"detail_url": "https://shopee.com.my/product/239487269/7332409231"
			},
			{
				"num_iid": 7428456381,
				"pic_url": "https://cf.shopee.com.my/file/9d93181d6ddbe596d9fc066e674a0aa6",
				"title": "🌈Korean House🌸Short-sleeve t-shirt women Clothes 2020 spring new Korean Tops loose print blouse round neck baju",
				"orginal_price": 23.8,
				"price": 12.9,
				"sales": 1611,
				"detail_url": "https://shopee.com.my/product/239487269/7428456381"
			},
			{
				"num_iid": 7722779532,
				"pic_url": "https://cf.shopee.com.my/file/1d2a4309358733a0187f8c4f148603e8",
				"title": "🌈Korean House🌸Summer Korean Clothing Round Neck Loose White Women 100% cotton Blouse Short-sleeved baju T-shirt/ Tees Student Tops MURAH Clothes",
				"orginal_price": 23.8,
				"price": 12.9,
				"sales": 1467,
				"detail_url": "https://shopee.com.my/product/239487269/7722779532"
			},
			{
				"num_iid": 5922796625,
				"pic_url": "https://cf.shopee.com.my/file/25580d4aa3298b1a698b45f8656cf385",
				"title": "🌈Korean House🌸Small Daisy Short-sleeved baju T-shirt Women Blouse 2020 Summer Korean Version of Loose White ins Tide Tops Students MURAH",
				"orginal_price": 23.8,
				"price": 12.9,
				"sales": 1499,
				"detail_url": "https://shopee.com.my/product/239487269/5922796625"
			},
			{
				"num_iid": 5925333330,
				"pic_url": "https://cf.shopee.com.my/file/243023959956ce0b8e2ee793f576aed9",
				"title": "🌸Korean House🌸White short-sleeved t-shirt Women Blouse 2020 new Korean MURAH loose baju",
				"orginal_price": 25.8,
				"price": 12.9,
				"sales": 1231,
				"detail_url": "https://shopee.com.my/product/239487269/5925333330"
			},
			{
				"num_iid": 7542409673,
				"pic_url": "https://cf.shopee.com.my/file/07d6795bc3592a927c19074db86ee503",
				"title": "🌈Korean House🌸 Plus size women clothes 2020 autumn clothing loose cartoon printing blouse bottoming shirt female round neck long sleeve T-shirt tops Baju Murah",
				"orginal_price": 29.8,
				"price": 13.9,
				"sales": 852,
				"detail_url": "https://shopee.com.my/product/239487269/7542409673"
			},
			{
				"num_iid": 6531861829,
				"pic_url": "https://cf.shopee.com.my/file/8ec730a44cdd7c0f70d64e2e8086fdc1",
				"title": "🌈Korean House🌸2020 Spring Autumn New Clothing Long-sleeve T-shirt Women Clothes Blouse Korean style Lengan panjang White Loose baju Tops",
				"orginal_price": 27.8,
				"price": 13.9,
				"sales": 1029,
				"detail_url": "https://shopee.com.my/product/239487269/6531861829"
			},
			{
				"num_iid": 6731861441,
				"pic_url": "https://cf.shopee.com.my/file/7cb1cb82f3787af3b40fe5282c41143e",
				"title": "🌈Korean House🌸2020 New Korean Harajuku Style Women Clothes Blouse Letter Loose Clothing Long-sleeved T-shirt Tops",
				"orginal_price": 27.8,
				"price": 13.9,
				"sales": 571,
				"detail_url": "https://shopee.com.my/product/239487269/6731861441"
			},
			{
				"num_iid": 7622768930,
				"pic_url": "https://cf.shopee.com.my/file/215a09a36e742184014f464aef241bd0",
				"title": "🌈Korean House🌸Summer Women Blouse Cartoon Print Loose Short Fashion of Clothes and Clothing and Half Sleeve Harajuku Style  baju T-shirt MURAH",
				"orginal_price": 25.8,
				"price": 12.9,
				"sales": 1397,
				"detail_url": "https://shopee.com.my/product/239487269/7622768930"
			},
			{
				"num_iid": 7226757909,
				"pic_url": "https://cf.shopee.com.my/file/b663e5e77a6325c4b9114bee1f218051",
				"title": "🌈Korean House🌸Short-sleeved t-shirt female Korean version loose 2020 student wild summer little girl printed half-sleeved shirt tide was thin",
				"orginal_price": 23.8,
				"price": 12.9,
				"sales": 1389,
				"detail_url": "https://shopee.com.my/product/239487269/7226757909"
			},
			{
				"num_iid": 6826750502,
				"pic_url": "https://cf.shopee.com.my/file/5e13af85e2e64682c452dec1874518ca",
				"title": "🌈Korean House🌸Mid-length t-shirt women short-sleeve spring and summer 2020 baju Tees MURAH plus size Blouse clothes",
				"orginal_price": 23.8,
				"price": 12.9,
				"sales": 1366,
				"detail_url": "https://shopee.com.my/product/239487269/6826750502"
			},
			{
				"num_iid": 3233050444,
				"pic_url": "https://cf.shopee.com.my/file/b3511311f8295d48e79e1e369e1869ab",
				"title": "🌈Korean House🌸Short-sleeve summer Blouse Pajamas/Pyjamas and Sleepwear/Nightdress Women cute Korean loose student home clothing Nightwear/Pregnant Dress",
				"orginal_price": 33.8,
				"price": 16.9,
				"sales": 790,
				"detail_url": "https://shopee.com.my/product/239487269/3233050444"
			},
			{
				"num_iid": 3426855352,
				"pic_url": "https://cf.shopee.com.my/file/b324c114a40c39b40bf22a1e9e9e662b",
				"title": "🌈Korean House🌸Korean large plus size women's baju t-shirt MURAH loose mid-length 2020 summer Korean Blouse clothes",
				"orginal_price": 23.8,
				"price": 12.9,
				"sales": 945,
				"detail_url": "https://shopee.com.my/product/239487269/3426855352"
			},
			{
				"num_iid": 4831990761,
				"pic_url": "https://cf.shopee.com.my/file/06e7d51eedaa7b94f98b3701dbc7d3a5",
				"title": "🌈Korean House🌸M-3XL Maternity Blouse Baju Clothes Nursing Dress /Nightwear Long Pajamas/Pyjamas Women's Sleepwear/Pregnant Dress",
				"orginal_price": 31.8,
				"price": 17.9,
				"sales": 696,
				"detail_url": "https://shopee.com.my/product/239487269/4831990761"
			},
			{
				"num_iid": 3731161052,
				"pic_url": "https://cf.shopee.com.my/file/11b733c4b6850716f66cf21d892feb70",
				"title": "🌈Korean House🌸M-3XL Nightdress Women Pajamas/Pyjamas fresh students loose sleepwear Korean Nightwear/Pregnant Dress",
				"orginal_price": 31.8,
				"price": 17.9,
				"sales": 632,
				"detail_url": "https://shopee.com.my/product/239487269/3731161052"
			},
			{
				"num_iid": 7630564678,
				"pic_url": "https://cf.shopee.com.my/file/ef16ad530d7133d9739f300145927a3e",
				"title": "🌈Korean House🌸Summer New Korean Loose Clothing Short-sleeve T-shirt Women Clothes Blouse baju Tops",
				"orginal_price": 23.8,
				"price": 12.9,
				"sales": 887,
				"detail_url": "https://shopee.com.my/product/239487269/7630564678"
			},
			{
				"num_iid": 6930542131,
				"pic_url": "https://cf.shopee.com.my/file/820c5a39b477d8be860d937a23b38873",
				"title": "🌈Korean House🌸New Korean Women Clothes Long-sleeve T-shirt Clothing Blouse Lengan panjang Loose baju Tops",
				"orginal_price": 25.8,
				"price": 9,
				"sales": 1403,
				"detail_url": "https://shopee.com.my/product/239487269/6930542131"
			},
			{
				"num_iid": 5429660673,
				"pic_url": "https://cf.shopee.com.my/file/37a78d8250c66af54000742225fd4a7b",
				"title": "🌈Korean House🌸100%Cotton  Oversize Korean Round Neck Women Clothes Short-sleeve T-shirt Blouse 2020 New Summer White Clothing tops",
				"orginal_price": 23.8,
				"price": 12.9,
				"sales": 1140,
				"detail_url": "https://shopee.com.my/product/239487269/5429660673"
			},
			{
				"num_iid": 3430238236,
				"pic_url": "https://cf.shopee.com.my/file/a07b6783f94de2c9d64b9bb892faae21",
				"title": "🌈Korean House🌸Summer Korean Loose Clothing Tie-dye Small Daisy Women Clothes Short-sleeved T-shirt Blouse New Wild Tide Tops",
				"orginal_price": 25.8,
				"price": 12.9,
				"sales": 574,
				"detail_url": "https://shopee.com.my/product/239487269/3430238236"
			},
			{
				"num_iid": 6030577362,
				"pic_url": "https://cf.shopee.com.my/file/1cec3b0b539c0fcdd1594d7190f79944",
				"title": "🌈Korean House🌸Short-sleeved T-shirt Women Clothes Blouse 2020 Summer New Korean Tie-dye Harajuku Style Loose Clothing Tide Tops",
				"orginal_price": 25.8,
				"price": 12.9,
				"sales": 500,
				"detail_url": "https://shopee.com.my/product/239487269/6030577362"
			},
			{
				"num_iid": 7543462621,
				"pic_url": "https://cf.shopee.com.my/file/e2f07acf012c2537cad7a38d7bc3377a",
				"title": "🌈Korean House🌸 2020 new Mickey Mouse Hoodie women clothes  loose sweater tops blouse outerwear sweater Baju Murah",
				"orginal_price": 39.8,
				"price": 23.88,
				"sales": 413,
				"detail_url": "https://shopee.com.my/product/239487269/7543462621"
			}
		],
		"total_results": 1611,
		"page_size": 30,
		"pagecount": 54,
		"page": "1",
		"url": "https://shopee.com.my/shop/239487269/search"
	},
	"error": "",
	"secache": "ae216083d8450ea408fa84859a4135fd",
	"secache_time": 1599614783,
	"secache_date": "2020-09-09 09:26:23",
	"translate_status": "",
	"translate_time": 0,
	"language": {
		"default_lang": "cn",
		"current_lang": "cn"
	},
	"reason": "",
	"error_code": "0000",
	"cache": 0,
	"api_info": "today: max:",
	"execution_time": 6.472,
	"server_time": "Beijing/2020-09-09 09:26:23",
	"call_args": {
		"nick": "239487269"
	},
	"api_type": "shopee",
	"translate_language": "zh-CN",
	"translate_engine": "baidu",
	"server_memory": "2.72MB",
	"request_id": "15f582f3922f05"
}
import time import random import json import pandas as pd from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import logging # 配置日志 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) class TaobaoCrawler: def __init__(self): """初始化游客模式爬虫""" self.driver = None self.products_data = [] self.setup_browser() def setup_browser(self): """设置浏览器配置""" options = webdriver.ChromeOptions() # 反检测配置 options.add_argument('--disable-blink-features=AutomationControlled') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--disable-extensions') options.add_argument( '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0') # 隐藏自动化特征 options.add_experimental_option('excludeSwitches', ['enable-automation']) options.add_experimental_option('useAutomationExtension', False) # 可选:无头模式(生产环境使用) # options.add_argument('--headless') try: # 直接使用系统 ChromeDriver,不通过 webdriver_manager self.driver = webdriver.Chrome(options=options) # 执行脚本来隐藏webdriver特征 self.driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") self.driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', { 'source': ''' Object.defineProperty(navigator, 'webdriver', {get: () => undefined}) Object.defineProperty(navigator, 'languages', {get: () => ['zh-CN', 'zh']}) ''' }) logger.info("浏览器初始化成功 - 游客模式") except Exception as e: logger.error(f"浏览器初始化失败: {str(e)}") raise def search_products(self, keyword, pages=3, sort_by="sale-desc"): """搜索商品并爬取多页数据 - 改进版""" try: # 构建搜索URL,添加排序参数 search_url = f"https://s.taobao.com/search?q={keyword}&sort={sort_by}" self.driver.get(search_url) logger.info(f"搜索关键词: {keyword}, 排序方式: {sort_by}") # 等待搜索结果加载 - 使用更稳定的选择器 WebDriverWait(self.driver, 15).until( EC.presence_of_element_located((By.CSS_SELECTOR, ".CardV2--doubleCard--_OJ1T8j")) ) # 爬取多页数据 for page in range(1, pages + 1): logger.info(f"正在爬取第 {page} 页") self._scrape_current_page() # 如果不是最后一页,尝试翻页 if page < pages: if not self._go_to_next_page(): logger.warning("无法翻页,停止爬取") break # 随机延迟避免被封 time.sleep(random.uniform(3, 7)) return True except Exception as e: logger.error(f"搜索商品失败: {str(e)}") return False def _go_to_next_page(self): """翻页到下一页""" try: # 尝试多种翻页方式 next_selectors = [ ".next-btn.next-next", # 淘宝的下一页按钮 ".next-btn", "button[aria-label*='下一页']", ".next-btn[aria-label*='下一页']" ] for selector in next_selectors: try: next_btn = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, selector)) ) self.driver.execute_script("arguments[0].click();", next_btn) # 等待新页面加载 WebDriverWait(self.driver, 15).until( EC.presence_of_element_located((By.CSS_SELECTOR, ".CardV2--doubleCard--_OJ1T8j")) ) logger.info("成功翻页到下一页") return True except: continue return False except Exception as e: logger.error(f"翻页失败: {str(e)}") return False def _scrape_current_page(self): """爬取当前页面的商品数据 - 改进版""" try: # 使用实际的选择器 WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, ".CardV2--doubleCard--_OJ1T8j")) ) # 获取商品元素 items = self.driver.find_elements(By.CSS_SELECTOR, ".CardV2--doubleCard--_OJ1T8j") if not items: # 备用选择器 items = self.driver.find_elements(By.CSS_SELECTOR, ".CardV2--mainPicAndDesc--Eb_BxDU") logger.info(f"找到 {len(items)} 个商品") successful_count = 0 for index, item in enumerate(items): try: # 滚动到元素位置 self.driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", item) time.sleep(0.5) product_data = self._extract_product_data(item) if product_data: self.products_data.append(product_data) successful_count += 1 logger.info(f"成功提取商品 {successful_count}: {product_data['title'][:20]}...") # 每5个商品休息一下 if (index + 1) % 5 == 0: time.sleep(random.uniform(1, 3)) except Exception as e: logger.error(f"提取第{index + 1}个商品失败: {str(e)}") continue logger.info(f"当前页面爬取完成,成功获取 {successful_count}/{len(items)} 个商品") except Exception as e: logger.error(f"爬取当前页面失败: {str(e)}") def _extract_product_data(self, item): """从商品元素中提取数据 - 改进版""" try: # 提取基本信息 - 使用实际的选择器 title = self._safe_find_text(item, ".Title--title--wJY8TeA span") # 提取价格(需要组合整数和小数) price_int = self._safe_find_text(item, ".Price--priceInt--BXYeCOI") price_float = self._safe_find_text(item, ".Price--priceFloat--rI_BYho") price = f"{price_int}{price_float}" if price_int else "未知" # 提取销量信息 sales = self._safe_find_text(item, ".Price--realSales--wnhaqVr") # 提取店铺信息 shop_name = self._safe_find_text(item, ".ShopInfo--shopNameText--kxQC2cC") # 提取发货地 location = self._safe_find_text(item, ".Price--procity--Na1DQVe") # 提取商品链接 try: link_element = item.find_element(By.CSS_SELECTOR, ".Title--title--wJY8TeA a") product_url = link_element.get_attribute("href") except: product_url = "" # 构建商品数据 product_data = { "title": title or "未知", "price": price or "未知", "shop_name": shop_name or "未知", "sales": sales or "0", "location": location or "未知", "url": product_url, "crawl_time": time.strftime("%Y-%m-%d %H:%M:%S") } return product_data except Exception as e: logger.error(f"提取商品数据失败: {str(e)}") return None def _safe_find_text(self, parent, selector): """安全查找元素文本""" try: element = parent.find_element(By.CSS_SELECTOR, selector) return element.text.strip() except: return "" def save_to_csv(self, filename="taobao_products.csv"): """将爬取的数据保存为CSV文件""" if not self.products_data: logger.warning("没有数据可保存") return False try: df = pd.DataFrame(self.products_data) df.to_csv(filename, index=False, encoding='utf-8-sig') logger.info(f"数据已保存到 {filename},共 {len(self.products_data)} 条记录") return True except Exception as e: logger.error(f"保存CSV失败: {str(e)}") return False def save_to_json(self, filename="taobao_products.json"): """将爬取的数据保存为JSON文件""" if not self.products_data: logger.warning("没有数据可保存") return False try: with open(filename, 'w', encoding='utf-8') as f: json.dump(self.products_data, f, ensure_ascii=False, indent=4) logger.info(f"数据已保存到 {filename},共 {len(self.products_data)} 条记录") return True except Exception as e: logger.error(f"保存JSON失败: {str(e)}") return False def get_statistics(self): """获取爬取统计信息""" if not self.products_data: return "没有数据" total = len(self.products_data) shops = len(set(item['shop_name'] for item in self.products_data)) return f"总计: {total} 个商品, {shops} 个店铺" def close(self): """关闭浏览器""" if hasattr(self, 'driver') and self.driver: self.driver.quit() logger.info("浏览器已关闭") def __del__(self): """析构函数,确保浏览器被关闭""" self.close() # 使用示例 if __name__ == "__main__": crawler = TaobaoCrawler() try: # 搜索商品,按销量排序 crawler.search_products("女装", pages=60, sort_by="sale-desc") # 保存数据 crawler.save_to_csv("taobao_products.csv") crawler.save_to_json("taobao_products.json") # 打印统计信息 print(crawler.get_statistics()) finally: crawler.close()将这段代码里面的谷歌浏览器全都换成edge浏览器
最新发布
11-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值