3
0

ysb_snapshot_crawl.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. import base64
  2. import hashlib
  3. import json
  4. import math
  5. import random
  6. import re
  7. import signal
  8. import socket
  9. import sys
  10. import time
  11. import zlib
  12. from pathlib import Path
  13. import requests
  14. import secrets
  15. import string
  16. from Crypto.Cipher import AES
  17. from commons.conn_mysql import MySQLPoolOn2
  18. from DrissionPage import ChromiumPage, ChromiumOptions
  19. from commons.Logger import logger
  20. from oss_upload.oss_upload import AliyunOSSUploader
  21. from pipelines.drug_pipelines import DrugPipeline
  22. from datetime import datetime, timedelta
  23. from area_info.city_name_to_id import get_city
  24. import platform
  25. CAPTCHA_TOKEN = "zPzmt1mG1ouCU6GTzsZN2Lmm8pdZypapPcLJTBRETco"
  26. CAPTCHA_API_URL = "http://api.jfbym.com/api/YmServer/customApi"
  27. SLIDER_OFFSET_FIX = 10
  28. DETAIL_GET_TIMEOUT = 15
  29. DETAIL_URL_WAIT = 10
  30. DETAIL_DOM_WAIT = 8
  31. DETAIL_NAV_RETRIES = 3
  32. DETAIL_APPROVAL_XPATH = (
  33. 'xpath://div[@class="drug-info"]//span[contains(text(),"批准文号")]'
  34. )
  35. chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
  36. PROJECT_ROOT = Path(__file__).resolve().parents[2]
  37. YSB_SPIDER_DIR = PROJECT_ROOT / "spiders" / "yaoshibang"
  38. headers = {
  39. "Accept": "*/*",
  40. "Accept-Language": "zh-CN,zh;q=0.9",
  41. "Connection": "keep-alive",
  42. "Content-Type": "application/json",
  43. "Origin": "https://dian.ysbang.cn",
  44. "Referer": "https://dian.ysbang.cn/",
  45. "Sec-Fetch-Dest": "empty",
  46. "Sec-Fetch-Mode": "cors",
  47. "Sec-Fetch-Site": "same-origin",
  48. "User-Agent": (
  49. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
  50. "(KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36"
  51. ),
  52. "sec-ch-ua": '"Chromium";v="146", "Not-A.Brand";v="24", "Google Chrome";v="146"',
  53. "sec-ch-ua-mobile": "?0",
  54. "sec-ch-ua-platform": '"Windows"',
  55. }
  56. def pkcs7_unpad(data):
  57. if not data:
  58. raise ValueError("Empty data for PKCS7 unpad")
  59. pad_len = data[-1]
  60. if pad_len < 1 or pad_len > 16:
  61. raise ValueError("Invalid PKCS7 padding length")
  62. if data[-pad_len:] != bytes([pad_len]) * pad_len:
  63. raise ValueError("Invalid PKCS7 padding bytes")
  64. return data[:-pad_len]
  65. def derive_ysb_key():
  66. base = "BhCLxFfFhd12K4qRGPfy"
  67. md5_hex = hashlib.md5(base.encode("utf-8")).hexdigest()
  68. return md5_hex[:16].upper().encode("utf-8")
  69. def decrypt_ysb_payload(cipher_text_b64):
  70. """解密药师帮列表接口 data.o 字段,返回 JSON 对象。"""
  71. key = derive_ysb_key()
  72. cipher_bytes = base64.b64decode(cipher_text_b64)
  73. cipher = AES.new(key, AES.MODE_ECB)
  74. decrypted = cipher.decrypt(cipher_bytes)
  75. unpadded = pkcs7_unpad(decrypted)
  76. json_bytes = zlib.decompress(unpadded, zlib.MAX_WBITS | 16)
  77. return json.loads(json_bytes.decode("utf-8"))
  78. class YaoShiBangSnapshot:
  79. def __init__(self, drug_dict=None):
  80. self.driver = None
  81. self.db = MySQLPoolOn2()
  82. self.ip = None
  83. self.login_username = None
  84. self.login_password = None
  85. self.platform = 5
  86. self.page = 0
  87. self.pipeline = DrugPipeline("ysb")
  88. self.task_dict = drug_dict or {}
  89. self.ossuploader = AliyunOSSUploader()
  90. self.start_page = 1
  91. self.end_page = 1
  92. self.account_name = None
  93. self.phone = None
  94. self.password = None
  95. self._register_signal_handler()
  96. if self.task_dict:
  97. self.get_product_data()
  98. self.success = True
  99. self.is_no_prodcut = 0
  100. self.is_product_count = 0
  101. self.token = ""
  102. self._state_value = ""
  103. self.start_date = (datetime.now() - timedelta(minutes=500)).strftime("%Y-%m-%d %H:%M")
  104. def get_product_data(self):
  105. self.task_id = self.task_dict["id"]
  106. self.company_id = self.task_dict["company_id"]
  107. self.product = self.task_dict["product_name"]
  108. self.product_desc = self.task_dict.get("product_specs", "")
  109. self.brand = self.task_dict.get("product_brand", "")
  110. self.product_keyword = self.task_dict.get("product_keyword", "")
  111. self.collect_task_id = self.task_dict.get("collect_task_id", "")
  112. self.sampling_cycle = self.task_dict.get("sampling_cycle", "")
  113. self.sampling_start_time = self.task_dict.get("sampling_start_time", "")
  114. self.sampling_end_time = self.task_dict.get("sampling_end_time", "")
  115. self.collect_equipment_id = self.task_dict.get("collect_equipment_id", "")
  116. self.account_id = self.task_dict.get("collect_equipment_account_id", "")
  117. self.collect_region_id = self.task_dict.get("collect_region_id", "")
  118. self.collect_round = self.task_dict.get("collect_round", 1)
  119. self.start_page = self._parse_page(self.task_dict.get("start_page"), 1)
  120. self.end_page = max(
  121. self.start_page,
  122. self._parse_page(self.task_dict.get("end_page"), self.start_page),
  123. )
  124. def _report_exception(self, exception_type, remark=""):
  125. """异常回告,上报 exception_type (1-6)"""
  126. scheduler = self.task_dict.get("scheduler")
  127. if scheduler:
  128. data = {
  129. "task_id": self.task_id,
  130. "platform": self.task_dict.get("_platform", str(self.platform)),
  131. "username": self.task_dict.get("_device_id", ""),
  132. "current_page": self.page,
  133. "is_finished": 0,
  134. "exception_type": exception_type,
  135. "remark": remark,
  136. }
  137. if self.page:
  138. data["total_pages"] = self.page
  139. if self.pipeline.crawl_count:
  140. data["crawled_count"] = self.pipeline.crawl_count
  141. scheduler.post_report(data)
  142. def _report_page_progress(self, page, crawled_count, is_finished=0, total_pages=0):
  143. """逐页回告进度,返回 True=code=error 需停止"""
  144. scheduler = self.task_dict.get("scheduler")
  145. if scheduler:
  146. data = {
  147. "task_id": self.task_id,
  148. "platform": self.task_dict.get("_platform", str(self.platform)),
  149. "username": self.task_dict.get("_device_id", ""),
  150. "current_page": page,
  151. "crawled_count": crawled_count,
  152. "is_finished": is_finished,
  153. }
  154. if total_pages:
  155. data["total_pages"] = total_pages
  156. scheduler.post_report(data)
  157. return scheduler.end
  158. return False
  159. @staticmethod
  160. def _parse_page(value, default=1):
  161. try:
  162. page = int(value)
  163. return page if page >= 1 else default
  164. except (TypeError, ValueError):
  165. return default
  166. def _register_signal_handler(self):
  167. def handler(signum, frame):
  168. logger.info("收到退出信号,正在关闭浏览器...")
  169. self._quit_browser()
  170. sys.exit(0)
  171. signal.signal(signal.SIGINT, handler)
  172. if hasattr(signal, "SIGTERM"):
  173. signal.signal(signal.SIGTERM, handler)
  174. def _quit_browser(self):
  175. if self.driver:
  176. try:
  177. self.driver.quit()
  178. except Exception:
  179. pass
  180. self.driver = None
  181. @staticmethod
  182. def _get_free_port():
  183. """获取一个当前可用的本地端口,供 Chrome 调试使用。"""
  184. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  185. s.bind(("127.0.0.1", 0))
  186. return s.getsockname()[1]
  187. def _resolve_browser_profile_dir(self):
  188. """浏览器数据目录: <项目根>/spiders/yaoshibang/<账号>"""
  189. profile_dir = YSB_SPIDER_DIR / self.account_name
  190. # 仅兼容历史误路径,新建不再使用 chrome_profile
  191. legacy_nested = YSB_SPIDER_DIR / "spiders" / "yaoshibang" / self.account_name
  192. legacy_chrome_profile = YSB_SPIDER_DIR / "chrome_profile" / self.account_name
  193. for candidate in (profile_dir, legacy_nested, legacy_chrome_profile):
  194. if (candidate / "Default").is_dir() or (candidate / "Local State").is_file():
  195. logger.info("使用已有浏览器配置目录: %s", candidate)
  196. return candidate
  197. profile_dir.mkdir(parents=True, exist_ok=True)
  198. logger.info("新建浏览器配置目录: %s", profile_dir)
  199. return profile_dir
  200. def get_account(self):
  201. # 优先用入口传入的账号密码,避免重复查 DB
  202. phone = self.task_dict.get("account_phone")
  203. password = self.task_dict.get("account_password")
  204. name = self.task_dict.get("account_name")
  205. if phone and password:
  206. self.account_name = name or phone
  207. self.phone = phone
  208. self.password = password
  209. logger.info("使用入口传入的账号: name=%s, phone=%s", self.account_name, phone)
  210. return {"name": self.account_name, "phone": phone, "password": password}
  211. # 兜底:查 DB
  212. sql_account = f""" select `id`,`name`,`ip`,`phone`,`password`,`cookie_timestamp`,`cookie_str` from `accounts_platform` where `platform`=5 and `status`=1 and `equipment_id`=3 order by `cookie_timestamp` asc limit 1 """
  213. account_list = self.db.select_data(sql_account)
  214. if not account_list:
  215. return {}
  216. account_dict = account_list[0]
  217. self.account_name = account_dict["name"]
  218. self.phone = account_dict["phone"]
  219. self.password = account_dict["password"]
  220. return account_dict
  221. def init_browser(self):
  222. co = ChromiumOptions()
  223. # ---------- 1. 根据系统选择 Chrome 路径 ----------
  224. system = platform.system()
  225. if system == "Windows":
  226. chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
  227. # 如果装在非默认路径,可再追加备选
  228. elif system == "Linux":
  229. chrome_path = "/usr/bin/google-chrome" # 或 "/usr/bin/chromium-browser"
  230. else: # macOS 或其他
  231. chrome_path = None # 让 DrissionPage 自动查找
  232. if chrome_path:
  233. co.set_browser_path(chrome_path)
  234. # ---------- 2. 统一使用 set_argument 设置(避免 set_headless 报错) ----------
  235. co.set_argument('--headless=new') # 启用新版无头模式(Windows/Linux 均支持)
  236. co.set_argument('--no-sandbox') # Linux 必需,Windows 下被忽略,无害
  237. co.set_argument('--disable-dev-shm-usage') # Linux 解决 /dev/shm 问题,Windows 无害
  238. co.set_argument('--disable-gpu') # 无头模式下可减少 GPU 问题,兼容所有系统
  239. co.set_argument('--disable-blink-features=AutomationControlled')
  240. co.set_argument('--start-maximized')
  241. co.set_argument('--no-first-run')
  242. co.set_argument('--no-default-browser-check')
  243. # ---------- 3. 用户数据目录(跨平台) ----------
  244. # 例如放在项目目录下的 profiles/ 中,按账号区分
  245. profile_dir = Path("./browser_profiles") / self.account_name
  246. profile_dir.mkdir(parents=True, exist_ok=True)
  247. co.set_user_data_path(str(profile_dir))
  248. # ---------- 4. 可选:本地调试端口(跨平台) ----------
  249. # 如果想手动指定端口,可取消注释并自行实现 _get_free_port()
  250. # debug_port = self._get_free_port()
  251. # co.set_local_port(debug_port)
  252. # co.set_argument(f"--remote-debugging-port={debug_port}")
  253. # co.set_argument("--remote-debugging-address=127.0.0.1")
  254. self.driver = ChromiumPage(co)
  255. def _solve_slider_captcha(self):
  256. """检测并处理易盾滑块验证码,成功返回 True。"""
  257. self.driver.wait.doc_loaded()
  258. time.sleep(2)
  259. yidun = self.driver.ele("xpath://div[@class='yidun_modal']", timeout=3)
  260. if not yidun:
  261. return True
  262. logger.info("检测到滑块验证码,开始处理")
  263. jpg_bytes = yidun.get_screenshot(as_bytes="jpg")
  264. distance = self._call_captcha_api(jpg_bytes)
  265. if distance is None:
  266. logger.error("验证码识别失败")
  267. self._report_exception(3, "滑块验证码识别失败")
  268. return False
  269. logger.info("滑块距离: %s", distance)
  270. slider = self.driver.ele(
  271. "xpath://div[contains(@class,'yidun_slider--hover')]", timeout=5
  272. )
  273. if not slider:
  274. logger.error("未找到滑块元素")
  275. self._report_exception(3, "未找到滑块元素")
  276. return False
  277. try:
  278. drag_distance = float(distance) + SLIDER_OFFSET_FIX
  279. except (TypeError, ValueError):
  280. logger.error("滑块距离非数字: %r", distance)
  281. return False
  282. if not math.isfinite(drag_distance) or drag_distance <= 0:
  283. logger.error("滑块距离无效: %s", drag_distance)
  284. return False
  285. self._simulate_slider_drag(slider, drag_distance - 5)
  286. time.sleep(3)
  287. return True
  288. def _call_captcha_api(self, image_bytes):
  289. """调用云码平台识别滑块距离,失败返回 None。"""
  290. try:
  291. b64 = base64.b64encode(image_bytes).decode()
  292. resp = requests.post(
  293. CAPTCHA_API_URL,
  294. json={"token": CAPTCHA_TOKEN, "type": "22222", "image": b64},
  295. headers={"Content-Type": "application/json"},
  296. timeout=15,
  297. ).json()
  298. logger.info("验证码 API 返回: %s", resp)
  299. if not isinstance(resp, dict):
  300. return None
  301. data = resp.get("data")
  302. if isinstance(data, dict):
  303. dist = data.get("data")
  304. else:
  305. dist = data
  306. if dist is None:
  307. logger.error("验证码 API 未返回距离字段: %s", resp)
  308. return None
  309. try:
  310. d = float(dist)
  311. except (TypeError, ValueError):
  312. logger.error("验证码距离无法解析为数字: %r", dist)
  313. return None
  314. if not math.isfinite(d):
  315. logger.error("验证码距离非有限数值: %r", dist)
  316. return None
  317. return d
  318. except Exception as e:
  319. logger.exception("验证码 API 调用失败: %s", e)
  320. return None
  321. @staticmethod
  322. def _generate_human_track(distance):
  323. try:
  324. distance = float(distance)
  325. except (TypeError, ValueError):
  326. return []
  327. if distance <= 0 or not math.isfinite(distance):
  328. return []
  329. tracks = []
  330. current = 0
  331. mid = distance * 0.7
  332. t = 0.2
  333. v = 0
  334. move_points = []
  335. while current < mid:
  336. a = random.uniform(2, 4)
  337. v0 = v
  338. v = v0 + a * t
  339. move = v0 * t + 0.5 * a * t * t
  340. current += move
  341. move_points.append(move)
  342. while current < distance:
  343. a = -random.uniform(0.5, 1.5)
  344. v0 = v
  345. v = v0 + a * t
  346. if v < 0.5:
  347. v = 0.5
  348. move = v0 * t + 0.5 * a * t * t
  349. current += move
  350. move_points.append(move)
  351. total_points = len(move_points)
  352. for i, move in enumerate(move_points):
  353. y_offset = random.randint(-2, 2) if i % random.randint(2, 4) == 0 else 0
  354. if i < total_points * 0.3:
  355. duration = random.uniform(0.01, 0.03)
  356. elif i > total_points * 0.7:
  357. duration = random.uniform(0.03, 0.08)
  358. else:
  359. duration = random.uniform(0.02, 0.05)
  360. if random.random() < 0.05:
  361. duration += random.uniform(0.05, 0.1)
  362. tracks.append((move, y_offset, duration))
  363. if random.random() < 0.7:
  364. tracks.append((-random.randint(1, 3), 0, 0.05))
  365. return tracks
  366. def _simulate_slider_drag(self, slider_element, target_distance):
  367. if target_distance <= 0:
  368. logger.warning("滑块目标距离无效: %s", target_distance)
  369. return
  370. self.driver.actions.move_to(slider_element).hold()
  371. for offset_x, offset_y, duration in self._generate_human_track(target_distance):
  372. self.driver.actions.move(offset_x, offset_y, duration=duration / 1000)
  373. self.driver.actions.release()
  374. def _is_logged_in(self):
  375. # 与当前账号店铺展示文案一致;换店后需同步修改或改为配置项
  376. title = self.driver.ele(
  377. "xpath=//span[@class='logout']",
  378. timeout=5,
  379. )
  380. return bool(title)
  381. def _current_url(self):
  382. try:
  383. return self.driver.url or ""
  384. except Exception:
  385. return ""
  386. def _goto_detail_page(self, item_id, detail_url):
  387. """get 后 refresh 一次,让 SPA 按当前 URL 重新渲染详情。"""
  388. for attempt in range(1, DETAIL_NAV_RETRIES + 1):
  389. try:
  390. self.driver.get(detail_url, timeout=5)
  391. time.sleep(1.5)
  392. eles = self.driver.eles("xpath=//div[@class='y-dialog']//button[contains(text(),'确认')]", timeout=3)
  393. if len(eles) == 2:
  394. eles[1].click()
  395. time.sleep(1)
  396. self.driver.refresh()
  397. time.sleep(1.5)
  398. ele = self.driver.ele("xpath=//div[@class='drug-pic-viewer']")
  399. if not ele:
  400. continue
  401. else:
  402. return True
  403. except Exception as e:
  404. logger.warning(
  405. "跳转详情异常 item_id=%s attempt=%s: %s",
  406. item_id, attempt, e,
  407. )
  408. time.sleep(random.uniform(0.8, 1.5))
  409. return False
  410. def login(self):
  411. logger.info("开始登录药师帮")
  412. self.driver.get("https://dian.ysbang.cn/#/login", timeout=15)
  413. self.driver.wait.doc_loaded(timeout=10)
  414. time.sleep(2)
  415. input_name = self.driver.ele("xpath://input[@name='userAccount']", timeout=5)
  416. if not input_name:
  417. logger.error("未找到账号输入框")
  418. self._report_exception(4, "登录页未找到账号输入框")
  419. return False
  420. input_name.input(self.phone)
  421. time.sleep(random.uniform(1.5, 2.5))
  422. input_pass = self.driver.ele("xpath://input[@name='password']", timeout=5)
  423. if not input_pass:
  424. logger.error("未找到密码输入框")
  425. self._report_exception(4, "登录页未找到密码输入框")
  426. return False
  427. input_pass.input(self.password)
  428. time.sleep(random.uniform(1.5, 2.5))
  429. login_btn = self.driver.ele("xpath://button[text()='登录']", timeout=5)
  430. if not login_btn:
  431. logger.error("未找到登录按钮")
  432. self._report_exception(4, "登录页未找到登录按钮")
  433. return False
  434. login_btn.click()
  435. time.sleep(3)
  436. for i in range(3):
  437. self._solve_slider_captcha()
  438. time.sleep(3)
  439. if self._is_logged_in():
  440. logger.info("登录成功")
  441. return True
  442. logger.error("登录后未检测到目标店铺名,登录可能失败")
  443. return False
  444. def save_cookies(self, cookies_list):
  445. cookies_dict = {c['name']: c['value'] for c in cookies_list}
  446. timestamp = int(time.time())
  447. next_update_time = timestamp + random.randint(3600, 7200)
  448. update_sql = f""" UPDATE `accounts_platform` SET `cookie_timestamp` = %s, `cookie_str`= %s,`cookie_update_time` = %s, `status`= %s WHERE `name` = %s; """
  449. self.db.execute(update_sql,
  450. (timestamp, json.dumps(cookies_dict), next_update_time, 1, self.account_name))
  451. logger.info("cookie已保存成功")
  452. def _take_snapshot(self, upload_key):
  453. """在当前页面截图并上传,不再重复跳转。"""
  454. time.sleep(1)
  455. self._dismiss_popup_before_screenshot()
  456. try:
  457. jpg_bytes = self.driver.get_screenshot(as_bytes="jpg")
  458. if not jpg_bytes:
  459. logger.warning("截图为空 upload_key=%s", upload_key)
  460. return ""
  461. img_url = self.ossuploader.upload_from_bytes(jpg_bytes, str(upload_key))
  462. except Exception:
  463. logger.exception("截图或 OSS 上传失败 upload_key=%s", upload_key)
  464. return ""
  465. if not img_url:
  466. logger.warning("OSS 未返回有效地址 upload_key=%s", upload_key)
  467. return ""
  468. logger.info("截图上传完成 upload_key=%s url=%s", upload_key, img_url)
  469. time.sleep(random.uniform(1, 2))
  470. return img_url
  471. def gen_pair(self, ex1_len=9, o_raw_len=16):
  472. alphabet = string.ascii_lowercase + string.digits
  473. ex1 = "".join(secrets.choice(alphabet) for _ in range(ex1_len))
  474. o = base64.b64encode(secrets.token_bytes(o_raw_len)).decode("ascii")
  475. return {"ex1": ex1, "o": o}
  476. def build_base_payload(self, keyword, page, first_search):
  477. date_str = time.strftime("%Y-%m-%d %H:%M:%S")
  478. return {
  479. "platform": "pc",
  480. "version": "6.0.0",
  481. "ua": "Chrome146",
  482. 'ex': '{} drugInfo {} {}'.format(self.start_date, date_str, date_str),
  483. "trafficType": 1,
  484. "ex1": "",
  485. "o": "",
  486. "lastClick": -1,
  487. "page": page,
  488. "pagesize": "60",
  489. "classify_id": "",
  490. "searchkey": keyword,
  491. "onlyTcm": 0,
  492. "operationtype": 1,
  493. "qualifiedLoanee": 0,
  494. "drugId": -1,
  495. "tagId": "",
  496. "showRecentlyPurchasedFlag": True,
  497. "onlySimpleLoan": 0,
  498. "sn": "",
  499. "buttons": [],
  500. "buttonList": [],
  501. "synonymId": 0,
  502. "activityTypes": [],
  503. "provider_filter": "",
  504. "factoryNames": "",
  505. "tcmGradeNames": [],
  506. "tcmExeStandardIds": [],
  507. "specs": "",
  508. "deliverFloor": 0,
  509. "purchaseLimitFloor": 0,
  510. "nextRequestKey": "",
  511. "adConfigId": 0,
  512. "stateValue": self._state_value,
  513. "firstSearch": first_search,
  514. "token": self.token,
  515. }
  516. @staticmethod
  517. def _extract_state_value(json_data, data_block):
  518. for src in (json_data, data_block):
  519. if not isinstance(src, dict):
  520. continue
  521. val = src.get("stateValue") or src.get("state_value")
  522. if val:
  523. return str(val)
  524. return None
  525. def _dismiss_popup_before_screenshot(self):
  526. """截图前关闭或隐藏营销弹窗,避免遮挡。"""
  527. close_locs = [
  528. "xpath=//div[contains(@class,'dialog')]//i[contains(@class,'close')]",
  529. "xpath=//div[contains(@class,'popup')]//i[contains(@class,'close')]",
  530. "xpath=//div[contains(@class,'modal')]//i[contains(@class,'close')]",
  531. "xpath=//button[contains(@class,'close')]",
  532. "xpath=//span[text()='×']",
  533. "xpath=//*[contains(text(),'智能采购')]/ancestor::div[1]//*[contains(@class,'close')]",
  534. ]
  535. for loc in close_locs:
  536. try:
  537. btn = self.driver.ele(loc, timeout=0.5)
  538. if btn:
  539. btn.click()
  540. time.sleep(0.2)
  541. except Exception:
  542. pass
  543. try:
  544. # 兜底:隐藏常见高层弹窗和遮罩
  545. self.driver.run_js(
  546. """
  547. const sels = [
  548. '[class*="modal"]',
  549. '[class*="popup"]',
  550. '[class*="dialog"]',
  551. '[class*="mask"]',
  552. '[class*="overlay"]'
  553. ];
  554. for (const s of sels) {
  555. document.querySelectorAll(s).forEach(el => {
  556. const style = getComputedStyle(el);
  557. const z = parseInt(style.zIndex || '0', 10);
  558. if (z >= 999 && style.display !== 'none') {
  559. el.style.display = 'none';
  560. }
  561. });
  562. }
  563. document.body.style.overflow = 'auto';
  564. """
  565. )
  566. time.sleep(0.2)
  567. except Exception:
  568. pass
  569. def to_product(self, item):
  570. now = time.strftime("%Y-%m-%d %H:%M:%S")
  571. item_id = item.get("wholesaleid", "")
  572. provider_id = item.get("providerId", "")
  573. city_str = item.get("warehouseCity", "")
  574. city_id = province_id = city = province = ""
  575. price = item.get("disPrice", "")
  576. if not price:
  577. price = item.get("minprice", "")
  578. if not price:
  579. price = item.get("price", "")
  580. shop_name = item.get("provider_name", "")
  581. if not shop_name:
  582. shop_name = item.get("abbreviation", "")
  583. product = {
  584. "platform": self.platform,
  585. "item_id": item_id,
  586. "enterprise_id": self.company_id,
  587. "product_name": item.get("drugname", ""),
  588. "spec": item.get("specification", ""),
  589. "one_price": '',
  590. "detail_url": f"https://dian.ysbang.cn/#/drugInfo?wholesaleid={item_id}&trafficType=1",
  591. "shop_name": shop_name,
  592. "anonymous_store_name": "",
  593. "shop_url": f"https://dian.ysbang.cn/#/supplierstore?providerId={provider_id}&trafficType=4",
  594. "city_name": city,
  595. "city_id": city_id,
  596. "province_name": province,
  597. "province_id": province_id,
  598. "area_info": "",
  599. "factory_name": item.get("manufacturer", ""),
  600. "scrape_date": time.strftime("%Y-%m-%d"),
  601. "price": price,
  602. "sales": "",
  603. "stock_count": item.get("stockAvailable", ""),
  604. "snapshot_url": "",
  605. "approval_num": "",
  606. "produced_time": item.get("prodDate", ""),
  607. "deadline": item.get("valid_date", ""),
  608. "update_time": now,
  609. "insert_time": now,
  610. "number": 1,
  611. "product_brand": self.brand or "",
  612. "collect_task_id": self.collect_task_id,
  613. "search_name": self.product,
  614. "company_name": "",
  615. "collect_config_info": json.dumps(
  616. {"sampling_cycle": self.sampling_cycle, "sampling_start_time": self.sampling_start_time,
  617. "sampling_end_time": self.sampling_end_time}),
  618. "account_id": self.account_id,
  619. "collect_region_id": self.collect_region_id,
  620. "collect_round": self.collect_round,
  621. "is_sold_out": 0
  622. }
  623. return product
  624. def parse_detail(self, product):
  625. appvolnum_ele = self.driver.ele(
  626. 'xpath://div[@class="drug-info"]//span[contains(text(),"批准文号")]/following-sibling::span[1]')
  627. appvolnum_value = appvolnum_ele.text if appvolnum_ele else ""
  628. price = ""
  629. discount_ele = self.driver.ele(
  630. 'xpath://div[@class="sale-info-wrap"]//div[@class="tooltip-content"]',
  631. timeout=2,
  632. )
  633. discount_value = discount_ele.text if discount_ele else ""
  634. if not price and discount_value:
  635. price_re = re.search(r"¥([0-9.]+)", discount_value)
  636. if price_re:
  637. price = price_re.group(1).strip()
  638. current_ele = self.driver.ele(
  639. 'xpath://div[@class="sale-info-wrap"]//span[contains(@class,"current-price")]',
  640. timeout=3,
  641. )
  642. if current_ele and not price:
  643. price = (current_ele.text or "").replace("¥", "").strip()
  644. list_price = product.get("price", "")
  645. if price:
  646. product["price"] = price
  647. if appvolnum_value:
  648. product["approval_num"] = appvolnum_value
  649. logger.info(
  650. "详情解析 wholesaleid=%s list_price=%s dom_price=%s url=%s",
  651. product.get("item_id"),
  652. list_price,
  653. product.get("price"),
  654. self._current_url(),
  655. )
  656. return product
  657. def search(self):
  658. self.driver.get("https://dian.ysbang.cn/#/home", timeout=15)
  659. self.driver.wait.doc_loaded(timeout=10)
  660. time.sleep(2)
  661. if not self._is_logged_in():
  662. if not self.login():
  663. return False
  664. time.sleep(3)
  665. cookies_list = self.driver.cookies()
  666. cookies_dict = {c['name']: c['value'] for c in cookies_list}
  667. self.save_cookies(cookies_list)
  668. self.token = cookies_dict.get("Token") or cookies_dict.get("token")
  669. keyword = self.product
  670. if self.brand:
  671. keyword = (self.brand + " " + self.product).strip()
  672. if self.product_desc:
  673. keyword = (keyword + " " + self.product_desc).strip()
  674. self._state_value = ""
  675. # 支持续传:从 current_page(>0)或 start_page 开始
  676. page_start = int(self.task_dict.get("current_page", 0) or 0)
  677. if page_start <= 0:
  678. page_start = self.start_page
  679. self.page = page_start # 兜底:避免0页时回告 total_pages=0
  680. for page in range(page_start, 100):
  681. first_search = page == 1
  682. logger.info("药师帮爬取第%s页 firstSearch=%s stateValue=%s", page, first_search,
  683. self._state_value or "(空)")
  684. pair = self.gen_pair()
  685. payload = self.build_base_payload(keyword, page=page, first_search=first_search)
  686. payload["ex1"] = pair["ex1"]
  687. payload["o"] = pair["o"]
  688. response = None
  689. for attempt in range(3):
  690. try:
  691. response = requests.post(
  692. "https://dian.ysbang.cn/wholesale-drug/sales/getWholesaleList/v4270", headers=headers,
  693. json=payload, timeout=30
  694. )
  695. if response.status_code == 200:
  696. break
  697. except Exception as e:
  698. logger.error("第%s页请求失败 (%s/3): %s", page, attempt + 1, e)
  699. response = None
  700. time.sleep(10)
  701. if not response or response.status_code != 200:
  702. logger.error("第%s页请求失败,停止爬取", page)
  703. self._report_exception(5, f"第{page}页请求失败(status={response.status_code if response else 'None'})")
  704. return False
  705. try:
  706. data_json = response.json()
  707. except json.JSONDecodeError:
  708. logger.exception("第%s页响应不是合法 JSON", page)
  709. self._report_exception(5, f"第{page}页响应非合法JSON")
  710. return False
  711. data_block = data_json.get("data") or {}
  712. if str(data_json.get("message", "")) == "该操作需要登录":
  713. logger.warning("第%s页需要登录,请检查浏览器登录态", page)
  714. self._report_exception(1, "账号登录态失效")
  715. return False
  716. encrypted_o = data_block.get("o")
  717. if not encrypted_o:
  718. logger.warning("第%s页返回无加密 data.o: %s", page, data_json)
  719. break
  720. try:
  721. json_data = decrypt_ysb_payload(encrypted_o)
  722. except Exception as e:
  723. logger.exception("第%s页解密失败: %s", page, e)
  724. self._report_exception(6, f"第{page}页数据解密失败")
  725. continue
  726. state_val = self._extract_state_value(json_data, data_block)
  727. if state_val:
  728. self._state_value = state_val
  729. wholesales = json_data.get("wholesales", [])
  730. if not wholesales:
  731. logger.info(f"第{page}页无数据,停止")
  732. break
  733. for item in wholesales:
  734. item_id = item.get("wholesaleid", "")
  735. if not item_id:
  736. continue
  737. detail_url = (
  738. f"https://dian.ysbang.cn/#/drugInfo?wholesaleid={item_id}&trafficType=1"
  739. )
  740. product = self.to_product(item)
  741. title = product.get("product_name", "")
  742. if self.brand not in title:
  743. self.is_product_count += 1
  744. continue
  745. if self.product not in title:
  746. self.is_product_count += 1
  747. continue
  748. if self.product in title and self.brand in title:
  749. self.is_product_count = 0
  750. if self.is_product_count >= 20:
  751. return False
  752. if not self._goto_detail_page(item_id, detail_url):
  753. logger.warning(
  754. "详情页跳转失败,跳过 item_id=%s url=%s",
  755. item_id, detail_url,
  756. )
  757. continue
  758. product = self.parse_detail(product)
  759. upload_key = hashlib.md5(detail_url.encode("utf-8")).hexdigest()
  760. product["snapshot_url"] = self._take_snapshot(upload_key)
  761. try:
  762. self.pipeline.storge_data(product)
  763. logger.info("%s", json.dumps(product, ensure_ascii=False, default=str))
  764. except Exception as e:
  765. logger.exception("写入数据库失败: %s", e)
  766. # 逐页回告进度
  767. self.page = page
  768. if self._report_page_progress(page, self.pipeline.crawl_count) == True:
  769. break
  770. def run(self):
  771. account_dict = self.get_account()
  772. if not account_dict:
  773. logger.error("ysb快照模式无账号可用")
  774. self._report_exception(1, "无可用账号")
  775. return 0, False
  776. try:
  777. self.init_browser()
  778. self.search()
  779. except Exception as e:
  780. logger.exception("运行异常: %s", e)
  781. self.success = False
  782. self._report_exception(5, f"未知异常: {e}")
  783. finally:
  784. self._quit_browser()
  785. return self.pipeline.crawl_count, self.success, self.page