Files
spider/lib/module/taketoys.py
2026-04-10 10:57:44 +08:00

132 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from core import spiders, types
from utils import formats, messages
class SpiderModule(spiders.Spiders):
project_name = "taketoys"
def __init__(self):
super().__init__(
self.project_name,
bitch = 30,
worker_num = 5,
switch_clash = False,
reflush_browser = False
)
self.tab.get('https://taketoys.sg/')
# input('wait:')
def get_category_urls(self) -> list[str]:
category_urls = []
one_menu_eles = self.tab.eles('xpath=/html/body/header/div[2]/div/nav/ul/li')
for one_menu_ele in one_menu_eles[1:-1]:
one_link_ele = one_menu_ele.ele('xpath=a')
one_url = one_link_ele.attr('href')
one_name = one_link_ele.text.replace('/', '-')
category_urls.append(f"{one_url}#{one_name}")
messages.sendInfo(one_name)
two_menu_eles = one_menu_ele.eles('xpath=ul/li', timeout=1)
for two_index, two_menu_ele in enumerate(two_menu_eles):
two_link_ele = two_menu_ele.ele('xpath=a')
two_url = two_link_ele.attr('href')
two_name = two_link_ele.text.replace('/', '-').replace(',', '').replace('#', ' ')
category_urls.append(f"{two_url}#{one_name}/{two_name}")
# try:
# three_menu_eles = self.tab.ele(f'xpath=//*[@id="__next"]/div[1]/header/div/div[2]/nav/div[{two_index+1}]/div/div[1]').eles('@tag()=li')
# except:
# continue
# for three_menu_ele in three_menu_eles:
# three_link_ele = three_menu_ele.ele('@tag()=a')
# three_url = three_link_ele.attr('href')
# three_name = three_link_ele.text.replace('/', '-')
# category_urls.append(f"{three_url}#{one_name}/{two_name}/{three_name}")
return category_urls
def get_goods_urls(self, category_url: str) -> list[str]:
self.tab.get(f"{category_url}?page=1")
goods_urls = []
#####获取页数####
#################
goods_urls = []
while True:
self.tab.scroll.to_bottom()
try:
self.tab.ele('text=Show more', timeout=5).click()
self.tab.wait(1)
except:
break
goods_eles = self.tab.eles('.col-xs-6 col-sm-4 col-lg-3 item-wrapper')
for goods_ele in goods_eles:
goods_url = f"{goods_ele.ele('@tag()=a').attr('href')}"
goods_urls.append(goods_url)
messages.sendInfo(f"{len(goods_urls)}")
return goods_urls
def get_goods_info(self, url) -> types.GoodsInfo:
def get_image_urls():
image_urls = []
image_eles = tab.eles('xpath=/html/body/main/div[2]/div[2]/div[2]/div[2]/div/div/div[3]/div[2]/div/div')
for image_ele in image_eles:
image_url = image_ele.ele('xpath=img').attr('src')
image_urls.append(image_url)
return image_urls
tab = self.browser.new_tab(url)
tab.set.window.max()
spu = formats.url_to_spu(url)
try:
h1_ele = tab.ele('@tag()=h1')
brand = ''
try:
brand = h1_ele.ele('@tag()=strong').text
except:
pass
title = h1_ele.ele('@tag()=span').text
desc = ""
try:
desc = formats.clean_html(tab.ele('#product-info-tab').html)
except:
pass
price = tab.ele('.price').attr('title').replace('SGD', '').replace(',', '')
old_price = price
main_images = get_image_urls()
goods_info = types.GoodsInfo(
title=title,
desc=desc,
brand=brand,
url=url,
spu=spu,
price=price,
old_price=old_price,
)
goods_info.attr = 'S'
goods_info.attr_items = []
goods_info.images = main_images
goods_info.p_lists = []
except Exception as e:
try:
tab.close()
except:
pass
raise Exception(e)
try:
tab.close()
except:
pass
return goods_info