| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- # -*- coding: utf-8 -*-
- # Author : Charley
- # Python : 3.10.8
- # Date : 2025/10/31 13:48
- import inspect
- import requests
- import user_agent
- from loguru import logger
- from parsel import Selector
- from mysql_pool import MySQLConnectionPool
- from tenacity import retry, stop_after_attempt, wait_fixed
- from settings import LANGUAGE_LIST
- """
- https://gatherer.wizards.com/advanced-search
- """
- logger.remove()
- logger.add("./logs/{time:YYYYMMDD}.log", encoding='utf-8', rotation="00:00",
- format="[{time:YYYY-MM-DD HH:mm:ss.SSS}] {level} {message}",
- level="DEBUG", retention="7 day")
- headers = {
- "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
- # "referer": "https://gatherer.wizards.com/M20/en-us/1/aerial-assault",
- "user-agent": user_agent.generate_user_agent()
- }
- def after_log(retry_state):
- """
- retry 回调
- :param retry_state: RetryCallState 对象
- """
- # 检查 args 是否存在且不为空
- if retry_state.args and len(retry_state.args) > 0:
- log = retry_state.args[0] # 获取传入的 logger
- else:
- log = logger # 使用全局 logger
- if retry_state.outcome.failed:
- log.warning(
- f"Function '{retry_state.fn.__name__}', Attempt {retry_state.attempt_number} Times")
- else:
- log.info(f"Function '{retry_state.fn.__name__}', Attempt {retry_state.attempt_number} succeeded")
- @retry(stop=stop_after_attempt(5), wait=wait_fixed(1), after=after_log)
- def get_proxys(log):
- """
- 获取代理
- :return: 代理
- """
- tunnel = "x371.kdltps.com:15818"
- kdl_username = "t13753103189895"
- kdl_password = "o0yefv6z"
- try:
- proxies = {
- "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": kdl_username, "pwd": kdl_password, "proxy": tunnel},
- "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": kdl_username, "pwd": kdl_password, "proxy": tunnel}
- }
- return proxies
- except Exception as e:
- log.error(f"Error getting proxy: {e}")
- raise e
- @retry(stop=stop_after_attempt(5), wait=wait_fixed(1), after=after_log)
- def get_cards_single_page(log, sql_pool, card_lang_url):
- """
- 获取卡组列表 单页信息
- :param log: logger对象
- :param sql_pool: MySQL对象
- :param card_lang_url: 单页链接
- :return: len(tag_div_list) -> 控制翻页
- """
- log.debug(f" ------------------------- 当前查询的 card_lang_url 为:{card_lang_url} -------------------------")
- response = requests.get(card_lang_url, headers=headers, timeout=5)
- # response = requests.get(card_list_url, headers=headers, proxies=get_proxys(log))
- response.raise_for_status()
- selector = Selector(response.text)
- tag_div_list = selector.xpath(
- '//div[@data-testid="resultsListWrapper"]/div') # data-testid="resultsListWrapper"
- for tag_div in tag_div_list:
- # card_name = tag_div.xpath('./div[@data-testid="cardName"]/a/text()').get()
- card_detail_url = tag_div.xpath('./a/@href').get()
- if card_detail_url:
- card_detail_url = "https://gatherer.wizards.com" + card_detail_url
- try:
- get_card_detail(log, sql_pool, card_detail_url)
- except Exception as e:
- log.error(f"Error getting card detail: {e}")
- else:
- log.warning(f"Warning getting card detail: {card_detail_url}")
- return len(tag_div_list)
- def get_cards_list(log, sql_pool, card_lang_url):
- """
- card 列表 翻页 每页72条
- https://gatherer.wizards.com/sets/SPM?page=2
- :param log: logger对象
- :param sql_pool: MySQL对象
- :param card_lang_url: 翻页链接
- """
- log.debug('开始查询卡组信息 ->->->->->->->->->->->->->->->->->->->')
- for page in range(1, 1000):
- card_list_url = f"{card_lang_url}{page}"
- log.debug(f" ------------------------- 当前查询的页码为:{page} -------------------------")
- len_tag_div_list = get_cards_single_page(log, sql_pool, card_list_url)
- if len_tag_div_list < 72:
- log.debug(f"当前页为 {page}, 当页卡牌数量为 {len_tag_div_list}, 没有更多数据了.........")
- break
- @retry(stop=stop_after_attempt(5), wait=wait_fixed(1), after=after_log)
- def get_card_detail(log, sql_pool, card_detail_url):
- """
- 卡牌详情
- :param log: logger对象
- :param sql_pool: MySQL对象
- :param card_detail_url: 详情页链接
- """
- log.debug('开始查询卡牌 <详情> 信息 ->->->->->->->->->->->->->->->->->->->')
- response = requests.get(card_detail_url, headers=headers)
- response.raise_for_status()
- selector = Selector(response.text)
- tag_detail_section = selector.xpath('//section[@data-testid="cardDetailsWrapper"]')
- card_name = tag_detail_section.xpath('.//h1[@data-testid="cardDetailsCardName"]/text()').get()
- card_type = tag_detail_section.xpath('.//h1[@data-testid="cardDetailsTypeLine"]/text()').get()
- card_rarity = tag_detail_section.xpath('.//h1[@data-testid="cardDetailsRarity"]/text()').get()
- card_artist = tag_detail_section.xpath('./article[3]/div[1]/div[1]/section//h1/text()').get() # 作者
- card_p_t = tag_detail_section.xpath('./article[3]/div[1]/div[2]/section/section/div//text()').getall()
- card_p_t = "".join(card_p_t).strip() if card_p_t else None
- card_set = tag_detail_section.xpath('./article[3]/div[2]/div[1]/section/a/h1//text()').getall() # 卡组
- card_set = "".join(card_set).strip() if card_set else None
- card_number = tag_detail_section.xpath('.//h1[@data-testid="cardDetailsCardNumber"]/text()').get() # 编号
- card_language = tag_detail_section.xpath('.//h1[@data-testid="cardDetailsLanguage"]/text()').get()
- img = selector.xpath('//img[@data-testid="cardFrontImage"]/@src').get()
- if 'webp' in img:
- img = img.replace('webp', 'png')
- detail_data_dict = {
- "card_name": card_name,
- "card_type": card_type,
- "card_rarity": card_rarity,
- "card_artist": card_artist,
- "card_p_t": card_p_t,
- "card_set": card_set,
- "card_number": card_number,
- "card_language": card_language,
- "card_url": card_detail_url,
- "img": img
- }
- # print(detail_data_dict)
- # 更新数据
- sql_pool.insert_one_or_dict(table="magic_cards_record", data=detail_data_dict, ignore=True)
- @retry(stop=stop_after_attempt(100), wait=wait_fixed(3600), after=after_log)
- def magic_main(log):
- """
- 主函数
- :param log: logger对象
- """
- log.info(
- f'开始运行 {inspect.currentframe().f_code.co_name} 爬虫任务....................................................')
- # 配置 MySQL 连接池
- sql_pool = MySQLConnectionPool(log=log)
- if not sql_pool.check_pool_health():
- log.error("数据库连接池异常")
- raise RuntimeError("数据库连接池异常")
- try:
- # 获取所有卡组中的卡牌信息
- for card_lang_url in LANGUAGE_LIST:
- try:
- get_cards_list(log, sql_pool, card_lang_url)
- except Exception as e:
- log.error(f"Error get_cards: {e}")
- except Exception as e:
- log.error(f'{inspect.currentframe().f_code.co_name} error: {e}')
- finally:
- log.info(f'爬虫程序 {inspect.currentframe().f_code.co_name} 运行结束,等待下一轮的采集任务............')
- if __name__ == '__main__':
- # card_url_ = "https://gatherer.wizards.com/sets/SPM"
- # https://gatherer.wizards.com/SPE/en-us/1/amateur-hero
- # get_card_detail(logger, None, "https://gatherer.wizards.com/MKM/zh-cn/167/a-killer-among-us")
- magic_main(logger)
|