jd_auto_crawl.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. import random
  2. import re
  3. import signal
  4. import socket
  5. import sys
  6. import time
  7. from decimal import Decimal, InvalidOperation
  8. from urllib.parse import quote
  9. from DrissionPage import ChromiumPage, ChromiumOptions
  10. import json
  11. import hashlib
  12. from commons.Logger import get_spider_logger
  13. from commons.conn_mysql import MySQLPoolOnline
  14. from pipelines.drug_pipelines import DrugPipeline
  15. from commons.feishu_webhook import send_text
  16. from spiders.jd.jd_captcha import handle_jd_slider_captcha
  17. from oss_upload.oss_upload import AliyunOSSUploader
  18. logger = get_spider_logger("jd")
  19. chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
  20. FETCH_TIMEOUT_FIRST = 5
  21. FETCH_TIMEOUT_SCROLL = 6
  22. LISTEN_CLEAR_ROUNDS = 3
  23. LISTEN_CLEAR_TIMEOUT = 0.45
  24. # 「下一页」是否在视口内(条件略宽)
  25. _JS_NEXT_BTN_IN_VIEWPORT = """
  26. var el = arguments[0];
  27. if (!el) return false;
  28. var r = el.getBoundingClientRect();
  29. var h = window.innerHeight || document.documentElement.clientHeight || 800;
  30. var w = window.innerWidth || document.documentElement.clientWidth || 1200;
  31. return r.bottom > 80 && r.top < h - 40 && r.right > 0 && r.left < w;
  32. """
  33. class JdCrawlerV2:
  34. def __init__(self, drug_dict=None):
  35. self.driver = None
  36. self.register_signal_handler()
  37. self.db = MySQLPoolOnline()
  38. self.ip = None
  39. self.account_name = None
  40. self.login_username = None
  41. self.login_password = None
  42. self.platform = 2
  43. self.pipeline = DrugPipeline("jd")
  44. self.task_dict = drug_dict or {}
  45. self.ossuploader = AliyunOSSUploader()
  46. self.start_page = 1
  47. self.end_page = 1
  48. if self.task_dict:
  49. self.get_product_data()
  50. self.success = True
  51. self.is_no_prodcut = 0
  52. def get_product_data(self):
  53. self.task_id = self.task_dict["id"]
  54. self.company_id = self.task_dict["company_id"]
  55. self.product = self.task_dict["product_name"]
  56. self.product_desc = self.task_dict.get("product_specs", "")
  57. self.brand = self.task_dict.get("product_brand", "")
  58. self.product_keyword = self.task_dict.get("product_keyword", "")
  59. self.collect_task_id = self.task_dict.get("collect_task_id", "")
  60. self.sampling_cycle = self.task_dict.get("sampling_cycle", "")
  61. self.sampling_start_time = self.task_dict.get("sampling_start_time", "")
  62. self.sampling_end_time = self.task_dict.get("sampling_end_time", "")
  63. self.collect_equipment_id = self.task_dict.get("collect_equipment_id", "")
  64. self.account_id = self.task_dict.get("collect_equipment_account_id", "")
  65. self.collect_region_id = self.task_dict.get("collect_region_id", "")
  66. self.collect_round = self.task_dict.get("collect_round", 1)
  67. self.start_page = self._parse_page(self.task_dict.get("start_page"), 1)
  68. self.end_page = self._parse_page(self.task_dict.get("end_page"), 15)
  69. @staticmethod
  70. def _parse_page(value, default=1):
  71. try:
  72. page = int(value)
  73. return page if page >= 1 else default
  74. except (TypeError, ValueError):
  75. return default
  76. @staticmethod
  77. def _get_free_port():
  78. """获取一个当前可用的本地端口,供 Chrome 调试使用。"""
  79. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  80. s.bind(("127.0.0.1", 0))
  81. return s.getsockname()[1]
  82. def init_browser(self):
  83. co = ChromiumOptions().set_browser_path(chrome_path)
  84. debug_port = self._get_free_port()
  85. co.set_user_data_path(f"./spiders/jd/{self.account_name}")
  86. co.set_local_port(debug_port)
  87. co.set_argument(f"--remote-debugging-port={debug_port}")
  88. co.set_argument("--remote-debugging-address=127.0.0.1")
  89. # co.set_argument("--disable-blink-features=AutomationControlled")
  90. co.set_argument("--disable-dev-shm-usage")
  91. co.set_argument("--no-first-run") # 避免首次运行弹窗
  92. co.set_argument("--no-default-browser-check") # 避免默认浏览器检查
  93. if self.ip:
  94. proxy = self.ip.strip()
  95. if not proxy.startswith(("http://", "https://")):
  96. proxy = f"http://{proxy}"
  97. co.set_argument(f"--proxy-server={proxy}")
  98. logger.info("启动浏览器: account=%s, debug_port=%s", self.account_name, debug_port)
  99. self.driver = ChromiumPage(co)
  100. self._listen_started = False
  101. def _start_listen(self):
  102. """登录完成后再开监听,避免干扰登录页/验证码拖动。"""
  103. if self._listen_started or not self.driver:
  104. return
  105. self.driver.listen.start("api?appid=search-pc-java")
  106. self._listen_started = True
  107. logger.info("已启动搜索接口监听")
  108. def register_signal_handler(self):
  109. def handler(signum, frame):
  110. print("\n⚠️ 程序退出")
  111. if self.driver:
  112. self.driver.quit()
  113. sys.exit(0)
  114. signal.signal(signal.SIGINT, handler)
  115. if hasattr(signal, "SIGTERM"):
  116. signal.signal(signal.SIGTERM, handler)
  117. def sleep(self, a, b):
  118. time.sleep(random.uniform(a, b))
  119. def _scroll_page_down(self, delta=900):
  120. self.driver.run_js(f"window.scrollBy(0, {int(delta)});")
  121. time.sleep(random.uniform(0.3, 0.6))
  122. def _scroll_next_into_view(self, el):
  123. if not el:
  124. return
  125. try:
  126. self.driver.run_js(
  127. "arguments[0].scrollIntoView({block:'center',behavior:'instant'});",
  128. el,
  129. )
  130. self.sleep(1, 2)
  131. except Exception as e:
  132. logger.warning("滚动到下一页按钮失败: %s", e)
  133. try:
  134. el.scroll.to_see()
  135. except Exception:
  136. pass
  137. def _get_scroll_info(self):
  138. return self.driver.run_js("""
  139. return {
  140. scrollY: window.scrollY || window.pageYOffset || 0,
  141. docH: Math.max(document.body.scrollHeight,
  142. document.documentElement.scrollHeight,
  143. document.body.offsetHeight),
  144. viewH: window.innerHeight || document.documentElement.clientHeight || 800
  145. };
  146. """)
  147. def _find_next_btn(self, timeout=0.3):
  148. try:
  149. return self.driver.ele("text=下一页", timeout=timeout)
  150. except Exception:
  151. return None
  152. def _is_next_btn_visible(self, btn):
  153. if not btn:
  154. return False
  155. try:
  156. return bool(self.driver.run_js(_JS_NEXT_BTN_IN_VIEWPORT, btn))
  157. except Exception:
  158. return False
  159. def _human_click(self, element):
  160. """在目标节点上触发 click,避免 move_to + 无目标 actions.click() 因布局位移点到商品链接触发详情页。"""
  161. if not element:
  162. return False
  163. try:
  164. self.sleep(0.8, 2.0)
  165. try:
  166. self.driver.run_js(
  167. "arguments[0].scrollIntoView({block:'center',behavior:'instant'});",
  168. element,
  169. )
  170. except Exception:
  171. pass
  172. self.sleep(0.2, 0.6)
  173. self.driver.run_js("arguments[0].click();", element)
  174. return True
  175. except Exception as e:
  176. logger.warning("点击失败: %s", e)
  177. try:
  178. element.click()
  179. return True
  180. except Exception:
  181. return False
  182. @staticmethod
  183. def _estimated_price(json_data):
  184. fp = json_data.get("finalPrice")
  185. if isinstance(fp, dict):
  186. return fp.get("estimatedPrice", "") or ""
  187. return ""
  188. def get_heshu(self,full_title):
  189. last_box = None
  190. last_bottle = None
  191. for match in re.finditer(r"(\d+)(盒|瓶)", full_title):
  192. if match.group(2) == '盒':
  193. last_box = match
  194. else: # 瓶
  195. last_bottle = match
  196. if last_box:
  197. return int(last_box.group(1))
  198. elif last_bottle:
  199. return int(last_bottle.group(1))
  200. else:
  201. return 1
  202. def get_heshu(self,full_title):
  203. last_box = None
  204. last_bottle = None
  205. for match in re.finditer(r"(\d+)(盒|瓶)", full_title):
  206. if match.group(2) == '盒':
  207. last_box = match
  208. else: # 瓶
  209. last_bottle = match
  210. if last_box:
  211. return int(last_box.group(1))
  212. elif last_bottle:
  213. return int(last_bottle.group(1))
  214. else:
  215. return 1
  216. def parse(self, ware_list):
  217. for w in ware_list:
  218. title = w.get("wareName", "")
  219. title = re.sub(r"<[^>]*>", "", title).strip()
  220. color = w.get("color", "")
  221. full_title = title + " " + color
  222. logger.info(full_title)
  223. if self.product not in full_title:
  224. self.is_no_prodcut += 1
  225. continue
  226. if self.brand not in full_title:
  227. self.is_no_prodcut += 1
  228. continue
  229. if self.product_desc:
  230. if self.product_desc in full_title:
  231. crawl_product_desc = self.product_desc
  232. else:
  233. crawl_product_desc = ""
  234. title = full_title
  235. else:
  236. crawl_product_desc = ""
  237. title = full_title
  238. if "+[" in title:
  239. continue
  240. self.is_no_prodcut = 0
  241. status = 1
  242. if self.product_keyword:
  243. search_keyword_list = self.product_keyword.split(",")
  244. for search_keyword in search_keyword_list:
  245. if search_keyword.strip() not in title:
  246. status = 0
  247. if status == 0:
  248. continue
  249. logger.info(f"商品名:{title}")
  250. sku_id = w.get("skuId", "")
  251. sales = w.get("totalSales", "")
  252. shop_id = w.get("shopId", "")
  253. shop_name = w.get("shopName", "")
  254. heshu_count = self.get_heshu(full_title)
  255. final_price = self._estimated_price(w)
  256. jd_price = w.get("jdPrice", "")
  257. item_url = f"https://item.jd.com/{sku_id}.html"
  258. low_price = final_price if final_price else jd_price
  259. # 获取列表页快照
  260. ele_xpath = "//div[@id='main_search_conter']//div[contains(@class,'_goodsContainer_')]/div[@data-sku=" + "'" + sku_id + "'" + "]"
  261. ele_screen = self.driver.ele("xpath="+ele_xpath)
  262. upload_key = hashlib.md5(item_url.encode("utf-8")).hexdigest()
  263. snap_url = ""
  264. try:
  265. price = Decimal(str(low_price)).quantize(Decimal("0.00"))
  266. except (InvalidOperation, ValueError):
  267. price = Decimal("0.00")
  268. item_url = f"https://item.jd.com/{sku_id}.html"
  269. mall_url = f"https://mall.jd.com/index-{shop_id}.html?from=pc"
  270. # 字段与 yaofangwang_crawl 对齐;键顺序须与 commons.sql_data.RETRIEVE_SCRAPE_INSERT_COLUMNS 一致
  271. now_ts = time.strftime("%Y-%m-%d %H:%M:%S")
  272. product = {
  273. "platform": self.platform,
  274. "item_id": sku_id,
  275. "enterprise_id": self.company_id,
  276. "product_name": title,
  277. "spec": crawl_product_desc,
  278. "one_price": "",
  279. "detail_url": item_url,
  280. "shop_name": shop_name,
  281. "anonymous_store_name": "",
  282. "shop_url": mall_url,
  283. "city_name": "",
  284. "city_id": "",
  285. "province_name": "",
  286. "province_id": "",
  287. "shipment_city_name": "",
  288. "shipment_city_id": "",
  289. "shipment_province_name": "",
  290. "shipment_province_id": "",
  291. "area_info": "",
  292. "factory_name": "",
  293. "scrape_date": time.strftime("%Y-%m-%d"),
  294. "price": price,
  295. "sales": sales,
  296. "stock_count": "",
  297. "snapshot_url": snap_url,
  298. "approval_num": "",
  299. "produced_time": "",
  300. "deadline": "",
  301. "update_time": now_ts,
  302. "insert_time": now_ts,
  303. "number": heshu_count,
  304. "product_brand": self.brand or "",
  305. "collect_task_id": self.collect_task_id,
  306. "search_name": self.product,
  307. "company_name": "",
  308. "collect_config_info": json.dumps(
  309. {
  310. "sampling_cycle": self.sampling_cycle,
  311. "sampling_start_time": self.sampling_start_time,
  312. "sampling_end_time": self.sampling_end_time,
  313. }
  314. ),
  315. "account_id": self.account_id,
  316. "collect_region_id": self.collect_region_id,
  317. "collect_round": self.collect_round,
  318. "is_sold_out": 0
  319. }
  320. try:
  321. self.pipeline.storge_data(product)
  322. logger.info("%s", json.dumps(product, ensure_ascii=False, default=str))
  323. except Exception as e:
  324. logger.exception("写入数据库失败: %s", e)
  325. @staticmethod
  326. def _response_has_ware_list(data):
  327. if not isinstance(data, dict):
  328. return False
  329. wl = data.get("data", {}).get("wareList")
  330. return bool(wl)
  331. def fetch_items_once(self, timeout=FETCH_TIMEOUT_FIRST):
  332. n = 0
  333. for resp in self.driver.listen.steps(timeout=timeout):
  334. try:
  335. data = resp.response.body
  336. if not self._response_has_ware_list(data):
  337. continue
  338. ware_list = data["data"]["wareList"]
  339. self.parse(ware_list)
  340. n += len(ware_list)
  341. except Exception as e:
  342. logger.warning("解析监听响应失败: %s", e)
  343. return n
  344. def clear_listen_buffer(self, rounds=LISTEN_CLEAR_ROUNDS, timeout=LISTEN_CLEAR_TIMEOUT):
  345. try:
  346. for _ in range(rounds):
  347. resps = list(self.driver.listen.steps(timeout=timeout))
  348. if not resps:
  349. break
  350. logger.debug("监听缓冲已清空")
  351. except Exception as e:
  352. logger.debug("清空监听缓冲失败: %s", e)
  353. def collect_full_page_items(self, max_steps=10):
  354. """单次循环:边滑动边收数据,到底 / 看见「下一页」即停。"""
  355. n = self.fetch_items_once(timeout=FETCH_TIMEOUT_FIRST)
  356. stagnant = 0
  357. last_scroll_y = None
  358. for step in range(max_steps):
  359. next_btn = self._find_next_btn(timeout=0.3)
  360. if self._is_next_btn_visible(next_btn):
  361. n += self.fetch_items_once(timeout=FETCH_TIMEOUT_SCROLL)
  362. return n, next_btn
  363. info = self._get_scroll_info()
  364. scroll_y = info["scrollY"]
  365. doc_h = info["docH"]
  366. view_h = info["viewH"]
  367. at_bottom = (scroll_y + view_h >= doc_h - 20)
  368. if last_scroll_y is not None and abs(scroll_y - last_scroll_y) < 8:
  369. stagnant += 1
  370. else:
  371. stagnant = 0
  372. last_scroll_y = scroll_y
  373. if at_bottom and stagnant >= 2:
  374. n += self.fetch_items_once(timeout=FETCH_TIMEOUT_SCROLL)
  375. next_btn = self._find_next_btn(timeout=2)
  376. if next_btn:
  377. self._scroll_next_into_view(next_btn)
  378. return n, next_btn
  379. logger.info("已到页面底部且未发现下一页,停止滑动")
  380. return n, None
  381. self._scroll_page_down(random.randint(400, 800))
  382. if random.random() < 0.15:
  383. self.driver.run_js(f"window.scrollBy(0, -{random.randint(60, 140)})")
  384. self.sleep(0.5, 1.5)
  385. if step % 3 == 2:
  386. n += self.fetch_items_once(timeout=FETCH_TIMEOUT_SCROLL)
  387. n += self.fetch_items_once(timeout=FETCH_TIMEOUT_SCROLL)
  388. next_btn = self._find_next_btn(timeout=3)
  389. if next_btn and not self._is_next_btn_visible(next_btn):
  390. self._scroll_next_into_view(next_btn)
  391. return n, next_btn
  392. def get_account(self):
  393. sql_account = """
  394. SELECT *
  395. FROM `retrieve_collect_equipment_account`
  396. WHERE `id` = %s
  397. and `status` = 0
  398. """
  399. account_list = self.db.select_data(sql_account, self.account_id)
  400. if not account_list:
  401. return False
  402. account_dict = account_list[0]
  403. print(account_dict)
  404. self.ip = account_dict.get("ip")
  405. self.account_name = account_dict.get("username")
  406. self.login_username = account_dict.get("phone", "")
  407. self.login_password = account_dict.get("password", "")
  408. logger.info("获取到账号: %s, ip: %s", self.account_name, self.ip)
  409. return True
  410. def disable_account(self):
  411. update_sql = f""" UPDATE `retrieve_collect_equipment_account` SET `status`= %s WHERE `name` = %s; """
  412. self.db.execute(update_sql, (1, self.account_name))
  413. def _build_search_keyword(self):
  414. parts = [p for p in (self.brand, self.product, self.product_desc) if p]
  415. return " ".join(parts).strip() or self.product
  416. def _is_logged_out(self):
  417. return bool(self.driver.ele("xpath=//*[@class='link-login']", timeout=2))
  418. def perform_jd_login(self):
  419. """
  420. 使用已有浏览器实例执行京东账号密码登录(含滑块验证码)。
  421. 成功返回 True,失败返回 False。
  422. """
  423. username = self.login_username
  424. password = self.login_password
  425. login_url = "https://passport.jd.com/new/login.aspx"
  426. self.driver.get(login_url)
  427. input_name = self.driver.ele("xpath=//input[@id='loginname']", timeout=15)
  428. if not input_name:
  429. print("未找到用户名输入框")
  430. return False
  431. input_name.input(username)
  432. time.sleep(random.uniform(1.5, 2.5))
  433. input_pass = self.driver.ele("xpath://input[@name='nloginpwd']", timeout=5)
  434. if not input_pass:
  435. print("未找到密码输入框")
  436. return False
  437. input_pass.input(password)
  438. time.sleep(random.uniform(1.5, 2.5))
  439. login_btn = self.driver.ele("xpath://a[@id='loginsubmit']", timeout=5)
  440. if not login_btn:
  441. print("未找到登录按钮")
  442. return False
  443. login_btn.click()
  444. time.sleep(random.uniform(3, 5))
  445. if not handle_jd_slider_captcha(self.driver):
  446. print("滑块验证码未通过")
  447. return False
  448. return True
  449. def _ensure_logged_in(self):
  450. """未登录时自动走登录流程(账号密码 + 滑块)。"""
  451. if not self._is_logged_out():
  452. return True
  453. logger.info("检测到未登录,开始自动登录: %s", self.account_name)
  454. ok = self.perform_jd_login()
  455. if ok and not self._is_logged_out():
  456. logger.info("自动登录成功: %s", self.account_name)
  457. return True
  458. logger.error("自动登录失败: %s", self.account_name)
  459. return False
  460. def _check_page_blocked(self):
  461. html = self.driver.html or ""
  462. if "抱歉由于访问频繁导致无法搜索" in html:
  463. logger.error("账号无法搜索(访问频繁)")
  464. self.success = False
  465. return True
  466. return False
  467. def _jump_to_page(self, target_page):
  468. """跳转到指定页码,并清空跳转前的监听残留。"""
  469. to_page_input = self.driver.ele(
  470. "xpath=//div[contains(@class,'_pagination_toPageNum_')]//input[@type='text']",
  471. timeout=3,
  472. )
  473. if not to_page_input:
  474. logger.warning("未找到跳页输入框,无法跳转到第 %s 页", target_page)
  475. return False
  476. self.clear_listen_buffer()
  477. to_page_input.input(str(target_page))
  478. self.sleep(1, 2)
  479. self.driver.actions.key_down("enter").key_up("enter")
  480. self.sleep(3, 5)
  481. self.clear_listen_buffer()
  482. logger.info("已跳转到第 %s 页", target_page)
  483. return True
  484. def _go_next_page(self, next_btn):
  485. self.clear_listen_buffer()
  486. if not self._human_click(next_btn):
  487. logger.warning("点击下一页失败")
  488. return False
  489. self.sleep(2, 4)
  490. return True
  491. def crawl(self):
  492. total = 0
  493. keyword = self._build_search_keyword()
  494. self.driver.get("https://www.jd.com/", timeout=15)
  495. time.sleep(15)
  496. if self._is_logged_out():
  497. if not self.login_password or not self.login_username:
  498. return
  499. if not self._ensure_logged_in():
  500. self.disable_account()
  501. send_text(f"京东:{self.account_name}账号登录失败")
  502. self.success = False
  503. return
  504. self.driver.get("https://www.jd.com/", timeout=15)
  505. self.sleep(3, 5)
  506. kw = quote(str(keyword or ""), safe="")
  507. self._search_kw = kw
  508. # 必须先监听再打开搜索页,否则首屏 wareList(前约 30 条)在监听开启前就返回了
  509. self._start_listen()
  510. self.driver.get(
  511. f"https://search.jd.com/Search?keyword={kw}&enc=utf-8&wq={kw}", timeout=15
  512. )
  513. self.sleep(5, 8)
  514. if self._check_page_blocked():
  515. return
  516. if not handle_jd_slider_captcha(self.driver, pause_listen=False):
  517. logger.warning("进入搜索页后滑块验证码处理失败")
  518. self.success = False
  519. return
  520. if self.start_page > 1:
  521. if not self._jump_to_page(self.start_page):
  522. logger.warning("跳页失败,将从第 1 页开始采集")
  523. self.start_page = 1
  524. logger.info(
  525. "采集页码范围: %s ~ %s(共 %s 页)",
  526. self.start_page,
  527. self.end_page,
  528. self.end_page - self.start_page + 1,
  529. )
  530. for page_no in range(self.start_page, self.end_page + 1):
  531. if self._is_logged_out():
  532. if not self._ensure_logged_in():
  533. self.success = False
  534. break
  535. self.driver.get(
  536. f"https://search.jd.com/Search?keyword={kw}&enc=utf-8&wq={kw}",
  537. timeout=15,
  538. )
  539. self.sleep(3, 5)
  540. if page_no > 1:
  541. self._jump_to_page(page_no)
  542. if not handle_jd_slider_captcha(self.driver, pause_listen=True):
  543. logger.warning("滑块验证码处理失败,停止采集")
  544. self.success = False
  545. break
  546. if self._check_page_blocked():
  547. break
  548. logger.info("===== 正在爬取第 %s 页 =====", page_no)
  549. search_ele = self.driver.ele("xpath=//div[@id='search-condition']", timeout=10)
  550. if not search_ele:
  551. logger.warning("未找到搜索结果区域,停止采集")
  552. break
  553. page_n, _ = self.collect_full_page_items()
  554. logger.info("本页监听商品条数(含可能重复): %s", page_n)
  555. total += page_n
  556. logger.info("累计监听条数: %s", total)
  557. if self.is_no_prodcut > 20:
  558. logger.info("连续无匹配商品过多,停止采集")
  559. break
  560. if page_no >= self.end_page:
  561. break
  562. next_btn = self.driver.ele("text=下一页", timeout=2)
  563. if not next_btn:
  564. logger.info("没有下一页(未找到)")
  565. break
  566. cls_str = next_btn.attr("class") or ""
  567. if "disabled" in cls_str:
  568. logger.info("没有下一页(已禁用)")
  569. break
  570. if not self._go_next_page(next_btn):
  571. break
  572. def run(self):
  573. # 检测账号
  574. if not self.get_account():
  575. logger.info("==================当前无账号可用==================")
  576. self.success = False
  577. return self.pipeline.crawl_count, self.success
  578. logger.info("获取到账号:%s,代理ip:%s", self.account_name, self.ip)
  579. # # # 每次选取账号,立马账号使用时间
  580. update_sql = f""" UPDATE `retrieve_collect_equipment_account` SET `status`= %s, `update_time`= %s WHERE `username` = %s; """
  581. self.db.execute(update_sql, (0, int(time.time()), self.account_name))
  582. try:
  583. self.init_browser()
  584. self.crawl()
  585. except Exception as e:
  586. self.success = False
  587. logger.exception("爬取异常: %s", e)
  588. self.sleep(3, 5)
  589. finally:
  590. if self.driver:
  591. self.driver.quit()
  592. self.driver = None
  593. return self.pipeline.crawl_count, self.success