no_search_single_8.py 268 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480
  1. import requests
  2. import base64
  3. import cv2
  4. import uiautomator2 as u2
  5. import time
  6. import subprocess
  7. import re
  8. import random
  9. import datetime
  10. import json
  11. from aip import AipOcr
  12. from apscheduler.schedulers.blocking import BlockingScheduler
  13. # from db_mysql import mysqlClient
  14. import threading
  15. from collections import deque
  16. import numpy as np
  17. import secrets
  18. import os
  19. import oss2
  20. import urllib.parse
  21. # import pyperclip
  22. from config import Config
  23. from logger import setup_logger
  24. import logging
  25. # from database import MySQLClient
  26. from PIL import Image
  27. from pathlib import Path
  28. from PIL import Image, ImageDraw, ImageFont
  29. # 配置日志
  30. # logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  31. setup_logger("mt_spider") # 初始化日志
  32. class SpiderMonitor(threading.Thread):
  33. """全局弹窗监控线程(增强版)"""
  34. def __init__(self, spider_instance):
  35. super().__init__(daemon=True)
  36. self.spider = spider_instance
  37. self.running = True
  38. self.pausing = threading.Event() # 主线程同步事件
  39. self.last_verification_time = 0
  40. self.verification_count = 0
  41. self.MAX_VERIFICATION_RETRY = 10
  42. self.recent_clicks = deque(maxlen=10) # 防重复点击
  43. self.logger = logging.getLogger("SpiderMonitor")
  44. # 可配置化弹窗规则
  45. self.popup_rules = {
  46. "simple": [
  47. ('//*[@text="确定"]', "点击确定"),
  48. ('//*[@text="允许"]', "点击允许"),
  49. ('//*[@text="关闭"]', "点击关闭"),
  50. ('//*[@resource-id="com.sankuai.meituan:id/close"]', "关闭按钮"),
  51. ('//*[@resource-id="com.sankuai.meituan:id/address_center_location_close"]', "关闭按钮"),
  52. ('//*[@resource-id="com.sankuai.meituan:id/location_close"]', "关闭按钮"),
  53. ('//*[@resource-id="com.sankuai.meituan:id/btn_close"]', "关闭按钮"),
  54. ],
  55. "verification": [
  56. '//*[contains(@text, "验证")]',
  57. '//*[contains(@text, "滑块")]',
  58. '//*[contains(@text, "依次点击")]',
  59. '//*[contains(@text, "请点击")]',
  60. '//*[contains(@text, "拖动滑块刚")]', #这个需要拖动滑块至最右边,然后再截图
  61. '//*[contains(@text, "请输入图片中的内容")]',
  62. '//*[contains(@text, "用最短线连接")]',
  63. '//*[contains(@text, "请按语序依次点击")]',
  64. '//*[contains(@text, "请向右滑动滑块")]',
  65. '//*[contains(@text, "请拖动下方滑块完成拼图")]',
  66. '//*[contains(@resource-id, "captcha")]'
  67. ]
  68. }
  69. def run(self):
  70. while self.running:
  71. try:
  72. handled = self.check_and_handle_popup()
  73. time.sleep(2 if handled else 1)
  74. except Exception as e:
  75. self.logger.exception("监控线程异常: %s", e)
  76. time.sleep(3)
  77. def _is_recent_click(self, xpath):
  78. """防止重复点击同一个弹窗"""
  79. key = f"{xpath}_{int(time.time())}"
  80. if key in self.recent_clicks:
  81. return True
  82. self.recent_clicks.append(key)
  83. return False
  84. def check_and_handle_popup(self):
  85. d = self.spider.d
  86. # 1. 处理简单弹窗
  87. for xpath, desc in self.popup_rules["simple"]:
  88. if d.xpath(xpath).exists and not self._is_recent_click(xpath):
  89. self.logger.info("检测到弹窗: %s", desc)
  90. d.xpath(xpath).click()
  91. return True
  92. # 2. 处理验证码弹窗
  93. for xpath in self.popup_rules["verification"]:
  94. if d.xpath(xpath).exists:
  95. now = time.time()
  96. if now - self.last_verification_time < 30:
  97. return False # 30秒内不重复触发
  98. self.last_verification_time = now
  99. self.verification_count += 1
  100. self.logger.warning("验证码弹窗触发,等待人工处理...")
  101. if self.verification_count > self.MAX_VERIFICATION_RETRY:
  102. self.logger.error("验证码重试超限,终止任务")
  103. self.spider.stop_all()
  104. return True
  105. self.pausing.set() # 通知主线程暂停
  106. # d.toast.show("需要人工处理验证码", 120)
  107. # 等待人工处理
  108. start = time.time()
  109. # while time.time() - start < 120*60:
  110. # if not d.xpath(xpath).exists:
  111. # self.logger.info("验证码已处理")
  112. # d.toast.show("验证完成", 2)
  113. # self.pausing.clear() # 放行主线程
  114. # return True
  115. # time.sleep(5)
  116. while True:
  117. if not d.xpath(xpath).exists:
  118. self.logger.info("验证码已处理")
  119. # d.toast.show("验证完成", 2)
  120. self.pausing.clear() # 放行主线程
  121. return True
  122. time.sleep(5)
  123. #self.logger.warning("验证码超时,重启APP")
  124. #self.spider.restart_app()
  125. #return True
  126. # 3. 处理广告弹窗(点击右上角)
  127. if d.xpath('//*[contains(@text, "广告")]').exists:
  128. w, h = d.info['displayWidth'], d.info['displayHeight']
  129. d.click(w - 50, 50)
  130. self.logger.info("关闭广告弹窗")
  131. return True
  132. return False
  133. def stop(self):
  134. self.running = False
  135. class MTScreenshot:
  136. def __init__(self, d, oss_config, search_key, scroll_times=4, compress_quality=7, resize_ratio=0.8):
  137. # 接收外部已连接好的u2设备实例
  138. self.d = d
  139. self.search_key = search_key # 添加这行
  140. # 启动全局弹窗监控
  141. self.monitor = SpiderMonitor(self)
  142. self.monitor.start()
  143. self.loggerMT = logging.getLogger()
  144. # 日志初始化
  145. self.logger = self._init_logger()
  146. # OSS配置与初始化(核心配置,无冗余)
  147. self.oss_config = oss_config
  148. self.oss_bucket = self._init_oss_bucket()
  149. # 截图核心参数
  150. self.scroll_times = scroll_times
  151. self.compress_quality = compress_quality
  152. self.resize_ratio = resize_ratio
  153. # self.title_xpaths = [
  154. # '//*[@resource-id="com.jd.lib.productdetail.feature:id/db"]',
  155. # '//*[@resource-id="com.jd.lib.productdetail.feature:id/cx"]',
  156. # '//*[@resource-id="com.jd.lib.productdetail.feature:id/cj"]'
  157. # ]
  158. def _init_logger(self):
  159. # 极简日志配置,仅保留必要输出
  160. logger = logging.getLogger("mt_screenshot")
  161. logger.setLevel(logging.INFO)
  162. logger.handlers.clear()
  163. handler = logging.StreamHandler()
  164. handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
  165. logger.addHandler(handler)
  166. return logger
  167. def _init_oss_bucket(self):
  168. # 仅做OSS配置校验和Bucket连接,无额外功能
  169. if not all([self.oss_config.get("access_key_id"),
  170. self.oss_config.get("access_key_secret"),
  171. self.oss_config.get("endpoint"),
  172. self.oss_config.get("bucket_name")]):
  173. self.logger.warning("OSS配置不完整,无法上传")
  174. return None
  175. try:
  176. auth = oss2.Auth(self.oss_config["access_key_id"], self.oss_config["access_key_secret"])
  177. bucket = oss2.Bucket(auth, self.oss_config["endpoint"], self.oss_config["bucket_name"])
  178. bucket.get_bucket_info() # 验证连接
  179. self.logger.info("OSS Bucket连接成功")
  180. return bucket
  181. except Exception as e:
  182. self.logger.error(f"OSS Bucket连接失败: {e}")
  183. return None
  184. def _upload_to_oss(self, local_path):
  185. # 极简上传逻辑,仅返回OSS URL或None
  186. if not self.oss_bucket or not os.path.exists(local_path):
  187. return None
  188. file_name = os.path.basename(local_path)
  189. safe_name = re.sub(r'[^\w\.\-]', '_', file_name)
  190. oss_key = f"{self.oss_config.get('oss_prefix', 'scrape_data/')}{safe_name}"
  191. try:
  192. oss2.resumable_upload(self.oss_bucket, oss_key, local_path)
  193. # 生成并返回完整OSS URL
  194. oss_file_url = f"https://{self.oss_config['bucket_name']}.{self.oss_config['endpoint']}/{urllib.parse.quote(oss_key, safe='/')}"
  195. self.logger.info(f"OSS上传成功: {oss_file_url}")
  196. return oss_file_url
  197. except Exception as e:
  198. self.logger.error(f"OSS上传失败: {e}")
  199. return None
  200. # def _get_title(self):
  201. # # 仅提取标题,无冗余逻辑
  202. # for xpath in self.title_xpaths:
  203. # elem = self.d.xpath(xpath)
  204. # if elem.exists:
  205. # info = elem.info
  206. # title = (info.get("contentDescription") or info.get("content-desc") or info.get("text") or "").strip()
  207. # return title[:50] # 限制标题长度,避免文件名过长
  208. # return ""
  209. def safe_exec(self, func, *args, **kwargs):
  210. """
  211. 万能安全壳:执行 func 前检查验证码,
  212. 若监控线程已置位 pausing,则一直阻塞直到放行。
  213. """
  214. while self.monitor.pausing.is_set():
  215. time.sleep(1)
  216. # 执行真正逻辑
  217. return func(*args, **kwargs)
  218. def _get_title(self):
  219. # try:
  220. # title = self.d.xpath(
  221. # '//android.widget.ScrollView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView').text
  222. # except:
  223. # title = self.d.xpath(
  224. # '//android.widget.ScrollView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.TextView').text
  225. # title = self.d.xpath('//*[contains(@text, "舒肝颗粒")]').text
  226. def _inner():
  227. temp_search_key = self.search_key
  228. if "999" in self.search_key:
  229. if self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  230. temp_search_key = self.search_key.replace("999皮炎平", "")
  231. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  232. temp_search_key = self.search_key.replace("999必无忧", "")
  233. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  234. temp_search_key = self.search_key.replace("999必无忧", "")
  235. elif self.search_key == '999速复康布洛芬缓释胶囊':
  236. temp_search_key = self.search_key.replace("999速复康", "")
  237. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  238. temp_search_key = self.search_key.replace("999选平", "")
  239. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  240. temp_search_key = self.search_key.replace("999皮炎平", "")
  241. else:
  242. temp_search_key = self.search_key.replace("999", "")
  243. else:
  244. if self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  245. temp_search_key = self.search_key.replace("史达功", "")
  246. temp_search_key = temp_search_key.replace("120", "")
  247. elif self.search_key == '三九胃泰养胃舒颗粒8袋':
  248. temp_search_key = self.search_key.replace("三九胃泰", "")
  249. temp_search_key = temp_search_key.replace("8袋", "")
  250. elif self.search_key == '今维多赐多康牌蛋白粉':
  251. temp_search_key = self.search_key.replace("今维多", "")
  252. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  253. temp_search_key = self.search_key.replace("佳美舒", "")
  254. temp_search_key = temp_search_key.replace("4", "")
  255. elif self.search_key == '三九胃泰颗粒20g*10':
  256. temp_search_key = self.search_key.replace("20g*10", "")
  257. elif self.search_key == '三九胃泰颗粒20g*6袋':
  258. temp_search_key = self.search_key.replace("20g*6袋", "")
  259. elif self.search_key == '顺峰康王酮康他索乳膏':
  260. temp_search_key = self.search_key.replace("顺峰康王", "")
  261. if self.search_key == '999糠酸莫米松凝胶15':
  262. temp_search_key = temp_search_key.replace("15", "")
  263. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  264. temp_search_key = temp_search_key.replace("30", "")
  265. elif self.search_key == '999复方感冒灵颗粒15':
  266. temp_search_key = temp_search_key.replace("15", "")
  267. elif self.search_key == '999复方金银花颗粒10g':
  268. temp_search_key = temp_search_key.replace("10g", "")
  269. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  270. temp_search_key = temp_search_key.replace("15g*15袋/盒", "")
  271. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  272. temp_search_key = temp_search_key.replace("6粒", "")
  273. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  274. temp_search_key = temp_search_key.replace("50", "")
  275. elif self.search_key == '999止泻利颗粒15g*8':
  276. temp_search_key = temp_search_key.replace("15g*8", "")
  277. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  278. temp_search_key = temp_search_key.replace("30", "")
  279. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  280. temp_search_key = temp_search_key.replace("15g", "")
  281. elif self.search_key == '999复方苦参肠炎康片12片':
  282. temp_search_key = temp_search_key.replace("12片", "")
  283. elif self.search_key == '999强力枇杷露16袋':
  284. temp_search_key = temp_search_key.replace("16袋", "")
  285. elif self.search_key == '999三蛇胆川贝膏138':
  286. temp_search_key = temp_search_key.replace("138", "")
  287. elif self.search_key == '999强力枇杷露120ml':
  288. temp_search_key = temp_search_key.replace("120ml", "")
  289. elif self.search_key == '999强力枇杷露150ml':
  290. temp_search_key = temp_search_key.replace("150ml", "")
  291. elif self.search_key == '999抗病毒口服液10ml*10':
  292. temp_search_key = temp_search_key.replace("10ml*10", "")
  293. elif self.search_key == '999抗病毒口服液10ml*12':
  294. temp_search_key = temp_search_key.replace("10ml*12", "")
  295. elif self.search_key == '999糠酸莫米松乳膏10g支':
  296. temp_search_key = temp_search_key.replace("10g支", "")
  297. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  298. temp_search_key = temp_search_key.replace("20g", "")
  299. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  300. temp_search_key = temp_search_key.replace("6g", "")
  301. elif self.search_key == '999壮骨关节丸6g*20':
  302. temp_search_key = temp_search_key.replace("6g*20", "")
  303. elif self.search_key == '999正天丸6g*15':
  304. temp_search_key = temp_search_key.replace("6g*15", "")
  305. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  306. temp_search_key = temp_search_key.replace("20", "")
  307. elif self.search_key == '999糠酸莫米松凝胶10':
  308. temp_search_key = temp_search_key.replace("10", "")
  309. elif self.search_key == '999板蓝根颗粒10g*20':
  310. temp_search_key = temp_search_key.replace("10g*20", "")
  311. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  312. temp_search_key = temp_search_key.replace("10粒", "")
  313. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  314. temp_search_key = temp_search_key.replace("12粒", "")
  315. elif self.search_key == '999咽炎片0.26g*12片*2板':
  316. temp_search_key = temp_search_key.replace("0.26g*12片*2板", "")
  317. elif self.search_key == '999小儿止咳糖浆120':
  318. temp_search_key = temp_search_key.replace("120", "")
  319. elif self.search_key == '999小儿止咳糖浆225':
  320. temp_search_key = temp_search_key.replace("225", "")
  321. elif self.search_key == '999小儿感冒颗粒6g*10':
  322. temp_search_key = temp_search_key.replace("6g*10", "")
  323. elif self.search_key == '999小儿感冒颗粒6g*24':
  324. temp_search_key = temp_search_key.replace("6g*24", "")
  325. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  326. temp_search_key = temp_search_key.replace("6g*10袋", "")
  327. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  328. temp_search_key = temp_search_key.replace("6g*20袋", "")
  329. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  330. temp_search_key = temp_search_key.replace("8g*10袋", "")
  331. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  332. temp_search_key = temp_search_key.replace("2.5g*10袋", "")
  333. # elif self.search_key == '三九胃泰颗粒':
  334. # self.search_key = '三九胃泰' #兼容三九胃泰 温胃舒颗粒
  335. print(f'获取商品title时的搜索关键字:{temp_search_key}')
  336. # title = self.d.xpath(f'//*[contains(@text, "{self.search_key}")]').text
  337. # 初始化
  338. drugs_name = ''
  339. specifications = ''
  340. title = ''
  341. # 循环的获取title为了有时间来处理人机验证
  342. for m in range(1, 6000):
  343. if self.d.xpath(f'//*[contains(@text, "{temp_search_key}")]').exists:
  344. title = self.safe_exec(
  345. lambda: self.d.xpath(f'//*[contains(@text, "{temp_search_key}")]').text
  346. )
  347. self.loggerMT.info(f"第{m}次获取title成功")
  348. print(f"第{m}次获取title成功")
  349. break
  350. else:
  351. time.sleep(3)
  352. # return drugs_name, specifications
  353. # drugs_name = ''
  354. # specifications = ''
  355. # try:
  356. # title_xpath = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView'
  357. # title_xpath_2 = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView'
  358. # if self.d.xpath(title_xpath).exists:
  359. # title = self.d.xpath(title_xpath).text
  360. # print(f"title_xpath获取的title={title}")
  361. # if temp_search_key not in title:
  362. # return drugs_name, specifications
  363. # elif self.d.xpath(title_xpath_2).exists:
  364. # title = self.d.xpath(title_xpath_2).text
  365. # print(f"title_xpath_2获取的title={title}")
  366. # if temp_search_key not in title:
  367. # return drugs_name, specifications
  368. # else:
  369. # print('title_xpath不存在,请确认')
  370. # return drugs_name, specifications
  371. # # title = self.d.xpath(f'//*[contains(@text, "{temp_search_key}")]').text
  372. # except Exception as e:
  373. # print(f"发生异常: {e}")
  374. # return drugs_name, specifications
  375. # 奇怪:有的时候title取出来的记过第一位会多一个0
  376. # title = self.safe_exec(self.d.xpath(f'//*[contains(@text, "{self.search_key}")]').text)
  377. # title = self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView').text
  378. title = title[1:] if title.startswith('0') else title
  379. print(f'获取到药品标题:{title}')
  380. # 从里面匹配出药品名和规格
  381. # drugs_name
  382. # specifications
  383. # match = re.search(r'([^\d]+)([\d\D]+)', title)
  384. if self.search_key == '999赐多康大豆':
  385. return title, '1罐'
  386. if self.search_key == "999感冒清热颗粒":
  387. match = re.search(r'(\[[^\]]+\])(.+?)(\d+.*)', title)
  388. else:
  389. match = re.match(r'(\[[^\]]+\])(.*?)\s*((?:\d+\S*|\(.+))$', title)
  390. if match:
  391. # drugs_name = match.group(1).strip() + match.group(2).strip()
  392. drugs_name = title
  393. specifications = match.group(3).strip()
  394. print("药品名:", drugs_name)
  395. print("规格:", specifications)
  396. # print('完整药名:', drugs_name + specifications)
  397. return drugs_name #, specifications
  398. else:
  399. if title == '999抗病毒口服液10ml*12' or title == '999抗病毒口服液':
  400. drugs_name = title
  401. specifications = '10ml*12支/盒'
  402. return drugs_name # , specifications
  403. elif title == '999抗病毒口服液10ml*10':
  404. drugs_name = title
  405. specifications = '10ml*10支/盒'
  406. return drugs_name #, specifications
  407. elif title == '999小柴胡颗粒':
  408. drugs_name = title
  409. specifications = '10g*9袋/盒'
  410. return drugs_name #, specifications
  411. elif title == '999养胃舒颗粒':
  412. drugs_name = title
  413. specifications = '10g*10袋/盒'
  414. return drugs_name #, specifications
  415. elif title == '三九胃泰胶囊':
  416. drugs_name = title
  417. specifications = '0.5g*24粒/盒'
  418. return drugs_name #, specifications
  419. elif title == '999补脾益肠丸':
  420. drugs_name = title
  421. specifications = '6g*15袋/盒'
  422. return drugs_name #, specifications
  423. elif title == '999感冒灵颗粒':
  424. drugs_name = title
  425. specifications = '10g*9袋/盒'
  426. return drugs_name #, specifications
  427. elif title == '999感冒灵胶囊':
  428. drugs_name = title
  429. specifications = '0.5g*12粒/盒'
  430. return drugs_name #, specifications
  431. else:
  432. print("没有匹配到预期格式")
  433. drugs_name = title
  434. specifications = ''
  435. return drugs_name #, specifications
  436. # 用 safe_exec 包装内部逻辑,确保验证码阻塞
  437. return self.safe_exec(_inner)
  438. def _merge_screenshots(self, screens):
  439. # 仅拼接截图,无额外功能
  440. if len(screens) == 1:
  441. return screens[0].convert('RGB')
  442. rgb_screens = [s.convert('RGB') for s in screens]
  443. total_width = rgb_screens[0].width
  444. total_height = sum(s.height for s in rgb_screens)
  445. merged_img = Image.new('RGB', (total_width, total_height))
  446. y_offset = 0
  447. for img in rgb_screens:
  448. merged_img.paste(img, (0, y_offset))
  449. y_offset += img.height
  450. return merged_img
  451. def get_oss_url(self):
  452. """核心方法:截图+临时本地保存+上传OSS+上传成功删本地文件+返回OSS URL,可直接赋值给oss_file"""
  453. local_file_path = None
  454. try:
  455. # 1. 提取标题
  456. title = self._get_title()
  457. self.logger.info(f"获取标题: {title[:20]}..." if title else "未获取到标题")
  458. # 2. 生成本地文件路径
  459. timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
  460. safe_title = re.sub(r'[\\/*?:"<>|]', '_', title)
  461. local_dir = "../scrape_data"
  462. os.makedirs(local_dir, exist_ok=True)
  463. local_file_path = os.path.join(local_dir, f"{timestamp}_{safe_title}.jpg")
  464. # 3. 滚动截图
  465. screen_list = [self.d.screenshot()]
  466. w, h = self.d.window_size()
  467. for i in range(self.scroll_times):
  468. #可能滑动距离太短,截不到店名。原本是0.8
  469. # self.d.swipe(w // 2, h * 0.9, w // 2, h * 0.1, duration=random.uniform(0.6, 1.2))
  470. self.d.swipe(w // 2, h * 0.85, w // 2, h * 0.15, # 滑动到15%
  471. duration=random.uniform(0.8, 1.5))
  472. time.sleep(random.uniform(2.0, 4.0))
  473. screen_list.append(self.d.screenshot())
  474. if self.d(textContains='商家服务').exists:
  475. #看情况是否需要补滑
  476. break
  477. # # ========== 自动处理“是否存储图像”弹窗 ==========
  478. # # 检测弹窗是否存在(根据弹窗的文本/控件ID定位)
  479. # # 通过“是否存储图像”文本定位弹窗
  480. # # if self.d(text="是否存储图像").exists(timeout=2):
  481. # # # 点击“取消”(不需要系统存储截图)
  482. # # self.d(text="取消").click(timeout=2)
  483. # # self.logger.info("已自动关闭“是否存储图像”弹窗")
  484. #
  485. # #出现标题 break
  486. # ========== 滑动截图完成后,滑回初始位置 ==========
  487. # self.logger.info("开始滑回初始位置")
  488. #
  489. # for i in range(self.scroll_times):
  490. # # 反向滑动(与正向滑动方向相反)
  491. # self.d.swipe_ext('down', 0.8)
  492. # time.sleep(random.uniform(1.0, 2.0))
  493. # print(f"第{i+1}次反向滑动,已滑回部分距离")
  494. # self.logger.info("✅ 已滑回初始页面位置")
  495. # 4. 拼接+压缩+保存
  496. merged_img = self._merge_screenshots(screen_list)
  497. if 0.1 < self.resize_ratio < 1.0:
  498. new_size = (int(merged_img.width * self.resize_ratio), int(merged_img.height * self.resize_ratio))
  499. resample_mode = Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS
  500. merged_img = merged_img.resize(new_size, resample_mode)
  501. #临时保存到本地
  502. merged_img.save(local_file_path, format='JPEG', quality=self.compress_quality)
  503. merged_img.close() #释放长图句柄
  504. self.logger.info(f"临时本地保存: {local_file_path}")
  505. # 5. 上传OSS
  506. oss_url = self._upload_to_oss(local_file_path)
  507. # 6. 核心:OSS上传成功后,删除本地临时文件
  508. if oss_url is not None:
  509. try:
  510. #先不删除,检查还有没有问题
  511. # os.remove(local_file_path)
  512. self.logger.info(f"✅ OSS上传成功,已删除本地临时文件: {local_file_path}")
  513. # 若本地目录为空,可删除目录(按需开启)
  514. # if not os.listdir(local_dir):
  515. # os.rmdir(local_dir)
  516. # self.logger.info(f"本地目录{local_dir}为空,已删除")
  517. except Exception as e:
  518. self.logger.warning(f"⚠️ OSS上传成功,但删除本地文件失败: {e}")
  519. return oss_url
  520. except Exception as e:
  521. self.logger.error(f"截图/上传失败: {e}")
  522. return None
  523. def get_access_token():
  524. AppKey = "tRK2RhyItCSh6BzyT4CNVXQa"
  525. AppSrcret = "TDgKiPo94i2mOM1sDqOuDnlcK1bG66jh"
  526. token_url = 'https://aip.baidubce.com/oauth/2.0/token'
  527. url = f"{token_url}?grant_type=client_credentials&client_id={AppKey}&client_secret={AppSrcret}"
  528. payload = ""
  529. headers = {
  530. 'Content-Type': 'application/json',
  531. 'Accept': 'application/json'
  532. }
  533. response = requests.request("POST", url, headers=headers, data=payload)
  534. try:
  535. return response.json()['access_token']
  536. except:
  537. return None
  538. def get_mysql():
  539. """
  540. 建立并返回一个到数据库的连接对象
  541. """
  542. import pymysql
  543. return pymysql.connect(
  544. host = Config.DB_HOST, #"localhost", # 修改后的主机
  545. port = Config.DB_PORT, #3306, # 添加端口号
  546. user = Config.DB_USER, #'root', # 修改后的用户名
  547. password = Config.DB_PASSWORD, # 修改后的密码
  548. db = Config.DB_NAME, #"drug_data", # 修改后的数据库名
  549. charset='utf8mb4'
  550. )
  551. class MT:
  552. def __init__(self, key):
  553. # self.package_name = 'com.sankuai.meituan'
  554. self.package_name = Config.PACKAGE_NAME
  555. self.access_token = get_access_token()
  556. self.city2province = self.get_city_info()
  557. self.APP_ID = '116857964'
  558. self.API_KEY = '1gAzACJOAr7BeILKqkqPOETh'
  559. self.SECRET_KEY = 'ZNArANb9GwJYgLKg4EfYhukKBfPdl1n3'
  560. self.client = AipOcr(self.APP_ID, self.API_KEY, self.SECRET_KEY)
  561. # host = Config.DB_HOST #"localhost"
  562. # user = Config.DB_USER #"root"
  563. # password = Config.DB_PASSWORD #"dfwy2025"
  564. # database = Config.DB_NAME #"drug_data"
  565. # port = Config.DB_PORT#3306
  566. # print(f'数据库配置:host:{host},user:{user},password:{password},database:{database},port:{port}')
  567. self.table_name = Config.DB_TABLE #"mt_drug_middle"
  568. self.shop_table_name = Config.DB_SHOP_TABLE
  569. # print(f'数据库表名:table_name:{self.table_name},shop_table_name:{self.shop_table_name}')
  570. # self.mysql_client = mysqlClient(host, user, password, database, port)
  571. self.loggerMT = logging.getLogger()
  572. self.search_key = key # 参苓健脾胃颗粒 舒肝颗粒 清肺化痰丸 香砂平胃颗粒
  573. self.unrelated_data = 0 # 无关数据数量
  574. self.shop_data_num = 0 # 店铺数据数量
  575. def stop_app(self):
  576. self.d.app_stop(self.package_name)
  577. time.sleep(5)
  578. def start_app(self):
  579. self.d.app_start(self.package_name)
  580. time.sleep(5)
  581. def restart_app(self):
  582. """
  583. 重启app
  584. :return:
  585. """
  586. self.stop_app()
  587. self.start_app()
  588. @staticmethod
  589. def get_sleep_time():
  590. # return random.randint(5, 8)
  591. return random.randint(1, 3)
  592. @staticmethod
  593. def get_current_date():
  594. return datetime.datetime.now().strftime('%Y/%m/%d')
  595. @staticmethod
  596. def get_city_info():
  597. """
  598. 获取所有的省市数据
  599. :return:
  600. """
  601. file_path = '../kailin_city.json'
  602. with open(file_path, 'r', encoding='utf-8') as f:
  603. data = json.load(f)
  604. province = {province_one["id"]: province_one for province_one in data['province']}
  605. city2province = dict()
  606. city = data['city']
  607. for city_one in city:
  608. name = city_one['name']
  609. pid = city_one['pid']
  610. if len(str(pid)) > 2:
  611. pid = int(re.match('^\d{2}', str(pid)).group())
  612. city2province[name] = province[pid]['name']
  613. return city2province
  614. def get_shop_name(self):
  615. """
  616. 获取店铺名
  617. :return:
  618. """
  619. try:
  620. shop_name = self.d.xpath(
  621. '//android.widget.ScrollView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.widget.FrameLayout[1]/android.widget.TextView').text
  622. print(f'获取到店铺名:{shop_name}')
  623. return shop_name
  624. except:
  625. try:
  626. shop_name = self.d.xpath(
  627. '//android.widget.ScrollView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()-1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.widget.FrameLayout[1]/android.widget.TextView').text
  628. print(f'获取到店铺名2:{shop_name}')
  629. return shop_name
  630. except Exception as e:
  631. #点击店铺曲获取店铺名称
  632. print("点击店铺进入后获取店铺名称")
  633. self.enter_shop()
  634. shop_xpath = '//*[@resource-id="com.sankuai.meituan:id/layout_header_view"]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]//android.widget.FrameLayout[2]/android.widget.FrameLayout[1]/android.widget.TextView'
  635. if self.d.xpath(shop_xpath).exists:
  636. shop_name = self.d.xpath(shop_xpath).text
  637. self.swipe_back(1)
  638. return shop_name
  639. else:
  640. print(f'获取店铺名出错:{e}')
  641. return None
  642. def get_qualification_number(self):
  643. """
  644. 获取资质编号
  645. :return:
  646. """
  647. try:
  648. qualification_number_str = self.d.xpath(
  649. '//*[@resource-id="com.sankuai.meituan:id/mil_container"]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.widget.TextView[2]').text
  650. qualification_number = qualification_number_str.strip('资质编号:').strip()
  651. return qualification_number
  652. except:
  653. return None
  654. def get_shop_address(self):
  655. try:
  656. xpath = '//*[@resource-id="com.sankuai.meituan:id/wm_sc_drug_shop_content_mrn_container_id_2"]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.TextView'
  657. if self.d.xpath(xpath).exists:
  658. shop_address = self.d.xpath(xpath).text
  659. print(f'111-获取到店铺地址:{shop_address}')
  660. if '发货时间' in shop_address:
  661. print(f'店铺地址包含发货时间,再次获取店铺地址')
  662. xpath2 = '//*[@resource-id="com.sankuai.meituan:id/wm_sc_drug_shop_content_mrn_container_id_2"]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.TextView'
  663. if self.d.xpath(xpath2).exists:
  664. shop_address = self.d.xpath(xpath2).text
  665. print(f'222-获取到店铺地址:{shop_address}')
  666. else:
  667. print(f'222-xpath2获取店铺地址失败')
  668. else:
  669. shop_address = ''
  670. print(f'333-获取到店铺地址:{shop_address}')
  671. return shop_address
  672. except:
  673. print(f'获取店铺地址出错-get_shop_address')
  674. return None
  675. def enter_detail(self):
  676. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/recycler"]/android.widget.FrameLayout[1]').click()
  677. time.sleep(self.get_sleep_time())
  678. def save_to_database(self, data):
  679. print(f'保存数据到数据库:{data}')
  680. # 连接数据库
  681. conn = get_mysql()
  682. # 创建游标对象
  683. cur = conn.cursor()
  684. # add_sql = "insert into delete_friend_table(delete_user_name,delete_user_id,delete_content,delete_time) value(%s,%s,%s,%s)"
  685. add_sql = f"""
  686. INSERT INTO {self.table_name}
  687. (product, min_price, manufacture_date, expiry_date, shop, business_license_company, province, city, manufacturer, specification, approval_number, product_link, scrape_date, scrape_province, availability, credit_code, platform, search_key, sales, inventory, snapshot_url)
  688. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  689. """
  690. # cur.execute(add_sql, (data['product'], data['min_price'], data['manufacture_date'], data['expiry_date'], data['shop'], data['business_license_company'],data['province'], data['city'], data['manufacturer'], data['specification'], data['approval_number'], data['product_link'], self.get_current_date(), data['scrape_province'], data['availability'], data['credit_code'], data['platform']))
  691. cur.execute(add_sql, (data['product'], data['min_price'], data['manufacture_date'], data['expiry_date'], data['shop'], data['business_license_company'],data['province'], data['city'], data['manufacturer'], data['specification'], data['approval_number'], data['product_link'], data['scrape_date'], data['scrape_province'], data['availability'], data['credit_code'], data['platform'], data['search_key'], data['sales'], data['inventory'], data['snapshot_url']))
  692. conn.commit() # 提交数据
  693. #self.mysql_client.insert(self.table_name, data)
  694. print(f"存入数据库成功")
  695. def save_shop_info_to_database(self, data):
  696. print(f'保存店铺数据到数据库:{data}')
  697. # 连接数据库
  698. conn = get_mysql()
  699. # 创建游标对象
  700. cur = conn.cursor()
  701. add_sql = f"""
  702. INSERT INTO {self.shop_table_name}
  703. (shop, contact_address, qualification_number, business_license_company, business_license_address, scrape_date, platform)
  704. VALUES (%s, %s, %s, %s, %s, %s, %s)
  705. """
  706. cur.execute(add_sql, (data['shop'], data['contact_address'], data['qualification_number'], data['business_license_company'], data['business_license_address'], data['scrape_date'], data['platform']))
  707. conn.commit() # 提交数据
  708. #self.mysql_client.insert(self.shop_table_name, data)
  709. print(f'存入店铺信息到数据库成功')
  710. def swipe_up(self):
  711. """
  712. 上滑
  713. :return:
  714. """
  715. screen_width = self.d.info['displayWidth']
  716. screen_height = self.d.info['displayHeight']
  717. duration_rate = random.uniform(0, 0.3)
  718. self.d.swipe(screen_width // 2, screen_height - 100, screen_width // 2, 100, duration=duration_rate)
  719. no = random.uniform(0, 1)
  720. if no > 0.85:
  721. # 有的时候卡着 再稍微往上滑一点点
  722. self.d.swipe_ext("up", 0.1)
  723. time.sleep(self.get_sleep_time())
  724. def swipe_back(self, no):
  725. """
  726. 返回
  727. :param no: 回退次数
  728. :return:
  729. """
  730. for idx in range(no):
  731. self.d.press('back')
  732. time.sleep(self.get_sleep_time())
  733. def drug_price(self):
  734. """
  735. 获取药品价格
  736. :return:
  737. """
  738. try:
  739. price_str = self.d.xpath('//*[starts-with(@text,"¥")]').text
  740. price = float(re.search(r'[\d\.]+', price_str).group())
  741. print(f'获取到价格:{price}')
  742. return price
  743. except Exception as e:
  744. print(f'提取价格出错-->{e}')
  745. return None
  746. def drug_sale_num(self):
  747. """
  748. 获取药品销量
  749. :return:
  750. """
  751. try:
  752. sales_element = self.d.xpath('//*[starts-with(@text,"已售")]')
  753. if sales_element.exists:
  754. sales_num_str = self.d.xpath('//*[starts-with(@text,"已售")]').text
  755. sales_num_str = sales_num_str.replace("已售", "").strip()
  756. # price = float(re.search(r'[\d\.]+', price_str).group())
  757. print(f'获取到已售数量:{sales_num_str}')
  758. return sales_num_str
  759. return None
  760. except Exception as e:
  761. print(f'提取已售数量出错-->{e}')
  762. return None
  763. def restart_uiautomator_services(self, device_id):
  764. """
  765. 重启atx的uiautomator 服务
  766. :param device_id:
  767. :return:
  768. """
  769. stop_uiautomator_services = f'adb -s {device_id} shell /data/local/tmp/atx-agent server -d --stop'
  770. start_uiautomator_services = f'adb -s {device_id} shell /data/local/tmp/atx-agent server -d'
  771. # result = subprocess.run(stop_uiautomator_services, capture_output=True, text=True, shell=True)
  772. # print(result.stdout)
  773. subprocess.run(stop_uiautomator_services, capture_output=True, text=True, shell=True)
  774. time.sleep(self.get_sleep_time())
  775. subprocess.run(start_uiautomator_services, capture_output=True, text=True, shell=True)
  776. time.sleep(self.get_sleep_time())
  777. # def connect_devices(self, device_id):
  778. # """
  779. # 连接设备
  780. # :return:
  781. # """
  782. # try:
  783. # self.d = u2.connect_usb(device_id)
  784. # # 设置隐形等待时间
  785. # # self.d.implicitly_wait(5)
  786. # self.restart_uiautomator_services(device_id)
  787. # print(f'连接到设备:{device_id}')
  788. # except Exception as e:
  789. # print(f'{device_id} 连接错误: {e}')
  790. # raise Exception(e)
  791. def connect_devices(self, device_id):
  792. """
  793. 连接设备
  794. :return:
  795. """
  796. try:
  797. self.d = u2.connect_usb(device_id)
  798. self.restart_uiautomator_services(device_id)
  799. self.oss_config = {
  800. "access_key_id": 'LTAI5tDwjfteBvivYN41r8sJ',
  801. "access_key_secret": 'yowuOGi2nYYnrqGpO3qcz94C4brcPp',
  802. "endpoint": "oss-cn-shenzhen.aliyuncs.com", # 例:oss-cn-beijing.aliyuncs.com
  803. "bucket_name": "zhijiayun-jiansuo",
  804. "oss_prefix": "scrape_data/" # OSS中存放截图的前缀(虚拟文件夹)
  805. }
  806. # jd_screenshot_ins = JDScreenshot(
  807. # d=self.d, # 传入你已连接好的设备实例
  808. # oss_config=self.oss_config,
  809. # scroll_times=2, # 可选,自定义滚动次数
  810. # compress_quality=8, # 可选,自定义压缩质量
  811. # resize_ratio=0.9 # 可选,自定义缩放比例
  812. # )
  813. print(f'连接到设备:{device_id}')
  814. self.loggerMT.info(f'连接到设备:{device_id}')
  815. except Exception as e:
  816. print(f'{device_id} 连接错误: {e}')
  817. self.loggerMT.info(f'{device_id} 连接错误: {e}')
  818. raise Exception(e)
  819. def get_ocr_res(self, img):
  820. try:
  821. #img地址
  822. print(f'开始识别图片:{img}')
  823. request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license"
  824. # 二进制方式打开图片文件
  825. f = open(img, 'rb')
  826. img = base64.b64encode(f.read())
  827. params = {"image": img}
  828. # access_token = get_access_token()
  829. request_url = request_url + "?access_token=" + self.access_token
  830. headers = {'content-type': 'application/x-www-form-urlencoded'}
  831. response = requests.post(request_url, data=params, headers=headers)
  832. if response:
  833. res = response.json()
  834. new_dic = dict()
  835. for ite in res['words_result'].keys():
  836. new_dic[ite] = res['words_result'][ite]['words']
  837. print('资质数据信息', new_dic)
  838. return new_dic
  839. else:
  840. return None
  841. except:
  842. return None
  843. def remove_watermark(self, img_path):
  844. """
  845. 图片去水印(将水印部分变成白色背景)并将数据转化为二进制数据
  846. :param img_path: 图片路径
  847. :return: 二进制图片数据
  848. """
  849. img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), -1)
  850. endswith = os.path.splitext(img_path)[1]
  851. new = np.clip(1.4057577998008846 * img - 38.33089999653017, 0, 255).astype(np.uint8)
  852. _, img_binary = cv2.imencode(endswith, new)
  853. return img_binary
  854. def get_ocr_res_image(self, img):
  855. try:
  856. image = self.remove_watermark(img)
  857. # image_file = open(img,'wb')
  858. # image_file.write(image)
  859. # res_image = self.client.basicAccurate(image) # 高精度
  860. res_image = self.client.basicGeneral(image)
  861. # print(f'百度api返回结果:{res_image}')
  862. # print(res_image.get('words_result', ''))
  863. # new_dic = dict()
  864. data = res_image.get('words_result', '')
  865. print(f'百度api返回结果:{data}')
  866. # full_text = ';'.join(item['words'] for item in data)
  867. # address = ''
  868. # for item in data:
  869. # if '企业注册号' in item['words']:
  870. # print('come in 111')
  871. # reg_number = item['words'].split(':', 1)[1].strip()
  872. # elif '企业名称' in item['words']:
  873. # print('come in 222')
  874. # company_name = item['words'].split(':', 1)[1].strip()
  875. # elif '所:' in item['words']:
  876. # print('come in 333')
  877. # address = item['words'].split(':', 1)[1].strip()
  878. # # 输出结果
  879. # print("企业注册号:", reg_number)
  880. # print("企业名称:", company_name)
  881. # print("住所:", address)
  882. return data
  883. except:
  884. return None
  885. def screenshot_the_business_license(self, qualification_number):
  886. screenshot_path = 'screenshot1.png'
  887. self.d.screenshot(screenshot_path)
  888. img = cv2.imread(screenshot_path)
  889. # 指定裁剪区域 (left, top, right, bottom)
  890. left = 0
  891. top = 480
  892. right = 720
  893. bottom = 1420
  894. cropped_img = img[top:bottom, left:right]
  895. # 创建目录
  896. SCREENSHOT_DIR = Path('screenshot') # 注意这里的变化和py文件同一级目录即可
  897. SCREENSHOT_DIR.mkdir(parents=True, exist_ok=True)
  898. if qualification_number:
  899. # cropped_screenshot_path = 'D:\\work\\dfwy_spider\\drug_data\\mt\\screenshot\\' + qualification_number + '.png'
  900. cropped_screenshot_path = SCREENSHOT_DIR / f'{qualification_number}.png'
  901. else:
  902. cropped_screenshot_path = 'cropped_screenshot.png'
  903. cv2.imwrite(cropped_screenshot_path, cropped_img)
  904. return cropped_screenshot_path
  905. def screenshot_instruction(self):
  906. # 获取当前时间
  907. current_time = datetime.datetime.now()
  908. # 格式化为时分秒
  909. time_str = current_time.strftime("%H-%M-%S")
  910. # 生成随机的 8 位字符串
  911. random_str = secrets.token_hex(4) # 生成 4 个字节的随机字符串,转换为 8 位十六进制字符串
  912. print(time_str)
  913. screenshot_path = 'instructionscreenshot1-' + time_str + '-' + random_str + '.png'
  914. self.d.screenshot(screenshot_path)
  915. return screenshot_path
  916. def extract_specification(self, text):
  917. """提取药品规格信息"""
  918. # 方法1:简单去除到期信息
  919. pattern = r'^[^【]+'
  920. match = re.search(pattern, text)
  921. if match:
  922. return match.group(0).strip()
  923. return text
  924. #获取商品title
  925. def get_title(self):
  926. # try:
  927. # title = self.d.xpath(
  928. # '//android.widget.ScrollView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView').text
  929. # except:
  930. # title = self.d.xpath(
  931. # '//android.widget.ScrollView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.TextView').text
  932. # title = self.d.xpath('//*[contains(@text, "舒肝颗粒")]').text
  933. def _inner():
  934. temp_search_key = self.search_key
  935. if "999" in self.search_key:
  936. if self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  937. temp_search_key = self.search_key.replace("999皮炎平", "")
  938. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  939. temp_search_key = self.search_key.replace("999必无忧", "")
  940. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  941. temp_search_key = self.search_key.replace("999必无忧", "")
  942. elif self.search_key == '999速复康布洛芬缓释胶囊':
  943. temp_search_key = self.search_key.replace("999速复康", "")
  944. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  945. temp_search_key = self.search_key.replace("999选平", "")
  946. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  947. temp_search_key = self.search_key.replace("999皮炎平", "")
  948. else:
  949. temp_search_key = self.search_key.replace("999", "")
  950. else:
  951. if self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  952. temp_search_key = self.search_key.replace("史达功", "")
  953. temp_search_key = temp_search_key.replace("120", "")
  954. elif self.search_key == '三九胃泰养胃舒颗粒8袋':
  955. temp_search_key = self.search_key.replace("三九胃泰", "")
  956. temp_search_key = temp_search_key.replace("8袋", "")
  957. elif self.search_key == '今维多赐多康牌蛋白粉':
  958. temp_search_key = self.search_key.replace("今维多", "")
  959. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  960. temp_search_key = self.search_key.replace("佳美舒", "")
  961. temp_search_key = temp_search_key.replace("4", "")
  962. elif self.search_key == '三九胃泰颗粒20g*10':
  963. temp_search_key = self.search_key.replace("20g*10", "")
  964. elif self.search_key == '三九胃泰颗粒20g*6袋':
  965. temp_search_key = self.search_key.replace("20g*6袋", "")
  966. elif self.search_key == '顺峰康王酮康他索乳膏':
  967. temp_search_key = self.search_key.replace("顺峰康王", "")
  968. if self.search_key == '999糠酸莫米松凝胶15':
  969. temp_search_key = temp_search_key.replace("15", "")
  970. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  971. temp_search_key = temp_search_key.replace("30", "")
  972. elif self.search_key == '999复方金银花颗粒10g':
  973. temp_search_key = temp_search_key.replace("10g", "")
  974. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  975. temp_search_key = temp_search_key.replace("15g*15袋/盒", "")
  976. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  977. temp_search_key = temp_search_key.replace("6粒", "")
  978. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  979. temp_search_key = temp_search_key.replace("50", "")
  980. elif self.search_key == '999止泻利颗粒15g*8':
  981. temp_search_key = temp_search_key.replace("15g*8", "")
  982. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  983. temp_search_key = temp_search_key.replace("30", "")
  984. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  985. temp_search_key = temp_search_key.replace("15g", "")
  986. elif self.search_key == '999复方苦参肠炎康片12片':
  987. temp_search_key = temp_search_key.replace("12片", "")
  988. elif self.search_key == '999强力枇杷露16袋':
  989. temp_search_key = temp_search_key.replace("16袋", "")
  990. elif self.search_key == '999三蛇胆川贝膏138':
  991. temp_search_key = temp_search_key.replace("138", "")
  992. elif self.search_key == '999强力枇杷露120ml':
  993. temp_search_key = temp_search_key.replace("120ml", "")
  994. elif self.search_key == '999强力枇杷露150ml':
  995. temp_search_key = temp_search_key.replace("150ml", "")
  996. elif self.search_key == '999抗病毒口服液10ml*10':
  997. temp_search_key = temp_search_key.replace("10ml*10", "")
  998. elif self.search_key == '999抗病毒口服液10ml*12':
  999. temp_search_key = temp_search_key.replace("10ml*12", "")
  1000. elif self.search_key == '999糠酸莫米松乳膏10g支':
  1001. temp_search_key = temp_search_key.replace("10g支", "")
  1002. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  1003. temp_search_key = temp_search_key.replace("20g", "")
  1004. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  1005. temp_search_key = temp_search_key.replace("(无糖)6g", "")
  1006. elif self.search_key == '999壮骨关节丸6g*20':
  1007. temp_search_key = temp_search_key.replace("6g*20", "")
  1008. elif self.search_key == '999正天丸6g*15':
  1009. temp_search_key = temp_search_key.replace("6g*15", "")
  1010. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  1011. temp_search_key = temp_search_key.replace("20", "")
  1012. elif self.search_key == '999糠酸莫米松凝胶10':
  1013. temp_search_key = temp_search_key.replace("10", "")
  1014. elif self.search_key == '999板蓝根颗粒10g*20':
  1015. temp_search_key = temp_search_key.replace("10g*20", "")
  1016. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  1017. temp_search_key = temp_search_key.replace("10粒", "")
  1018. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  1019. temp_search_key = temp_search_key.replace("12粒", "")
  1020. elif self.search_key == '999咽炎片0.26g*12片*2板':
  1021. temp_search_key = temp_search_key.replace("0.26g*12片*2板", "")
  1022. elif self.search_key == '999小儿止咳糖浆120':
  1023. temp_search_key = temp_search_key.replace("120", "")
  1024. elif self.search_key == '999小儿止咳糖浆225':
  1025. temp_search_key = temp_search_key.replace("225", "")
  1026. elif self.search_key == '999小儿感冒颗粒6g*10':
  1027. temp_search_key = temp_search_key.replace("6g*10", "")
  1028. elif self.search_key == '999小儿感冒颗粒6g*24':
  1029. temp_search_key = temp_search_key.replace("6g*24", "")
  1030. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  1031. temp_search_key = temp_search_key.replace("6g*10袋", "")
  1032. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  1033. temp_search_key = temp_search_key.replace("6g*20袋", "")
  1034. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  1035. temp_search_key = temp_search_key.replace("8g*10袋", "")
  1036. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  1037. temp_search_key = temp_search_key.replace("2.5g*10袋", "")
  1038. elif self.search_key == '999感冒清热颗粒12g*18':
  1039. temp_search_key = temp_search_key.replace("12g*18", "")
  1040. elif self.search_key == '999小柴胡颗粒10g*15':
  1041. temp_search_key = temp_search_key.replace("10g*15", "")
  1042. # elif self.search_key == '三九胃泰颗粒':
  1043. # self.search_key = '三九胃泰' #兼容三九胃泰 温胃舒颗粒
  1044. print(f'获取商品title时的搜索关键字:{temp_search_key}')
  1045. # title = self.d.xpath(f'//*[contains(@text, "{self.search_key}")]').text
  1046. #初始化
  1047. drugs_name = ''
  1048. specifications = ''
  1049. title = ''
  1050. #循环的获取title为了有时间来处理人机验证
  1051. for m in range(1, 6000) :
  1052. if self.d.xpath(f'//*[contains(@text, "{temp_search_key}")]').exists:
  1053. title = self.safe_exec(
  1054. lambda: self.d.xpath(f'//*[contains(@text, "{temp_search_key}")]').text
  1055. )
  1056. print(f"第{m}次获取title成功")
  1057. break
  1058. else:
  1059. time.sleep(3)
  1060. # return drugs_name, specifications
  1061. # drugs_name = ''
  1062. # specifications = ''
  1063. # try:
  1064. # title_xpath = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView'
  1065. # title_xpath_2 = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView'
  1066. # if self.d.xpath(title_xpath).exists:
  1067. # title = self.d.xpath(title_xpath).text
  1068. # print(f"title_xpath获取的title={title}")
  1069. # if temp_search_key not in title:
  1070. # return drugs_name, specifications
  1071. # elif self.d.xpath(title_xpath_2).exists:
  1072. # title = self.d.xpath(title_xpath_2).text
  1073. # print(f"title_xpath_2获取的title={title}")
  1074. # if temp_search_key not in title:
  1075. # return drugs_name, specifications
  1076. # else:
  1077. # print('title_xpath不存在,请确认')
  1078. # return drugs_name, specifications
  1079. # # title = self.d.xpath(f'//*[contains(@text, "{temp_search_key}")]').text
  1080. # except Exception as e:
  1081. # print(f"发生异常: {e}")
  1082. # return drugs_name, specifications
  1083. #奇怪:有的时候title取出来的记过第一位会多一个0
  1084. # title = self.safe_exec(self.d.xpath(f'//*[contains(@text, "{self.search_key}")]').text)
  1085. # title = self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView').text
  1086. title = title[1:] if title.startswith('0') else title
  1087. print(f'获取到药品标题:{title}')
  1088. # 从里面匹配出药品名和规格
  1089. # drugs_name
  1090. # specifications
  1091. # match = re.search(r'([^\d]+)([\d\D]+)', title)
  1092. if self.search_key == '999赐多康大豆':
  1093. return title, '1罐'
  1094. if self.search_key == "999感冒清热颗粒" :
  1095. match = re.search(r'(\[[^\]]+\])(.+?)(\d+.*)', title)
  1096. else:
  1097. match = re.match(r'(\[[^\]]+\])(.*?)\s*((?:\d+\S*|\(.+))$', title)
  1098. if match:
  1099. #drugs_name = match.group(1).strip() + match.group(2).strip()
  1100. drugs_name = title
  1101. specifications = match.group(3).strip()
  1102. print("药品名:", drugs_name)
  1103. print("规格:", specifications)
  1104. #如果品规中包含到期则需要再次的正则处理
  1105. if '到期' in specifications:
  1106. specifications = self.extract_specification(specifications)
  1107. # print('完整药名:', drugs_name + specifications)
  1108. return drugs_name, specifications
  1109. else:
  1110. if title == '999抗病毒口服液10ml*12' or title == '999抗病毒口服液':
  1111. drugs_name = title
  1112. specifications = '10ml*12支/盒'
  1113. return drugs_name, specifications
  1114. elif title == '999抗病毒口服液10ml*10':
  1115. drugs_name = title
  1116. specifications = '10ml*10支/盒'
  1117. return drugs_name, specifications
  1118. elif title == '999小柴胡颗粒':
  1119. drugs_name = title
  1120. specifications = '10g*9袋/盒'
  1121. return drugs_name, specifications
  1122. elif title == '999养胃舒颗粒':
  1123. drugs_name = title
  1124. specifications = '10g*10袋/盒'
  1125. return drugs_name, specifications
  1126. elif title == '三九胃泰胶囊':
  1127. drugs_name = title
  1128. specifications = '0.5g*24粒/盒'
  1129. return drugs_name, specifications
  1130. elif title == '999补脾益肠丸':
  1131. drugs_name = title
  1132. specifications = '6g*15袋/盒'
  1133. return drugs_name, specifications
  1134. elif title == '999复方感冒灵颗粒':
  1135. drugs_name = title
  1136. specifications = '14g*9袋/盒'
  1137. return drugs_name, specifications
  1138. else:
  1139. print("没有匹配到预期格式")
  1140. drugs_name = title
  1141. specifications = ''
  1142. return drugs_name, specifications
  1143. # 用 safe_exec 包装内部逻辑,确保验证码阻塞
  1144. return self.safe_exec(_inner)
  1145. def enter_shop(self):
  1146. """
  1147. 进店,方便提取资质环境
  1148. :return:
  1149. """
  1150. # self.d.xpath('//*[@text="进店"]').click()
  1151. self.d.xpath('//*[@text="店铺"]').click()
  1152. time.sleep(self.get_sleep_time())
  1153. def enter_shoper(self):
  1154. """
  1155. 进入商家
  1156. :return:
  1157. """
  1158. is_shoper_exists = 0
  1159. for i in range(10):
  1160. if self.d.xpath('//*[@text="商家"]').exists:
  1161. print(f'第{i}次商家存在')
  1162. is_shoper_exists = 1
  1163. break
  1164. else:
  1165. print(f'第{i}次商家不存在')
  1166. time.sleep(self.get_sleep_time())
  1167. if is_shoper_exists == 1:
  1168. self.d.xpath('//*[@text="商家"]').click()
  1169. time.sleep(self.get_sleep_time())
  1170. return True
  1171. else:
  1172. return False
  1173. #点击查看商家资质
  1174. def scan_shoper_license(self):
  1175. exist_shoper = 0
  1176. for i in range(10):
  1177. if self.d.xpath('//*[@text="查看商家资质"]').exists:
  1178. print(f'第{i}次查看商家资质存在')
  1179. exist_shoper = 1
  1180. break
  1181. else:
  1182. print(f'第{i}次查看商家资质不存在')
  1183. time.sleep(self.get_sleep_time())
  1184. if exist_shoper == 1:
  1185. self.d.xpath('//*[@text="查看商家资质"]').click()
  1186. time.sleep(self.get_sleep_time())
  1187. else:
  1188. self.swipe_back(1)
  1189. #验证商品的信息是否在数据库中已存在
  1190. def data_is_exists(self, data):
  1191. """
  1192. 检查指定数据是否已存在于数据库表中(仅检查存在性)
  1193. 参数:
  1194. data: 包含查询条件的字典,键为列名,值为条件值
  1195. 返回:
  1196. True: 数据存在
  1197. False: 数据不存在
  1198. None: 检查过程中出错
  1199. """
  1200. # dup_data = {'product': product, 'min_price': min_price, 'shop': shop, 'scrape_date': scrape_date,
  1201. # 'platform': '美团'}
  1202. # 1. 验证必要字段
  1203. required_keys = ['product', 'min_price', 'shop', 'scrape_date', 'platform']
  1204. if not all(key in data for key in required_keys):
  1205. missing = [key for key in required_keys if key not in data]
  1206. logging.error(f"缺少必要字段: {', '.join(missing)}")
  1207. return None
  1208. try:
  1209. # 连接数据库
  1210. conn = get_mysql()
  1211. # 创建游标对象
  1212. cur = conn.cursor()
  1213. # query_sql = f"SELECT * FROM {self.table_name} WHERE product = '{data['product']}' AND min_price = '{data['min_price']}' AND shop = '{data['shop']}' AND scrape_date = '{data['scrape_date']}' AND platform = '{data['platform']}'"
  1214. # cur.execute(query_sql)
  1215. query_sql = """
  1216. SELECT * FROM {}
  1217. WHERE product = %s
  1218. AND min_price = %s
  1219. AND shop = %s
  1220. AND scrape_date = %s
  1221. AND platform = %s
  1222. """.format(self.table_name)
  1223. cur.execute(query_sql, (
  1224. data['product'],
  1225. data['min_price'],
  1226. data['shop'],
  1227. data['scrape_date'],
  1228. data['platform']
  1229. ))
  1230. result = cur.fetchone()
  1231. return bool(result) # 如果存在返回True,否则False
  1232. except Exception as e:
  1233. print(f"MySQL 错误: {str(e)}")
  1234. #验证店铺信息是否在数据库中已存在
  1235. def shop_is_exists_database(self, shop):
  1236. try:
  1237. # 连接数据库
  1238. conn = get_mysql()
  1239. # 创建游标对象
  1240. cur = conn.cursor()
  1241. query_sql = """
  1242. SELECT * FROM {}
  1243. WHERE shop = %s
  1244. """.format(self.shop_table_name)
  1245. cur.execute(query_sql, (
  1246. shop
  1247. ))
  1248. result = cur.fetchone()
  1249. return bool(result) # 如果存在返回True,否则False
  1250. except Exception as e:
  1251. print(f"MySQL 错误: {str(e)}")
  1252. def wait_if_verifying(self, monitor, timeout=120):
  1253. """验证码处理期间阻塞主线程"""
  1254. start = time.time()
  1255. while monitor.pausing.is_set() and time.time() - start < timeout:
  1256. time.sleep(1)
  1257. # def safe_xpath(self, xpath, timeout=10):
  1258. # """线程安全 xpath 查找"""
  1259. # self.wait_if_verifying(self.monitor)
  1260. # return self.d.xpath(xpath).wait(timeout=timeout)
  1261. def wait_for_ready(self, monitor, timeout=86400):
  1262. """进入每一页前都先等验证码"""
  1263. start = time.time()
  1264. while monitor.pausing.is_set() and time.time() - start < timeout:
  1265. time.sleep(1)
  1266. # 额外保险:如果验证码突然在这一秒才弹,再主动扫一次
  1267. monitor.check_and_handle_popup()
  1268. def safe_list(self, xpath, monitor):
  1269. """线程安全地拿商品列表"""
  1270. self.wait_for_ready(monitor)
  1271. return self.d.xpath(xpath).all()
  1272. def safe_exec(self, func, *args, **kwargs):
  1273. """
  1274. 万能安全壳:执行 func 前检查验证码,
  1275. 若监控线程已置位 pausing,则一直阻塞直到放行。
  1276. """
  1277. while self.monitor.pausing.is_set():
  1278. time.sleep(1)
  1279. # 执行真正逻辑
  1280. return func(*args, **kwargs)
  1281. def get_next_data(self, data, target):
  1282. for i, item in enumerate(data):
  1283. if item['words'] == target:
  1284. if i + 1 < len(data):
  1285. return data[i + 1]['words']
  1286. return None
  1287. def delete_instruction_screenshot(self, screenshot_path):
  1288. # 删除截图文件
  1289. try:
  1290. os.remove(screenshot_path)
  1291. print(f"截图文件已删除:{screenshot_path}")
  1292. except FileNotFoundError:
  1293. print(f"文件未找到,无法删除:{screenshot_path}")
  1294. except Exception as e:
  1295. print(f"删除文件时出错:{e}")
  1296. '''
  1297. def get_instructions_data(self):
  1298. """
  1299. 确定有说明书之后,提取所有的说明书数据
  1300. :return:
  1301. """
  1302. self.d.xpath('//*[@text="说明"]').click()
  1303. # time.sleep(random.randint(3, 5))
  1304. time.sleep(0.5)
  1305. self.d.xpath('//*[@text="查看详细说明"]').click()
  1306. # time.sleep(random.randint(3, 5))
  1307. time.sleep(0.5)
  1308. self.d.xpath('//*[@text="加载更多"]').click_exists()
  1309. loop_page = 5
  1310. # new_list = list()
  1311. new_list = []
  1312. for i in range(loop_page):
  1313. self.d.xpath('//*[@text="加载更多"]').click_exists()
  1314. time.sleep(0.2)
  1315. if i == 0:
  1316. self.d.swipe(200, 1000, 200, 300, 0.4)
  1317. else:
  1318. self.d.swipe(200, 1000, 200, 62)
  1319. time.sleep(0.2)
  1320. if self.d.xpath('//*[@text="加载更多"]').exists:
  1321. self.d.xpath('//*[@text="加载更多"]').click()
  1322. time.sleep(0.2)
  1323. all_tt = self.d.xpath(
  1324. '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup').all()
  1325. for idx in range(1, len(all_tt) + 1):
  1326. all_tt1 = self.d.xpath(
  1327. f'//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[{idx}]//android.widget.TextView').all()
  1328. # print(f'当前说明书列表数据:{all_tt1}')
  1329. for tt in all_tt1:
  1330. if tt.text and tt.text != '展开全文':
  1331. new_list.append(tt.text)
  1332. if i == 0:
  1333. height = 938
  1334. else:
  1335. drug_box = self.d.xpath(
  1336. '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]').info
  1337. bounds = drug_box['bounds']
  1338. height = bounds['bottom'] - bounds['top']
  1339. if height < 938:
  1340. # print('说明书翻页到底部')
  1341. break
  1342. # 展开全文
  1343. new_list = [item for item in new_list if item != '展开全文']
  1344. print(f'当前说明书列表数据:{new_list}')
  1345. # expiry_date_index = next(idx for idx, i in enumerate(new_list) if i == '有效期')
  1346. # manufacturer_index = next(idx for idx, i in enumerate(new_list) if i == '生产单位')
  1347. # approval_number_index = next(idx for idx, i in enumerate(new_list) if i == '批准文号')
  1348. # res_data = {
  1349. # "有效期": new_list[expiry_date_index + 1],
  1350. # "生产单位": new_list[manufacturer_index + 1],
  1351. # "批准文号": new_list[approval_number_index + 1]
  1352. # }
  1353. res_data = {
  1354. "有效期": (new_list[new_list.index("有效期") + 1]) if "有效期" in new_list and new_list.index("有效期") + 1 < len(new_list) else "",
  1355. "生产单位": (new_list[new_list.index("生产单位") + 1]) if "生产单位" in new_list and new_list.index("生产单位") + 1 < len(new_list) else "",
  1356. "批准文号": (new_list[new_list.index("批准文号") + 1]) if "批准文号" in new_list and new_list.index("批准文号") + 1 < len(new_list) else ""
  1357. }
  1358. print(f'当前说明书字典数据:{res_data}')
  1359. return res_data
  1360. '''
  1361. '''
  1362. def get_instructions_data(self):
  1363. """
  1364. 确定有说明书之后,提取所有的说明书数据
  1365. :return:
  1366. """
  1367. self.d.xpath('//*[@text="说明"]').click()
  1368. # time.sleep(random.randint(3, 5))
  1369. time.sleep(0.5)
  1370. self.d.xpath('//*[@text="查看详细说明"]').click()
  1371. # time.sleep(random.randint(3, 5))
  1372. time.sleep(0.5)
  1373. # 1) 先向上滑动一次,触发“加载更多”出现
  1374. self.d.swipe(200, 1000, 200, 300, 0.4)
  1375. time.sleep(0.3)
  1376. # 2) 再进入“出现就点”的循环
  1377. while self.d.xpath('//*[@text="加载更多"]').click_exists(timeout=1):
  1378. time.sleep(0.2)
  1379. self.d.swipe(200, 1000, 200, 300, 0.4)
  1380. # self.d.swipe(200, 1000, 200, 62)
  1381. time.sleep(0.2)
  1382. # 一次性获取所有文本
  1383. texts = [
  1384. node.text.strip()
  1385. # for node in self.d.xpath('//android.widget.TextView').all()
  1386. for node in self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]//android.widget.TextView').all()
  1387. if node.text and node.text.strip() and node.text != '加载更多'
  1388. ]
  1389. print(f'当前说明书列表数据:{texts}')
  1390. # 提取关键字段
  1391. def safe_get(key):
  1392. # try:
  1393. # idx = texts.index(key)
  1394. # return texts[idx + 1] if idx + 1 < len(texts) else ""
  1395. # except ValueError:
  1396. # return ""
  1397. try:
  1398. idx = next(i for i, text in enumerate(texts) if text == key)
  1399. return texts[idx + 1] if idx + 1 < len(texts) else ""
  1400. except StopIteration:
  1401. return ""
  1402. res_data = {
  1403. "有效期": safe_get("有效期"),
  1404. "生产单位": safe_get("生产单位"),
  1405. "批准文号": safe_get("批准文号")
  1406. }
  1407. print(f'当前说明书字典数据:{res_data}')
  1408. return res_data
  1409. '''
  1410. '''
  1411. def get_instructions_data(self):
  1412. """
  1413. 说明书键值对采集:连续两个 TextView 为一对,精确提取
  1414. """
  1415. # 1. 进入说明书
  1416. self.d(text="说明").click()
  1417. time.sleep(0.5)
  1418. self.d(text="查看详细说明").click()
  1419. time.sleep(0.5)
  1420. # self.d(text="加载更多").click_exists(timeout=0.5)
  1421. # 2. 找到说明书最外层 ScrollView(页面主体)
  1422. scroll_view = self.d(resourceId="com.sankuai.meituan:id/container") .child(className="android.widget.ScrollView")
  1423. count = scroll_view.count
  1424. print(f"找到的 ScrollView 数量: {count}")
  1425. if not scroll_view.exists:
  1426. return {"有效期": "", "生产单位": "", "批准文号": ""}
  1427. # 3. 在 ScrollView 内再定位真正包含键值对的容器
  1428. # 绝大多数美团说明书页面对应的是 ScrollView > ViewGroup > 若干 TextView
  1429. kv_container = scroll_view.child(className="android.view.ViewGroup")
  1430. if not kv_container.exists:
  1431. kv_container = scroll_view # 降级:直接对 ScrollView 取子孙 TextView
  1432. # 4. 滑动到底并收集所有 TextView(保留顺序)
  1433. all_texts = []
  1434. max_swipe = 5
  1435. last_length = 0
  1436. for _ in range(max_swipe):
  1437. texts = kv_container.child(className="android.widget.TextView")
  1438. #获取texts中的文本
  1439. print(f'当前说明书列表数据:{texts}')
  1440. current_texts = []
  1441. self.loggerMT.info(f'说明书111')
  1442. for tv in texts:
  1443. try:
  1444. txt = tv.get_text().strip()
  1445. # txt = tv.info['text'].strip()
  1446. except Exception:
  1447. continue
  1448. if txt and txt != "展开全文":
  1449. current_texts.append(txt)
  1450. self.loggerMT.info(f'说明书222')
  1451. print(f'当前说明书列表数据:{current_texts}')
  1452. # 去重
  1453. if current_texts:
  1454. current_texts = [t for t in current_texts if t not in all_texts]
  1455. all_texts.extend(current_texts)
  1456. # 判断是否到底
  1457. # if not scroll_view.info.get("scrollable"):
  1458. # break
  1459. # 判断是否到底
  1460. if len(all_texts) == last_length:
  1461. break
  1462. last_length = len(all_texts)
  1463. # self.d.swipe_ext("up", scale=0.7)
  1464. #向上滑动一次
  1465. self.d.swipe(200, 1000, 200, 300, 0.2)
  1466. time.sleep(0.2)
  1467. if self.d.xpath('//*[@text="加载更多"]').exists:
  1468. self.d.xpath('//*[@text="加载更多"]').click()
  1469. # 5. 成对解析
  1470. res_data = {"有效期": "", "生产单位": "", "批准文号": ""}
  1471. for i in range(len(all_texts) - 1):
  1472. key = all_texts[i]
  1473. val = all_texts[i + 1]
  1474. if key in res_data:
  1475. res_data[key] = val
  1476. print(f'说明书文本共 {len(all_texts)} 条,提取结果: {res_data}')
  1477. # time.sleep(1000000)
  1478. return res_data
  1479. '''
  1480. def get_instructions_data(self):
  1481. """
  1482. 确定有说明书之后,提取所有的说明书数据
  1483. :return:
  1484. """
  1485. self.d.xpath('//*[@text="说明"]').click()
  1486. # time.sleep(random.randint(3, 5))
  1487. time.sleep(0.5)
  1488. if self.d.xpath('//*[@text="查看详细说明"]').exists:
  1489. self.d.xpath('//*[@text="查看详细说明"]').click()
  1490. else:
  1491. for i in range(8):
  1492. if self.d.xpath('//*[@text="查看全部"]').exists:
  1493. print('开始点击查看全部')
  1494. break
  1495. self.d.swipe_ext('down', 0.3)
  1496. time.sleep(1)
  1497. if self.d.xpath('//*[@text="查看全部"]').exists:
  1498. print('开始点击查看全部2')
  1499. break
  1500. if self.d.xpath('//*[@text="查看全部"]').exists:
  1501. self.d.xpath('//*[@text="查看全部"]').click()
  1502. else:
  1503. res_data = {
  1504. "有效期": '',
  1505. "生产单位": '',
  1506. "批准文号": ''
  1507. }
  1508. self.loggerMT.info('获取到的说明书信息为空。')
  1509. return res_data
  1510. # time.sleep(random.randint(3, 5))
  1511. time.sleep(0.5)
  1512. # self.d.xpath('//*[@text="加载更多"]').click_exists()
  1513. # loop_page = 5
  1514. # new_list = list()
  1515. # new_list = []
  1516. for ii in range(8):
  1517. if self.d.xpath('//*[@text="加载更多"]').exists:
  1518. self.d.xpath('//*[@text="加载更多"]').click()
  1519. time.sleep(0.2)
  1520. break
  1521. else:
  1522. self.d.swipe(200, 1000, 200, 300, 0.3)
  1523. # self.d.swipe_ext("up", scale=0.3)
  1524. for iii in range(10):
  1525. if self.d.xpath('//*[@text="生产单位"]').exists and self.d.xpath('//*[@text="批准文号"]').exists:
  1526. break
  1527. else:
  1528. self.d.swipe(200, 1300, 200, 300, 0.3)
  1529. # self.d.swipe_ext("up", scale=0.3)
  1530. instruction_path = self.screenshot_instruction()
  1531. print(f"instruction_path= {instruction_path}")
  1532. time.sleep(2)
  1533. ocr_res = self.get_ocr_res_image(instruction_path)
  1534. # print(f'ocr_res:{ocr_res}')
  1535. if ocr_res:
  1536. # 获取有效期的下一个数据
  1537. validity = self.get_next_data(ocr_res, '有效期')
  1538. # 获取批准文号的下一个数据
  1539. approval_number = self.get_next_data(ocr_res, '批准文号')
  1540. # 获取生产单位的下一个数据
  1541. manufacturer = self.get_next_data(ocr_res, '生产单位')
  1542. else:
  1543. validity = ''
  1544. approval_number = ''
  1545. manufacturer = ''
  1546. # print("有效期:", validity)
  1547. # print("批准文号:", approval_number)
  1548. # print("生产单位:", manufacturer)
  1549. res_data = {
  1550. "有效期": validity,
  1551. "生产单位": manufacturer,
  1552. "批准文号": approval_number
  1553. }
  1554. print(f"res_data={res_data}")
  1555. time.sleep(1)
  1556. self.delete_instruction_screenshot(instruction_path)
  1557. return res_data
  1558. def has_instructions(self):
  1559. """
  1560. 是否有说明书
  1561. :return:
  1562. """
  1563. # 没有说明书的无法采集具体数据
  1564. time.sleep(self.get_sleep_time())
  1565. is_has_instructions = False
  1566. for i in range(8):
  1567. if self.d.xpath('//*[@text="说明"]').exists:
  1568. print(f"第{i}次有说明书1")
  1569. is_has_instructions = True
  1570. break
  1571. self.d.swipe_ext('down', 0.3)
  1572. time.sleep(1)
  1573. # detail_info = self.d.xpath(
  1574. # '//android.widget.ScrollView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[6]').info
  1575. # bounds = detail_info['bounds']
  1576. # height = bounds['bottom'] - bounds['top']
  1577. # if self.d.xpath('//*[@text="进店"]').exists and height > 100:
  1578. if self.d.xpath('//*[@text="说明"]').exists:
  1579. is_has_instructions = True
  1580. print(f"第{i}次有说明书2")
  1581. break
  1582. # is_has_instructions = self.d.xpath('//*[@text="说明"]').exists
  1583. return is_has_instructions
  1584. def has_shop(self):
  1585. """
  1586. 是否有进店按钮
  1587. :return:
  1588. """
  1589. # self.d.swipe_ext('up', 0.1)
  1590. time.sleep(self.get_sleep_time())
  1591. is_has_enter_shop = self.d.xpath('//*[@text="进店"]').exists
  1592. return is_has_enter_shop
  1593. #获取商品对应的店铺信息
  1594. def get_license_info_ex(self):
  1595. # self.enter_shop()
  1596. self.safe_exec(self.enter_shop)
  1597. # self.enter_shoper()
  1598. result = self.safe_exec(self.enter_shoper)
  1599. if result == False:
  1600. license_info_data = {'contact_address': '', 'qualification_number': '', 'business_license_company': '', 'business_license_address': ''}
  1601. return license_info_data
  1602. for i in range(10):
  1603. if self.d.xpath('//*[@text="查看商家资质"]').exists:
  1604. print(f"第{i}次有商家资质")
  1605. break
  1606. else:
  1607. print(f"第{i}次没有商家资质")
  1608. time.sleep(self.get_sleep_time())
  1609. #获取地址
  1610. # contact_address = self.get_shop_address()
  1611. contact_address = self.safe_exec(self.get_shop_address)
  1612. # time.sleep(50000)
  1613. ###
  1614. # self.scan_shoper_license()
  1615. self.safe_exec(self.scan_shoper_license)
  1616. # 获取资质编码
  1617. # qualification_number = self.get_qualification_number()
  1618. qualification_number = self.safe_exec(self.get_qualification_number)
  1619. #qualification_number 不为None继续下一步
  1620. if qualification_number:
  1621. #营业执照公司名称
  1622. business_license_company = ''
  1623. #营业执照地址
  1624. business_license_address = ''
  1625. self.d.click(0.603, 0.27)
  1626. time.sleep(self.get_sleep_time())
  1627. cropped_screenshot_path = self.screenshot_the_business_license(qualification_number)
  1628. print(f'cropped_screenshot_path:{cropped_screenshot_path}')
  1629. # if qualification_number:
  1630. # cropped_screenshot_path = 'D:\\work\\dfwy_spider\\drug_data\\mt\\screenshot\\' + qualification_number + '.png'
  1631. # else:
  1632. # cropped_screenshot_path = 'cropped_screenshot.png'
  1633. # ocr_res = self.get_ocr_res('cropped_screenshot.png')
  1634. ocr_res = self.get_ocr_res(cropped_screenshot_path)
  1635. print(f'ocr_res:{ocr_res}')
  1636. #获取ocr_res 中的地址、单位名称
  1637. if ocr_res:
  1638. if '单位名称' in ocr_res.keys():
  1639. business_license_company = ocr_res['单位名称']
  1640. if '地址' in ocr_res.keys():
  1641. business_license_address = ocr_res['地址']
  1642. license_info_data = {'contact_address': contact_address, 'qualification_number': qualification_number, 'business_license_company': business_license_company, 'business_license_address': business_license_address}
  1643. else:
  1644. license_info_data = {'contact_address': contact_address, 'qualification_number': '', 'business_license_company': '', 'business_license_address': ''}
  1645. return license_info_data
  1646. """暂不用该功能
  1647. def get_license_info(self):
  1648. self.enter_shop()
  1649. self.enter_shoper()
  1650. self.scan_shoper_license()
  1651. # 获取资质编码
  1652. qualification_number = self.get_qualification_number()
  1653. if qualification_number:
  1654. table_license_info = self.get_table_license_info(qualification_number)
  1655. if table_license_info:
  1656. return {
  1657. '单位名称': table_license_info[0],
  1658. '地址': table_license_info[1],
  1659. '社会信用代码': table_license_info[2]
  1660. }
  1661. else:
  1662. # operate_no = random.randint(0, 1)
  1663. self.d.click(0.603, 0.27)
  1664. # if operate_no == 0:
  1665. # self.d.xpath('//*[@text="营业执照"]').click()
  1666. # else:
  1667. # self.d.click(0.603, 0.27)
  1668. time.sleep(self.get_sleep_time())
  1669. self.screenshot_the_business_license()
  1670. ocr_res = self.get_ocr_res('cropped_screenshot.png')
  1671. return ocr_res
  1672. # operate_no = random.randint(0, 1)
  1673. self.d.click(0.603, 0.27)
  1674. # if operate_no == 0:
  1675. # self.d.xpath('//*[@text="营业执照"]').click()
  1676. # else:
  1677. # self.d.click(0.603, 0.27)
  1678. time.sleep(self.get_sleep_time())
  1679. self.screenshot_the_business_license()
  1680. ocr_res = self.get_ocr_res('cropped_screenshot.png')
  1681. return ocr_res
  1682. """
  1683. def distinct_target(self):
  1684. result = False
  1685. position_xpath = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]'
  1686. position_xpath2 = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]'
  1687. is_position = self.d.xpath(position_xpath).exists
  1688. is_position2 = self.d.xpath(position_xpath2).exists
  1689. xpath = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()]'
  1690. xpath2 = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()]'
  1691. xpath3 = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()]'
  1692. xpath4 = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()]'
  1693. is_position5 = self.d.xpath(xpath).exists
  1694. is_position6 = self.d.xpath(xpath2).exists
  1695. is_position7 = self.d.xpath(xpath3).exists
  1696. is_position8 = self.d.xpath(xpath4).exists
  1697. # print(f"is_position = {is_position}")
  1698. # print(f"is_position2 = {is_position2}")
  1699. if result == False:
  1700. print("---检测没有回到列表页---")
  1701. else:
  1702. print("---检测回到了列表页---")
  1703. if is_position or is_position2 or is_position5 or is_position6 or is_position7 or is_position8:
  1704. result = True
  1705. return result
  1706. # return is_position
  1707. def enter_target_page(self):
  1708. self.d.xpath('//*[@content-desc="看病买药"]').click()
  1709. time.sleep(self.get_sleep_time())
  1710. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/vf_search_carousel_text"]').click()
  1711. time.sleep(self.get_sleep_time())
  1712. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]').click()
  1713. time.sleep(self.get_sleep_time())
  1714. self.d.send_keys(self.search_key, clear=True)
  1715. time.sleep(self.get_sleep_time())
  1716. self.d.xpath('//*[@text="搜索"]').click()
  1717. time.sleep(self.get_sleep_time())
  1718. # content_frame = self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]').exists
  1719. # print(content_frame)
  1720. # position_xpath1 = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]'
  1721. # position_xpath2 = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]'
  1722. # if self.d.xpath(position_xpath1).exists:
  1723. # print("position_xpath1 exist")
  1724. # elif self.d.xpath(position_xpath2).exists:
  1725. # print("position_xpath2 exist")
  1726. # else:
  1727. # print("position_xpath not exist")
  1728. # time.sleep(10000)
  1729. #增加点击快递送
  1730. self.click_express_send()
  1731. time.sleep(self.get_sleep_time())
  1732. def click_express_send(self):
  1733. # xpath= '//*[@resource-id="com.sankuai.meituan:id/container"]//android.widget.HorizontalScrollView[last()]'
  1734. slide_xpath = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]'
  1735. slide_xpath2= '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]'
  1736. slide_xpath3= '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]'
  1737. slide_xpath4= '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]'
  1738. for i in range (1,3):
  1739. if self.d.xpath(slide_xpath).exists:
  1740. bounds = self.d.xpath(slide_xpath).info['bounds']
  1741. top = bounds['top']
  1742. bottom = bounds['bottom']
  1743. print(f'top={top}')
  1744. print(f'bottom={bottom}')
  1745. y = (top + bottom) // 2
  1746. print(f'y={y}')
  1747. self.loggerMT.info('开始滑动1')
  1748. self.d.swipe(500, y, 100, y, 0.5)
  1749. time.sleep(self.get_sleep_time())
  1750. break
  1751. elif self.d.xpath(slide_xpath2).exists:
  1752. bounds = self.d.xpath(slide_xpath2).info['bounds']
  1753. top = bounds['top']
  1754. bottom = bounds['bottom']
  1755. print(f'top={top}')
  1756. print(f'bottom={bottom}')
  1757. y = (top + bottom) // 2
  1758. print(f'y={y}')
  1759. self.loggerMT.info('开始滑动2')
  1760. self.d.swipe(500, y, 100, y, 0.5)
  1761. time.sleep(self.get_sleep_time())
  1762. break
  1763. elif self.d.xpath(slide_xpath3).exists:
  1764. bounds = self.d.xpath(slide_xpath3).info['bounds']
  1765. top = bounds['top']
  1766. bottom = bounds['bottom']
  1767. print(f'top={top}')
  1768. print(f'bottom={bottom}')
  1769. y = (top + bottom) // 2
  1770. print(f'y={y}')
  1771. self.loggerMT.info('开始滑动3')
  1772. self.d.swipe(500, y, 100, y, 0.5)
  1773. time.sleep(self.get_sleep_time())
  1774. break
  1775. elif self.d.xpath(slide_xpath4).exists:
  1776. bounds = self.d.xpath(slide_xpath4).info['bounds']
  1777. top = bounds['top']
  1778. bottom = bounds['bottom']
  1779. print(f'top={top}')
  1780. print(f'bottom={bottom}')
  1781. y = (top + bottom) // 2
  1782. print(f'y={y}')
  1783. self.loggerMT.info('开始滑动4')
  1784. self.d.swipe(500, y, 100, y, 0.5)
  1785. time.sleep(self.get_sleep_time())
  1786. break
  1787. max_retry = 5 # 最多尝试次数
  1788. for idx in range(1, max_retry + 1):
  1789. # xpath= '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()-1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]'
  1790. xpath= '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()]'
  1791. xpath2= '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()]'
  1792. xpath3= '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()]'
  1793. xpath4 = '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()]'
  1794. # print(f"xpath:{xpath}")
  1795. # scroll_view = self.d(resourceId="com.sankuai.meituan:id/container") .child(className="android.widget.HorizontalScrollView")
  1796. if self.d.xpath(xpath).exists:
  1797. self.d.xpath(xpath).click()
  1798. # time.sleep(self.get_sleep_time())
  1799. print(f"第{idx}次点击xpath快递送成功")
  1800. time.sleep(self.get_sleep_time())
  1801. break
  1802. elif self.d.xpath(xpath2).exists:
  1803. self.d.xpath(xpath2).click()
  1804. # time.sleep(self.get_sleep_time())
  1805. print(f"第{idx}次点击xpath2快递送成功")
  1806. time.sleep(self.get_sleep_time())
  1807. break
  1808. elif self.d.xpath(xpath3).exists:
  1809. self.d.xpath(xpath3).click()
  1810. # time.sleep(self.get_sleep_time())
  1811. print(f"第{idx}次点击xpath3快递送成功")
  1812. time.sleep(self.get_sleep_time())
  1813. break
  1814. elif self.d.xpath(xpath4).exists:
  1815. self.d.xpath(xpath4).click()
  1816. # time.sleep(self.get_sleep_time())
  1817. print(f"第{idx}次点击xpath4快递送成功")
  1818. time.sleep(self.get_sleep_time())
  1819. break
  1820. else:
  1821. print(f"第{idx}次点击xpath或xpath2或xpath3快递送都失败")
  1822. time.sleep(self.get_sleep_time())
  1823. # xpath2= '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.HorizontalScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[last()]'
  1824. # if self.d.xpath(xpath2).exists:
  1825. # self.d.xpath(xpath2).click()
  1826. # print(f"第{idx}次点击xpath2快递送成功")
  1827. # time.sleep(self.get_sleep_time())
  1828. # break
  1829. """暂不用该功能
  1830. def get_table_license_info(self, qualification_number):
  1831. try:
  1832. sql = f'select business_license_company,city,credit_code from mt_drug where credit_code = "{qualification_number}"'
  1833. self.mysql_client.cur.execute(sql)
  1834. res = self.mysql_client.cur.fetchone()
  1835. return res
  1836. except:
  1837. return None
  1838. """
  1839. # def get_clipboard(self):
  1840. # """通过ADB获取Android手机剪贴板内容"""
  1841. # try:
  1842. # result = subprocess.run(
  1843. # ["adb", "shell", "am", "broadcast", "-a", "clipper.get"],
  1844. # capture_output=True,
  1845. # text=True,
  1846. # timeout=5
  1847. # )
  1848. # print(f"获取剪贴板结果: {result.stdout}")
  1849. # # 解析返回信息中的剪贴板内容
  1850. # for line in result.stdout.splitlines():
  1851. # if "data=" in line:
  1852. # return line.split("data=")[1].strip()
  1853. # return ""
  1854. # except Exception as e:
  1855. # print("获取剪贴板失败:", e)
  1856. # return ""
  1857. # def get_clipboard(self):
  1858. # """读取 Android 剪贴板(系统自带命令)"""
  1859. # try:
  1860. # text = subprocess.check_output(
  1861. # ["adb", "shell", "cmd", "clipboard", "get"],
  1862. # text=True, timeout=5, stderr=subprocess.STDOUT
  1863. # ).strip()
  1864. # print(f"获取剪贴板结果: {text}")
  1865. # return text if text else ""
  1866. # except Exception as e:
  1867. # print("获取剪贴板失败:", e)
  1868. # return ""
  1869. def get_clipboard(self):
  1870. time.sleep(1)
  1871. self.loggerMT.info(f"Clipboard content:{self.d.clipboard}") # 打印调试信息
  1872. clipboard_content = self.d.clipboard
  1873. if clipboard_content is None:
  1874. return ''
  1875. return clipboard_content.strip()
  1876. # return self.d.clipboard.strip()
  1877. def clear_clipboard(self):
  1878. self.d.set_clipboard("", "text/plain")
  1879. # def clear_clipboard(self):
  1880. # """清空手机剪贴板:写入空字符串(subprocess 版)"""
  1881. # try:
  1882. # subprocess.run(
  1883. # ["adb", "shell", "am", "broadcast", "-a", "clipper.set", "-e", "text", " "],
  1884. # check=True,
  1885. # capture_output=True,
  1886. # text=True,
  1887. # timeout=5
  1888. # )
  1889. # except subprocess.CalledProcessError as e:
  1890. # print("ADB 清空失败:", e.stderr)
  1891. # def clear_clipboard():
  1892. # """清空手机剪贴板:写入空字符串"""
  1893. # try:
  1894. # adb_shell(["shell", "am", "broadcast", "-a", "clipper.set", "-e", "text", ""])
  1895. # except subprocess.CalledProcessError as e:
  1896. # print("ADB 清空失败:", e.output)
  1897. #获取一个商品的数据、商品对应的店铺的数据
  1898. def get_product_link(self):
  1899. product_link = ''
  1900. # 两种可能的“···”按钮
  1901. dots_xpaths = [
  1902. '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]',
  1903. '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]',
  1904. '//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]'
  1905. ]
  1906. max_retry = 5 # 最多尝试次数
  1907. for idx in range(1, max_retry + 1):
  1908. if product_link: # 已经拿到则退出
  1909. break
  1910. for xp in dots_xpaths:
  1911. if self.d.xpath(xp).exists:
  1912. print(f'{idx}-进入分享点点点')
  1913. self.loggerMT.info(f'{idx}-进入分享点点点')
  1914. # #先清空剪贴板的内容
  1915. # self.clear_clipboard()
  1916. # print("清空剪贴板内容成功。")
  1917. self.d.xpath(xp).click()
  1918. time.sleep(0.2)
  1919. self.d.xpath('//*[@text="分享商品"]').click_exists()
  1920. time.sleep(0.2)
  1921. link_xpath = '//*[@text="复制链接"]'
  1922. if self.d.xpath(link_xpath).exists:
  1923. self.d.xpath(link_xpath).click()
  1924. time.sleep(1)
  1925. product_link = self.get_clipboard()
  1926. time.sleep(0.5)
  1927. print(f'{idx}-商品链接:{product_link}')
  1928. self.loggerMT.info(f'{idx}-商品链接:{product_link}')
  1929. break # 找到并执行后跳出内层循环
  1930. else:
  1931. print(f'{idx}-商品链接:{product_link}')
  1932. self.loggerMT.info(f'{idx}-商品链接:{product_link}')
  1933. product_link = ''
  1934. # self.d.xpath('//*[@text="复制链接"]').click_exists()
  1935. # time.sleep(1)
  1936. # product_link = self.get_clipboard()
  1937. # time.sleep(0.5)
  1938. # print(f'{idx}-商品链接:{product_link}')
  1939. # self.loggerMT.info(f'{idx}-商品链接:{product_link}')
  1940. # break # 找到并执行后跳出内层循环
  1941. if not product_link and idx < max_retry:
  1942. time.sleep(0.5) # 最后一次不需要再等待
  1943. return product_link
  1944. def integrate_data(self):
  1945. #测试说明书详情:
  1946. # instructions_info = self.safe_exec(self.get_instructions_data)
  1947. # time.sleep(1000000)
  1948. #测试店铺信息
  1949. # license_info = self.safe_exec(self.get_license_info_ex)
  1950. # time.sleep(1000000)
  1951. #测试定位地址
  1952. #获取链接开始
  1953. #self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ScrollView[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView').text
  1954. #1、点击页面的... 先判断元素是否存在
  1955. '''
  1956. if self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').exists:
  1957. print('1-进入分享点点点111')
  1958. self.loggerMT.info('1-进入分享点点点111')
  1959. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').click()
  1960. #点击分享商品
  1961. # if self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').exists:
  1962. time.sleep(0.2)
  1963. self.d.xpath('//*[@text="分享商品"]').click_exists()
  1964. time.sleep(0.2)
  1965. self.d.xpath('//*[@text="复制链接"]').click_exists()
  1966. time.sleep(1)
  1967. #获取剪切板的数据
  1968. product_link = self.get_clipboard()
  1969. time.sleep(0.5)
  1970. print(f'1-商品链接:{product_link}')
  1971. self.loggerMT.info(f'1-商品链接:{product_link}')
  1972. #清空剪切板
  1973. # self.clear_clipboard()
  1974. # if self.d.xpath('//*[@text="加载更多"]').click_exists():
  1975. # self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').click()
  1976. # if self.d.xpath('//android.support.v7.widget.RecyclerView/android.view.ViewGroup[3]/android.widget.ImageView[1]').exists:
  1977. # self.d.xpath('//android.support.v7.widget.RecyclerView/android.view.ViewGroup[3]/android.widget.ImageView[1]').click()
  1978. # #获取剪切板的数据
  1979. # product_link = self.get_clipboard()
  1980. # time.sleep(0.5)
  1981. # print(f'商品链接:{product_link}')
  1982. # #清空剪切板
  1983. # self.clear_clipboard()
  1984. # else:
  1985. # print('未找到分享按钮111')
  1986. elif self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').exists:
  1987. print('1-进入分享点点点222')
  1988. self.loggerMT.info('1-进入分享点点点222')
  1989. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').click()
  1990. time.sleep(0.2)
  1991. self.d.xpath('//*[@text="分享商品"]').click_exists()
  1992. time.sleep(0.2)
  1993. self.d.xpath('//*[@text="复制链接"]').click_exists()
  1994. time.sleep(1)
  1995. #获取剪切板的数据
  1996. product_link = self.get_clipboard()
  1997. time.sleep(0.5)
  1998. print(f'1-商品链接:{product_link}')
  1999. self.loggerMT.info(f'1-商品链接:{product_link}')
  2000. #如果为获取到product_link 则等待0.5秒再获取
  2001. if not product_link:
  2002. time.sleep(0.5)
  2003. if self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').exists:
  2004. print('2-进入分享点点点111')
  2005. self.loggerMT.info('2-进入分享点点点111')
  2006. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').click()
  2007. #点击分享商品
  2008. # if self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').exists:
  2009. time.sleep(0.2)
  2010. self.d.xpath('//*[@text="分享商品"]').click_exists()
  2011. time.sleep(0.2)
  2012. self.d.xpath('//*[@text="复制链接"]').click_exists()
  2013. time.sleep(1)
  2014. #获取剪切板的数据
  2015. product_link = self.get_clipboard()
  2016. time.sleep(0.5)
  2017. print(f'2-商品链接:{product_link}')
  2018. self.loggerMT.info(f'2-商品链接:{product_link}')
  2019. elif self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').exists:
  2020. print('2-进入分享点点点222')
  2021. self.loggerMT.info('2-进入分享点点点222')
  2022. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').click()
  2023. time.sleep(0.2)
  2024. self.d.xpath('//*[@text="分享商品"]').click_exists()
  2025. time.sleep(0.2)
  2026. self.d.xpath('//*[@text="复制链接"]').click_exists()
  2027. time.sleep(1)
  2028. #获取剪切板的数据
  2029. product_link = self.get_clipboard()
  2030. time.sleep(0.5)
  2031. print(f'2-商品链接:{product_link}')
  2032. self.loggerMT.info(f'2-商品链接:{product_link}')
  2033. #如果为获取到product_link 则等待0.5秒再获取
  2034. if not product_link:
  2035. time.sleep(0.5)
  2036. if self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').exists:
  2037. print('3-进入分享点点点111')
  2038. self.loggerMT.info('3-进入分享点点点111')
  2039. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').click()
  2040. #点击分享商品
  2041. # if self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[3]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').exists:
  2042. time.sleep(0.2)
  2043. self.d.xpath('//*[@text="分享商品"]').click_exists()
  2044. time.sleep(0.2)
  2045. self.d.xpath('//*[@text="复制链接"]').click_exists()
  2046. time.sleep(1)
  2047. #获取剪切板的数据
  2048. product_link = self.get_clipboard()
  2049. time.sleep(0.5)
  2050. print(f'3-商品链接:{product_link}')
  2051. self.loggerMT.info(f'3-商品链接:{product_link}')
  2052. elif self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').exists:
  2053. print('3-进入分享点点点222')
  2054. self.loggerMT.info('3-进入分享点点点222')
  2055. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[3]/android.widget.FrameLayout[1]/android.view.ViewGroup[3]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]').click()
  2056. time.sleep(0.2)
  2057. self.d.xpath('//*[@text="分享商品"]').click_exists()
  2058. time.sleep(0.2)
  2059. self.d.xpath('//*[@text="复制链接"]').click_exists()
  2060. time.sleep(1)
  2061. #获取剪切板的数据
  2062. product_link = self.get_clipboard()
  2063. time.sleep(0.5)
  2064. print(f'3-商品链接:{product_link}')
  2065. self.loggerMT.info(f'3-商品链接:{product_link}')
  2066. '''
  2067. #获取链接结束
  2068. """
  2069. 整合数据
  2070. :return:
  2071. """
  2072. # title_info = self.get_title() # 药品,规格
  2073. # title_info = self.safe_exec(self.get_title) # 药品,规格
  2074. product, specifications = self.safe_exec(self.get_title) # 药品,规格
  2075. if product:
  2076. # product, specifications = title_info
  2077. #如果关键字包含999 则 product必须包含999 和 999后面的那段字符串 ps 999感冒灵颗粒必须包含:"999"和"感冒灵颗粒"
  2078. if '999' in self.search_key:
  2079. if self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  2080. temp_search_key = self.search_key.replace('999皮炎平', '')
  2081. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  2082. temp_search_key = self.search_key.replace('999必无忧', '')
  2083. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  2084. temp_search_key = self.search_key.replace('999必无忧', '')
  2085. elif self.search_key == '999速复康布洛芬缓释胶囊':
  2086. temp_search_key = self.search_key.replace('999速复康', '')
  2087. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  2088. temp_search_key = self.search_key.replace('999选平', '')
  2089. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  2090. temp_search_key = self.search_key.replace('999皮炎平', '')
  2091. else:
  2092. temp_search_key = self.search_key.replace('999', '')
  2093. if self.search_key == '999糠酸莫米松凝胶15':
  2094. temp_search_key = temp_search_key.replace('15', '')
  2095. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  2096. temp_search_key = temp_search_key.replace('30', '')
  2097. elif self.search_key == '999抗病毒口服液10ml*6支/盒':
  2098. temp_search_key = temp_search_key.replace("10ml*6支/盒", "")
  2099. elif self.search_key == '999复方金银花颗粒10g':
  2100. temp_search_key = temp_search_key.replace("10g", "")
  2101. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  2102. temp_search_key = temp_search_key.replace("15g*15袋/盒", "")
  2103. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  2104. temp_search_key = temp_search_key.replace("6粒", "")
  2105. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  2106. temp_search_key = temp_search_key.replace("50", "")
  2107. elif self.search_key == '999止泻利颗粒15g*8':
  2108. temp_search_key = temp_search_key.replace("15g*8", "")
  2109. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  2110. temp_search_key = temp_search_key.replace("30", "")
  2111. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  2112. temp_search_key = temp_search_key.replace("15g", "")
  2113. elif self.search_key == '999复方苦参肠炎康片12片':
  2114. temp_search_key = temp_search_key.replace("12片", "")
  2115. elif self.search_key == '999强力枇杷露16袋':
  2116. temp_search_key = temp_search_key.replace("16袋", "")
  2117. elif self.search_key == '999三蛇胆川贝膏138':
  2118. temp_search_key = temp_search_key.replace("138", "")
  2119. elif self.search_key == '999抗病毒口服液10ml*12':
  2120. temp_search_key = temp_search_key.replace("10ml*12", "")
  2121. elif self.search_key == '999抗病毒口服液10ml*10':
  2122. temp_search_key = temp_search_key.replace("10ml*10", "")
  2123. elif self.search_key == '999强力枇杷露120ml':
  2124. temp_search_key = temp_search_key.replace("120ml", "")
  2125. elif self.search_key == '999强力枇杷露150ml':
  2126. temp_search_key = temp_search_key.replace("150ml", "")
  2127. elif self.search_key == '999糠酸莫米松乳膏10g支':
  2128. temp_search_key = temp_search_key.replace("10g支", "")
  2129. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  2130. temp_search_key = temp_search_key.replace("20g", "")
  2131. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  2132. temp_search_key = temp_search_key.replace("(无糖)6g", "")
  2133. elif self.search_key == '999壮骨关节丸6g*20':
  2134. temp_search_key = temp_search_key.replace("6g*20", "")
  2135. elif self.search_key == '999正天丸6g*15':
  2136. temp_search_key = temp_search_key.replace("6g*15", "")
  2137. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  2138. temp_search_key = temp_search_key.replace("20", "")
  2139. elif self.search_key == '999糠酸莫米松凝胶10':
  2140. temp_search_key = temp_search_key.replace("10", "")
  2141. elif self.search_key == '999板蓝根颗粒10g*20':
  2142. temp_search_key = temp_search_key.replace("10g*20", "")
  2143. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  2144. temp_search_key = temp_search_key.replace("10粒", "")
  2145. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  2146. temp_search_key = temp_search_key.replace("12粒", "")
  2147. elif self.search_key == '999咽炎片0.26g*12片*2板':
  2148. temp_search_key = temp_search_key.replace("0.26g*12片*2板", "")
  2149. elif self.search_key == '999小儿止咳糖浆120':
  2150. temp_search_key = temp_search_key.replace("120", "")
  2151. elif self.search_key == '999小儿止咳糖浆225':
  2152. temp_search_key = temp_search_key.replace("225", "")
  2153. elif self.search_key == '999小儿感冒颗粒6g*10':
  2154. temp_search_key = temp_search_key.replace("6g*10", "")
  2155. elif self.search_key == '999小儿感冒颗粒6g*24':
  2156. temp_search_key = temp_search_key.replace("6g*24", "")
  2157. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  2158. temp_search_key = temp_search_key.replace("6g*10袋", "")
  2159. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  2160. temp_search_key = temp_search_key.replace("6g*20袋", "")
  2161. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  2162. temp_search_key = temp_search_key.replace("8g*10袋", "")
  2163. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  2164. temp_search_key = temp_search_key.replace("2.5g*10袋", "")
  2165. elif self.search_key == '999感冒清热颗粒12g*18':
  2166. temp_search_key = temp_search_key.replace("12g*18", "")
  2167. elif self.search_key == '999小柴胡颗粒10g*15':
  2168. temp_search_key = temp_search_key.replace("10g*15", "")
  2169. if '999' not in product or temp_search_key not in product:
  2170. self.swipe_back(1)
  2171. self.unrelated_data += 1
  2172. return
  2173. else:
  2174. if self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  2175. temp_search_key = self.search_key.replace('史达功', '')
  2176. temp_search_key = temp_search_key.replace('120', '')
  2177. if '史达功' not in product or temp_search_key not in product:
  2178. self.swipe_back(1)
  2179. self.unrelated_data += 1
  2180. return
  2181. elif self.search_key == '三九胃泰养胃舒颗粒8袋':
  2182. temp_search_key = self.search_key.replace('三九胃泰', '')
  2183. temp_search_key = temp_search_key.replace('8袋', '')
  2184. if '三九胃泰' not in product or temp_search_key not in product:
  2185. self.swipe_back(1)
  2186. self.unrelated_data += 1
  2187. return
  2188. elif self.search_key == '今维多赐多康牌蛋白粉':
  2189. temp_search_key = self.search_key.replace('今维多', '')
  2190. if '今维多' not in product or temp_search_key not in product:
  2191. self.swipe_back(1)
  2192. self.unrelated_data += 1
  2193. return
  2194. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  2195. temp_search_key = self.search_key.replace('佳美舒', '')
  2196. temp_search_key = temp_search_key.replace('4', '')
  2197. if '佳美舒' not in product or temp_search_key not in product:
  2198. self.swipe_back(1)
  2199. self.unrelated_data += 1
  2200. return
  2201. elif self.search_key == '三九胃泰颗粒20g*10':
  2202. temp_search_key = self.search_key.replace('20g*10', '')
  2203. if temp_search_key not in product:
  2204. self.swipe_back(1)
  2205. self.unrelated_data += 1
  2206. return
  2207. elif self.search_key == '三九胃泰颗粒20g*6袋':
  2208. temp_search_key = self.search_key.replace('20g*6袋', '')
  2209. if temp_search_key not in product:
  2210. self.swipe_back(1)
  2211. self.unrelated_data += 1
  2212. return
  2213. elif self.search_key == '顺峰康王酮康他索乳膏':
  2214. temp_search_key = self.search_key.replace('顺峰康王', '')
  2215. if '顺峰康王' not in product or temp_search_key not in product:
  2216. self.swipe_back(1)
  2217. self.unrelated_data += 1
  2218. return
  2219. else:
  2220. if self.search_key not in product.replace(' ', ''):
  2221. self.swipe_back(1)
  2222. self.unrelated_data += 1
  2223. return
  2224. # if self.search_key not in product.replace(' ', ''):
  2225. # self.swipe_back(1)
  2226. # self.unrelated_data += 1
  2227. # return
  2228. else:
  2229. self.swipe_back(1)
  2230. return
  2231. min_price = self.drug_price() # 最低价格
  2232. sales_num = self.drug_sale_num() #销售数量
  2233. snapshot_url = '' #网页快照
  2234. # 在这里截图存放到OSS;#采集图片存放的oss_url;
  2235. # mt_screenshot = MTScreenshot(
  2236. # d=self.d,
  2237. # oss_config=self.oss_config,
  2238. # search_key=self.search_key # 添加这行
  2239. # )
  2240. # snapshot_url = mt_screenshot.get_oss_url() #网页快照
  2241. #判断是否有自营的文本,有的话不需要获取店铺的信息
  2242. if self.d.xpath('//*[@text="自营"]').exists:
  2243. shop = "美团自营大药房(快递电商)"
  2244. # 爬取日期
  2245. scrape_date = self.get_current_date()
  2246. # scrape_date = "2025-07-18"
  2247. dup_data = {'product': product, 'min_price': min_price, 'shop': shop, 'scrape_date': scrape_date,
  2248. 'platform': '美团'}
  2249. print(f'当前数据:{dup_data}')
  2250. if self.data_is_exists(dup_data):
  2251. print('存在相同数据不入库')
  2252. self.swipe_back(1)
  2253. return
  2254. else:
  2255. for i in range(8):
  2256. if self.d.xpath('//*[@text="进店"]').exists:
  2257. print('开始获取店铺名1')
  2258. break
  2259. self.d.swipe_ext('up', 0.3)
  2260. time.sleep(1)
  2261. # detail_info = self.d.xpath(
  2262. # '//android.widget.ScrollView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[6]').info
  2263. # bounds = detail_info['bounds']
  2264. # height = bounds['bottom'] - bounds['top']
  2265. # if self.d.xpath('//*[@text="进店"]').exists and height > 100:
  2266. if self.d.xpath('//*[@text="进店"]').exists:
  2267. print('开始获取店铺名2')
  2268. break
  2269. shop = self.get_shop_name()
  2270. # 爬取日期
  2271. scrape_date = self.get_current_date()
  2272. # scrape_date = "2025-07-18"
  2273. dup_data = {'product': product, 'min_price': min_price, 'shop': shop, 'scrape_date': scrape_date,
  2274. 'platform': '美团'}
  2275. print(f'当前数据:{dup_data}')
  2276. #获取店铺信息开始
  2277. #暂时不获取店铺信息 start
  2278. is_has_enter_shop = self.has_shop()
  2279. #需要判断shop是否已经在数据库中存在,如果存在,则不再进入店铺,直接进入下一个商品
  2280. shop_is_exists = self.shop_is_exists_database(shop)
  2281. #存在进店 并且店铺的名称不包含美团官方的字样
  2282. print(f"已采集{self.shop_data_num}家店铺数据")
  2283. if is_has_enter_shop and '美团官方' not in shop and '美团自营' not in shop and not shop_is_exists and self.shop_data_num < 500:
  2284. # license_info = self.get_license_info_ex()
  2285. license_info = self.safe_exec(self.get_license_info_ex)
  2286. contact_address = license_info['contact_address']
  2287. qualification_number = license_info['qualification_number']
  2288. business_license_company = license_info['business_license_company']
  2289. business_license_address = license_info['business_license_address']
  2290. save_shop_data = {
  2291. 'shop': shop,
  2292. 'contact_address': contact_address,
  2293. 'qualification_number': qualification_number,
  2294. 'scrape_date': scrape_date,
  2295. 'business_license_company':business_license_company,
  2296. 'business_license_address':business_license_address,
  2297. 'platform': '美团'
  2298. }
  2299. self.save_shop_info_to_database(save_shop_data)
  2300. self.shop_data_num += 1 # 店铺数据数量+1
  2301. self.swipe_back(2)
  2302. else:
  2303. print('不采集店铺信息')
  2304. #获取店铺信息结束
  2305. #暂时不获取店铺信息 end
  2306. if self.data_is_exists(dup_data):
  2307. print('存在相同数据不入库')
  2308. self.swipe_back(1)
  2309. return
  2310. # 商品链接
  2311. product_link = self.get_product_link()
  2312. if not shop:
  2313. print('未获取到店铺名:开始回退')
  2314. self.swipe_back(1)
  2315. return
  2316. if not shop or '自营' in shop:
  2317. self.swipe_back(1)
  2318. return
  2319. time.sleep(self.get_sleep_time())
  2320. # 生产日期为空
  2321. manufacture_date = ''
  2322. # 执政信息
  2323. # if is_has_enter_shop:
  2324. # license_info = self.get_license_info()
  2325. # business_license_company = license_info["单位名称"]
  2326. # credit_code = license_info['社会信用代码']
  2327. # city_str = license_info['地址']
  2328. # # 先把省份啥的替换掉
  2329. # city_sub_str = re.sub(r'[u4e00-\u9fa5]+省', '', city_str)
  2330. # try:
  2331. # city = re.search(r'[\u4e00-\u9fa5]+?(市|区|县)', city_sub_str).group(0)
  2332. # except:
  2333. # city = city_sub_str
  2334. # try:
  2335. # province = self.city2province[city]
  2336. # except:
  2337. # province = ''
  2338. # self.swipe_back(2)
  2339. # else:
  2340. # business_license_company = ''
  2341. # credit_code = ''
  2342. # city = ''
  2343. # province = ''
  2344. business_license_company = ''
  2345. credit_code = ''
  2346. city = ''
  2347. province = ''
  2348. expiry_date = ''
  2349. manufacturer = ''
  2350. approval_number = ''
  2351. #暂时不获取说明书信息 start
  2352. #是否存在说明书
  2353. # is_has_instructions = self.has_instructions()
  2354. #有的药品没有说明书,直接默认
  2355. if self.search_key == '今维多赐多康牌蛋白粉':
  2356. expiry_date = '18个月'
  2357. manufacturer = '华润圣海健康科技有限公司'
  2358. approval_number = '食健备G202437001992'
  2359. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  2360. expiry_date = '24个月'
  2361. manufacturer = '浙江华润三九众益制药有限公司'
  2362. approval_number = '国药准字H20090152'
  2363. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  2364. expiry_date = '3年'
  2365. manufacturer = '江苏萨瑞斯医疗科技有限公司'
  2366. approval_number = '苏械注准20212140025'
  2367. elif self.search_key == '999蒲地蓝消炎片':
  2368. expiry_date = '24个月'
  2369. manufacturer = '特一药业集团股份有限公司'
  2370. approval_number = '国药准字Z20063596'
  2371. elif self.search_key == '999养胃舒颗粒':
  2372. expiry_date = '36个月'
  2373. manufacturer = '合肥华润神鹿药业有限公司'
  2374. approval_number = '国药准字Z34020289'
  2375. elif self.search_key == '999糠酸莫米松凝胶15':
  2376. expiry_date = '36个月'
  2377. manufacturer = '华润三九(南昌)药业有限公司'
  2378. approval_number = '国药准字H20080010'
  2379. elif self.search_key == '999黄芪精':
  2380. expiry_date = '36个月'
  2381. manufacturer = '台州南峰药业有限公司'
  2382. approval_number = '国药准字Z33020783'
  2383. elif self.search_key == '999复方感冒灵颗粒':
  2384. expiry_date = '24个月'
  2385. manufacturer = '华润三九(郴州)制药有限公司'
  2386. approval_number = '国药准字Z43020334'
  2387. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  2388. expiry_date = '36个月'
  2389. manufacturer = '华润三九(南昌)药业有限公司'
  2390. approval_number = '国药准字H20074155'
  2391. elif self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  2392. expiry_date = '暂定24个月,具体有效期以实物说明书为准'
  2393. manufacturer = '史达德药业(北京)有限公司'
  2394. approval_number = '国药准字H11021837'
  2395. elif self.search_key == '999速复康布洛芬缓释胶囊':
  2396. expiry_date = '24个月'
  2397. manufacturer = '北京红林制药有限公司'
  2398. approval_number = '国药准字H20074172'
  2399. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  2400. expiry_date = '24个月'
  2401. manufacturer = '重庆科瑞东和制药有限责任公司'
  2402. approval_number = '国药准字Z50020420'
  2403. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  2404. expiry_date = '24个月'
  2405. manufacturer = '华润三九(南昌)药业有限公司'
  2406. approval_number = '国药准字H20073954'
  2407. elif self.search_key == '999维生素C咀嚼片':
  2408. expiry_date = '24个月'
  2409. manufacturer = '甘肃成纪生物药业有限公司'
  2410. approval_number = '国药准字H62021166'
  2411. elif self.search_key == '999强力枇杷露120ml':
  2412. expiry_date = '36个月'
  2413. manufacturer = '华润三九(南昌)药业有限公司'
  2414. approval_number = '国药准字Z36021533'
  2415. elif self.search_key == '999强力枇杷露150ml':
  2416. expiry_date = '36个月'
  2417. manufacturer = '华润三九(南昌)药业有限公司'
  2418. approval_number = '国药准字Z36021533'
  2419. elif self.search_key == '999抗病毒口服液10ml*10' or self.search_key == '999抗病毒口服液10ml*12':
  2420. expiry_date = '24个月'
  2421. manufacturer = '杭州华润老桐君药业有限公司'
  2422. approval_number = '国药准字Z33020518'
  2423. elif self.search_key == '999精氨酸布洛芬颗粒':
  2424. expiry_date = '暂定36个月'
  2425. manufacturer = '华润三九(唐山)药业有限公司'
  2426. approval_number = '国药准字H20070139'
  2427. elif self.search_key == '999糠酸莫米松乳膏10g支':
  2428. expiry_date = '36个月'
  2429. manufacturer = '华润三九(南昌)药业有限公司'
  2430. approval_number = '国药准字H20074090'
  2431. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  2432. expiry_date = '24个月'
  2433. manufacturer = '华润三九(南昌)药业有限公司'
  2434. approval_number = '国药准字H20074079'
  2435. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  2436. expiry_date = '36个月'
  2437. manufacturer = '合肥华润神鹿药业有限公司'
  2438. approval_number = '国药准字Z20055023'
  2439. elif self.search_key == '999银菊清咽颗粒':
  2440. expiry_date = '30个月'
  2441. manufacturer = '合肥华润神鹿药业有限公司'
  2442. approval_number = '国药准字Z20026680'
  2443. elif self.search_key == '999阿奇霉素片':
  2444. expiry_date = '48个月'
  2445. manufacturer = '浙江华润三九众益制药有限公司'
  2446. approval_number = '国药准字H20084458'
  2447. elif self.search_key == '999补脾益肠丸':
  2448. expiry_date = '24个月'
  2449. manufacturer = '惠州市九惠制药股份有限公司'
  2450. approval_number = '国药准字Z44023376'
  2451. elif self.search_key == '999壮骨关节丸6g*20':
  2452. expiry_date = '24个月'
  2453. manufacturer = '华润三九医药股份有限公司'
  2454. approval_number = '国药准字Z44023377'
  2455. elif self.search_key == '999壮骨关节胶囊':
  2456. expiry_date = '24个月'
  2457. manufacturer = '华润三九医药股份有限公司'
  2458. approval_number = '国药准字Z20080055'
  2459. elif self.search_key == '999正天丸6g*15':
  2460. expiry_date = '30个月'
  2461. manufacturer = '华润三九医药股份有限公司'
  2462. approval_number = '国药准字Z44020711'
  2463. elif self.search_key == '999正天胶囊':
  2464. expiry_date = '24个月'
  2465. manufacturer = '华润三九医药股份有限公司'
  2466. approval_number = '国药准字Z20010142'
  2467. elif self.search_key == '三九胃泰胶囊':
  2468. expiry_date = '24个月'
  2469. manufacturer = '华润三九医药股份有限公司'
  2470. approval_number = '国药准字Z44020704'
  2471. elif self.search_key == '三九胃泰颗粒20g*10':
  2472. expiry_date = '24个月'
  2473. manufacturer = '华润三九医药股份有限公司'
  2474. approval_number = '国药准字Z44020705'
  2475. elif self.search_key == '999感冒灵颗粒':
  2476. expiry_date = '24个月'
  2477. manufacturer = '华润三九(枣庄)药业有限公司'
  2478. approval_number = '国药准字Z44021940'
  2479. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  2480. expiry_date = '36个月'
  2481. manufacturer = '华润三九医药股份有限公司'
  2482. approval_number = '国药准字H44024170'
  2483. elif self.search_key == '三九胃泰颗粒20g*6袋':
  2484. expiry_date = '24个月'
  2485. manufacturer = '华润三九医药股份有限公司'
  2486. approval_number = '国药准字Z44020705'
  2487. elif self.search_key == '顺峰康王酮康他索乳膏':
  2488. expiry_date = '24个月'
  2489. manufacturer = '广东华润顺峰药业有限公司'
  2490. approval_number = '国药准字H10980204'
  2491. elif self.search_key == '999糠酸莫米松凝胶10':
  2492. expiry_date = '36个月'
  2493. manufacturer = '华润三九(南昌)药业有限公司'
  2494. approval_number = '国药准字H20080010'
  2495. elif self.search_key == '999板蓝根颗粒10g*20':
  2496. expiry_date = '36个月'
  2497. manufacturer = '广东恒诚制药股份有限公司'
  2498. approval_number = '国药准字Z44021520'
  2499. elif self.search_key == '999复方氨酚烷胺胶囊' or self.search_key == '999复方氨酚烷胺胶囊12粒' or self.search_key == '999复方氨酚烷胺胶囊10粒' or self.search_key == '999复方氨酚烷胺胶囊6粒' :
  2500. expiry_date = '36个月'
  2501. manufacturer = '华润三九(唐山)药业有限公司'
  2502. approval_number = '国药准字H13021912'
  2503. elif self.search_key == '999咽炎片0.26g*12片*2板':
  2504. expiry_date = '24个月'
  2505. manufacturer = '华润三九(黄石)药业有限公司'
  2506. approval_number = '国药准字Z42021062'
  2507. elif self.search_key == '999小儿止咳糖浆120' or self.search_key == '999小儿止咳糖浆225':
  2508. expiry_date = '24个月'
  2509. manufacturer = '华润三九(雅安)药业有限公司'
  2510. approval_number = '国药准字Z51020675'
  2511. elif self.search_key == '999小儿感冒颗粒6g*10' or self.search_key == '999小儿感冒颗粒6g*24':
  2512. expiry_date = '36个月'
  2513. manufacturer = '华润三九(枣庄)药业有限公司'
  2514. approval_number = '国药准字Z37021392'
  2515. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋' or self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  2516. expiry_date = '36个月'
  2517. manufacturer = '华润三九(黄石)药业有限公司'
  2518. approval_number = '国药准字H42022510'
  2519. elif self.search_key == '999感冒灵胶囊':
  2520. expiry_date = '24个月'
  2521. manufacturer = '华润三九医药股份有限公司'
  2522. approval_number = '国药准字Z44021939'
  2523. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  2524. expiry_date = '24个月'
  2525. manufacturer = '华润三九(黄石)药业有限公司'
  2526. approval_number = '国药准字Z42021105'
  2527. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  2528. expiry_date = '18个月'
  2529. manufacturer = '华润三九医药股份有限公司'
  2530. approval_number = '国药准字Z20100067'
  2531. elif self.search_key == '999感冒清热颗粒12g*18':
  2532. expiry_date = '36个月'
  2533. manufacturer = '山东新大陆制药有限公司'
  2534. approval_number = '国药准字Z37020066'
  2535. elif self.search_key == '999小柴胡颗粒10g*15':
  2536. expiry_date = '24个月'
  2537. manufacturer = '广东一力罗定制药有限公司'
  2538. approval_number = '国药准字Z44020709'
  2539. elif self.search_key == '999布洛芬混悬液':
  2540. expiry_date = '24个月'
  2541. manufacturer = '华润三九(南昌)药业有限公司'
  2542. approval_number = '国药准字H20223755'
  2543. else:
  2544. is_has_instructions = self.safe_exec(self.has_instructions)
  2545. # 说明书等信息
  2546. if is_has_instructions:
  2547. print('开始获取说明书信息')
  2548. # instructions_info = self.get_instructions_data()
  2549. instructions_info = self.safe_exec(self.get_instructions_data)
  2550. if instructions_info['有效期'] is not None:
  2551. expiry_date = instructions_info['有效期'].strip('。')
  2552. if instructions_info['生产单位'] is not None:
  2553. manufacturer = instructions_info['生产单位'].strip('。')
  2554. if instructions_info['批准文号'] is not None:
  2555. approval_number = instructions_info['批准文号'].strip('。')
  2556. else:
  2557. # 没有说明书不入库
  2558. print('没有获取到说明书信息')
  2559. self.swipe_back(1)
  2560. return
  2561. #暂时不获取说明书信息 end
  2562. self.unrelated_data = 0
  2563. if self.search_key == '999小柴胡颗粒10g*15':
  2564. save_search_key = '999小柴胡颗粒'
  2565. else:
  2566. save_search_key = self.search_key
  2567. # 爬取省份
  2568. scrape_province = '广东' # 这里先默认广东
  2569. # 是否有货
  2570. availability = ''
  2571. save_data = {
  2572. 'product': product,
  2573. 'min_price': min_price,
  2574. 'manufacture_date': manufacture_date,
  2575. 'expiry_date': expiry_date,
  2576. 'shop': shop,
  2577. 'business_license_company': business_license_company,
  2578. 'province': province,
  2579. 'city': city,
  2580. 'manufacturer': manufacturer,
  2581. 'specification': specifications,
  2582. 'approval_number': approval_number,
  2583. 'product_link': product_link,
  2584. 'scrape_date': scrape_date,
  2585. 'scrape_province': scrape_province,
  2586. 'availability': availability,
  2587. 'credit_code': credit_code,
  2588. 'platform': '美团',
  2589. 'search_key': save_search_key,
  2590. 'sales': sales_num,
  2591. 'inventory':'',
  2592. 'snapshot_url':snapshot_url
  2593. }
  2594. self.save_to_database(save_data)
  2595. # time.sleep(100000)
  2596. time.sleep(self.get_sleep_time())
  2597. if self.distinct_target():
  2598. print('已到达搜索列表页')
  2599. else:
  2600. for i in range(1):
  2601. print('在详情页')
  2602. self.swipe_back(1)
  2603. time.sleep(self.get_sleep_time())
  2604. # 最外部有个定位按钮
  2605. if self.distinct_target():
  2606. break
  2607. #主函数
  2608. def main(self, device_id, retry_count=0):
  2609. MAX_RETRY = 3 # 最大重试次数
  2610. spider_no = 0
  2611. self.connect_devices(device_id)
  2612. time.sleep(self.get_sleep_time())
  2613. # self.d.toast.show("测试toast", 20)
  2614. # 启动全局弹窗监控
  2615. self.monitor = SpiderMonitor(self)
  2616. self.monitor.start()
  2617. try:
  2618. # 重新开启美团应用
  2619. self.restart_app()
  2620. # 搜索关键字
  2621. # self.enter_target_page()
  2622. self.safe_exec(self.enter_target_page)
  2623. # print('开始滑动')
  2624. # self.d.drag(300, 1400, 300, 400, 1)
  2625. # time.sleep(100000)
  2626. for idx in range(300):
  2627. print(f'第{idx + 1}页')
  2628. if spider_no > 30:
  2629. time.sleep(60)
  2630. spider_no = 0
  2631. print('目前无关数据量: ', self.unrelated_data)
  2632. # 检查是否需要暂停(验证码过多)
  2633. if self.monitor.verification_count >= self.monitor.MAX_VERIFICATION_RETRY:
  2634. print("频繁遇到验证码,暂停程序")
  2635. # self.d.toast("请处理验证码后点击继续", 30)
  2636. # 等待用户点击屏幕继续
  2637. self.d.click(0, 0) # 无效点击,等待用户操作
  2638. self.monitor.verification_count = 0
  2639. if self.unrelated_data > 15:
  2640. # 连续超过15个不达标的数据则停止采集
  2641. print("连续超过15个不达标的数据则停止采集")
  2642. return
  2643. # 线程安全获取商品列表
  2644. # drug_lis = self.d.xpath('//android.support.v7.widget.RecyclerView/android.widget.FrameLayout').all()
  2645. # drug_lis = self.safe_list('//android.support.v7.widget.RecyclerView/android.widget.FrameLayout', self.monitor)
  2646. while True:
  2647. if self.d.xpath('//android.support.v7.widget.RecyclerView/android.widget.FrameLayout').exists:
  2648. break
  2649. time.sleep(1)
  2650. drug_lis = self.safe_exec(self.d.xpath('//android.support.v7.widget.RecyclerView/android.widget.FrameLayout').all)
  2651. lis_len = len(drug_lis)
  2652. print(f'当前页面共有{lis_len}个商品')
  2653. for idxx,drug_one in enumerate(drug_lis,start = 1):
  2654. bounds = drug_one.info['bounds']
  2655. top = bounds['top']
  2656. bottom = bounds['bottom']
  2657. # height = bottom - top
  2658. print(f'当前商品bottom:{bottom}')
  2659. print(f'当前商品top:{top}')
  2660. # if 304 <= top and bottom <= 1475: # 默认高度241的才行
  2661. if 304 <= top and bottom <= 1475: # 默认高度241的才行 1559
  2662. # print('目标-->', drug_one.info)
  2663. # drug_one.click()
  2664. #获取当前元素中的属性来判断是否要点击进入采集
  2665. print(f"这页的第几个商品:{idxx}")
  2666. product_title = ''
  2667. price = ''
  2668. shop_name = ''
  2669. #商品名称的xpath
  2670. product_tittle_xpath = f'//android.support.v7.widget.RecyclerView/android.widget.FrameLayout[{idxx}]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView'
  2671. product_tittle_xpath2 = f'//android.support.v7.widget.RecyclerView/android.widget.FrameLayout[{idxx}]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView'
  2672. if self.d.xpath(product_tittle_xpath).exists:
  2673. product_title = self.d.xpath(product_tittle_xpath).text
  2674. product_title = product_title[1:] if product_title.startswith('0') else product_title
  2675. print(f"product_tittle_xpath列表当前商品名称:{product_title}")
  2676. if '999' in self.search_key:
  2677. if self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  2678. temp_search_key = self.search_key.replace('999皮炎平', '')
  2679. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  2680. temp_search_key = self.search_key.replace('999必无忧', '')
  2681. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  2682. temp_search_key = self.search_key.replace('999必无忧', '')
  2683. elif self.search_key == '999速复康布洛芬缓释胶囊':
  2684. temp_search_key = self.search_key.replace('999速复康', '')
  2685. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  2686. temp_search_key = self.search_key.replace('999选平', '')
  2687. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  2688. temp_search_key = self.search_key.replace('999皮炎平', '')
  2689. else:
  2690. temp_search_key = self.search_key.replace('999', '')
  2691. if self.search_key == '999糠酸莫米松凝胶15':
  2692. temp_search_key = temp_search_key.replace('15', '')
  2693. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  2694. temp_search_key = temp_search_key.replace('30', '')
  2695. elif self.search_key == '999复方金银花颗粒10g':
  2696. temp_search_key = temp_search_key.replace('10g', '')
  2697. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  2698. temp_search_key = temp_search_key.replace('15g*15袋/盒', '')
  2699. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  2700. temp_search_key = temp_search_key.replace('6粒', '')
  2701. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  2702. temp_search_key = temp_search_key.replace('50', '')
  2703. elif self.search_key == '999止泻利颗粒15g*8':
  2704. temp_search_key = temp_search_key.replace('15g*8', '')
  2705. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  2706. temp_search_key = temp_search_key.replace('30', '')
  2707. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  2708. temp_search_key = temp_search_key.replace('15g', '')
  2709. elif self.search_key == '999复方苦参肠炎康片12片':
  2710. temp_search_key = temp_search_key.replace("12片", "")
  2711. elif self.search_key == '999强力枇杷露16袋':
  2712. temp_search_key = temp_search_key.replace("16袋", "")
  2713. elif self.search_key == '999三蛇胆川贝膏138':
  2714. temp_search_key = temp_search_key.replace("138", "")
  2715. elif self.search_key == '999抗病毒口服液10ml*12':
  2716. temp_search_key = temp_search_key.replace("10ml*12", "")
  2717. elif self.search_key == '999抗病毒口服液10ml*10':
  2718. temp_search_key = temp_search_key.replace("10ml*10", "")
  2719. elif self.search_key == '999强力枇杷露120ml':
  2720. temp_search_key = temp_search_key.replace("120ml", "")
  2721. elif self.search_key == '999强力枇杷露150ml':
  2722. temp_search_key = temp_search_key.replace("150ml", "")
  2723. elif self.search_key == '999糠酸莫米松乳膏10g支':
  2724. temp_search_key = temp_search_key.replace("10g支", "")
  2725. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  2726. temp_search_key = temp_search_key.replace("20g", "")
  2727. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  2728. temp_search_key = temp_search_key.replace("(无糖)6g", "")
  2729. elif self.search_key == '999壮骨关节丸6g*20':
  2730. temp_search_key = temp_search_key.replace("6g*20", "")
  2731. elif self.search_key == '999正天丸6g*15':
  2732. temp_search_key = temp_search_key.replace("6g*15", "")
  2733. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  2734. temp_search_key = temp_search_key.replace("20", "")
  2735. elif self.search_key == '999糠酸莫米松凝胶10':
  2736. temp_search_key = temp_search_key.replace("10", "")
  2737. elif self.search_key == '999板蓝根颗粒10g*20':
  2738. temp_search_key = temp_search_key.replace("10g*20", "")
  2739. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  2740. temp_search_key = temp_search_key.replace("10粒", "")
  2741. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  2742. temp_search_key = temp_search_key.replace("12粒", "")
  2743. elif self.search_key == '999咽炎片0.26g*12片*2板':
  2744. temp_search_key = temp_search_key.replace("0.26g*12片*2板", "")
  2745. elif self.search_key == '999小儿止咳糖浆120':
  2746. temp_search_key = temp_search_key.replace("120", "")
  2747. elif self.search_key == '999小儿止咳糖浆225':
  2748. temp_search_key = temp_search_key.replace("225", "")
  2749. elif self.search_key == '999小儿感冒颗粒6g*10':
  2750. temp_search_key = temp_search_key.replace("6g*10", "")
  2751. elif self.search_key == '999小儿感冒颗粒6g*24':
  2752. temp_search_key = temp_search_key.replace("6g*24", "")
  2753. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  2754. temp_search_key = temp_search_key.replace("6g*10袋", "")
  2755. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  2756. temp_search_key = temp_search_key.replace("6g*20袋", "")
  2757. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  2758. temp_search_key = temp_search_key.replace("8g*10袋", "")
  2759. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  2760. temp_search_key = temp_search_key.replace("2.5g*10袋", "")
  2761. elif self.search_key == '999感冒清热颗粒12g*18':
  2762. temp_search_key = temp_search_key.replace("12g*18", "")
  2763. elif self.search_key == '999小柴胡颗粒10g*15':
  2764. temp_search_key = temp_search_key.replace("10g*15", "")
  2765. if self.search_key == '999抗病毒口服液':
  2766. if '999' not in product_title or temp_search_key not in product_title :
  2767. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2768. continue
  2769. elif '10ml*12' not in product_title and '10ml*10' not in product_title:
  2770. print(f"当前商品名称:{product_title} 不包含10ml*12和10ml*10品规")
  2771. continue
  2772. elif self.search_key == '999抗病毒口服液10ml*12':
  2773. if '999' not in product_title or temp_search_key not in product_title :
  2774. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2775. continue
  2776. elif '10ml*12' not in product_title:
  2777. print(f"当前商品名称:{product_title} 不包含10ml*12品规")
  2778. continue
  2779. elif self.search_key == '999抗病毒口服液10ml*10':
  2780. if '999' not in product_title or temp_search_key not in product_title :
  2781. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2782. continue
  2783. elif '10ml*10' not in product_title:
  2784. print(f"当前商品名称:{product_title} 不包含10ml*10品规")
  2785. continue
  2786. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  2787. if '999' not in product_title or temp_search_key not in product_title :
  2788. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2789. continue
  2790. elif '30' not in product_title:
  2791. print(f"当前商品名称:{product_title} 不包含30品规")
  2792. continue
  2793. elif self.search_key == '999复方感冒灵颗粒':
  2794. if '999' not in product_title or temp_search_key not in product_title :
  2795. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2796. continue
  2797. # elif '14g*15' not in product_title and '14g*9' not in product_title:
  2798. elif '14g*15' not in product_title:
  2799. print(f"当前商品名称:{product_title} 不包含14g*15品规")
  2800. continue
  2801. elif self.search_key == '999养胃舒颗粒':
  2802. if '999' not in product_title or temp_search_key not in product_title :
  2803. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2804. continue
  2805. elif '10g*10' not in product_title:
  2806. print(f"当前商品名称:{product_title} 不包含10g*10品规")
  2807. continue
  2808. elif self.search_key == '999糠酸莫米松凝胶15':
  2809. if '999' not in product_title or temp_search_key not in product_title :
  2810. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2811. continue
  2812. elif '15' not in product_title:
  2813. print(f"当前商品名称:{product_title} 不包含15品规")
  2814. continue
  2815. elif self.search_key == '999复方金银花颗粒10g':
  2816. if '999' not in product_title or temp_search_key not in product_title :
  2817. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2818. continue
  2819. elif '10g' not in product_title:
  2820. print(f"当前商品名称:{product_title} 不包含10g品规")
  2821. continue
  2822. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  2823. if '999' not in product_title or temp_search_key not in product_title :
  2824. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2825. continue
  2826. elif '15g*15' not in product_title:
  2827. print(f"当前商品名称:{product_title} 不包含15g*15品规")
  2828. continue
  2829. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  2830. if '999' not in product_title or temp_search_key not in product_title :
  2831. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2832. continue
  2833. elif '6' not in product_title:
  2834. print(f"当前商品名称:{product_title} 不包含6品规")
  2835. continue
  2836. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  2837. if '999' not in product_title or temp_search_key not in product_title :
  2838. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2839. continue
  2840. elif '50' not in product_title:
  2841. print(f"当前商品名称:{product_title} 不包含50品规")
  2842. continue
  2843. elif self.search_key == '999止泻利颗粒15g*8':
  2844. if '999' not in product_title or temp_search_key not in product_title :
  2845. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2846. continue
  2847. elif '15g*8' not in product_title:
  2848. print(f"当前商品名称:{product_title} 不包含15g*8品规")
  2849. continue
  2850. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  2851. if '999' not in product_title or temp_search_key not in product_title :
  2852. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2853. continue
  2854. elif '30' not in product_title:
  2855. print(f"当前商品名称:{product_title} 不包含30品规")
  2856. continue
  2857. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  2858. if '999' not in product_title or temp_search_key not in product_title :
  2859. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2860. continue
  2861. elif '15' not in product_title:
  2862. print(f"当前商品名称:{product_title} 不包含15品规")
  2863. continue
  2864. elif self.search_key == '999复方苦参肠炎康片12片':
  2865. if '999' not in product_title or temp_search_key not in product_title :
  2866. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2867. continue
  2868. elif '12' not in product_title:
  2869. print(f"当前商品名称:{product_title} 不包含12品规")
  2870. continue
  2871. elif self.search_key == '999强力枇杷露16袋':
  2872. if '999' not in product_title or temp_search_key not in product_title :
  2873. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2874. continue
  2875. elif '16' not in product_title:
  2876. print(f"当前商品名称:{product_title} 不包含16品规")
  2877. continue
  2878. elif self.search_key == '999三蛇胆川贝膏138':
  2879. if '999' not in product_title or temp_search_key not in product_title :
  2880. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2881. continue
  2882. elif '138' not in product_title:
  2883. print(f"当前商品名称:{product_title} 不包含16品规")
  2884. continue
  2885. elif self.search_key == '999速复康布洛芬缓释胶囊':
  2886. if '999' not in product_title or temp_search_key not in product_title :
  2887. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2888. continue
  2889. elif self.search_key == '999维生素C咀嚼片':
  2890. if '999' not in product_title or temp_search_key not in product_title :
  2891. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2892. continue
  2893. elif '80' not in product_title:
  2894. print(f"当前商品名称:{product_title} 不包含80品规")
  2895. elif self.search_key == '999精氨酸布洛芬颗粒':
  2896. if '999' not in product_title or temp_search_key not in product_title :
  2897. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2898. continue
  2899. elif '9' not in product_title:
  2900. print(f"当前商品名称:{product_title} 不包含9品规")
  2901. continue
  2902. elif self.search_key == '999强力枇杷露120ml':
  2903. if '999' not in product_title or temp_search_key not in product_title :
  2904. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2905. continue
  2906. elif '120' not in product_title:
  2907. print(f"当前商品名称:{product_title} 不包含120品规")
  2908. continue
  2909. elif self.search_key == '999强力枇杷露150ml':
  2910. if '999' not in product_title or temp_search_key not in product_title :
  2911. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2912. continue
  2913. elif '150' not in product_title:
  2914. print(f"当前商品名称:{product_title} 不包含120品规")
  2915. continue
  2916. elif self.search_key == '999糠酸莫米松乳膏10g支':
  2917. if '999' not in product_title or temp_search_key not in product_title :
  2918. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2919. continue
  2920. elif '10' not in product_title:
  2921. print(f"当前商品名称:{product_title} 不包含10品规")
  2922. continue
  2923. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  2924. if '999' not in product_title or temp_search_key not in product_title :
  2925. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2926. continue
  2927. elif '20' not in product_title:
  2928. print(f"当前商品名称:{product_title} 不包含20品规")
  2929. continue
  2930. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  2931. if '999' not in product_title or temp_search_key not in product_title :
  2932. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2933. continue
  2934. elif '6' not in product_title:
  2935. print(f"当前商品名称:{product_title} 不包含6品规")
  2936. continue
  2937. elif self.search_key == '999阿奇霉素片':
  2938. if '999' not in product_title or temp_search_key not in product_title :
  2939. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2940. continue
  2941. elif '0.25g*6' not in product_title:
  2942. print(f"当前商品名称:{product_title} 不包含0.25g*6品规")
  2943. continue
  2944. elif self.search_key == '999补脾益肠丸':
  2945. if '999' not in product_title or temp_search_key not in product_title :
  2946. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2947. continue
  2948. elif '6g*15' not in product_title:
  2949. print(f"当前商品名称:{product_title} 不包含6g*15品规")
  2950. continue
  2951. elif self.search_key == '999壮骨关节丸6g*20':
  2952. if '999' not in product_title or temp_search_key not in product_title :
  2953. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2954. continue
  2955. elif '6g*20' not in product_title:
  2956. print(f"当前商品名称:{product_title} 不包含6g*20品规")
  2957. continue
  2958. elif self.search_key == '999正天丸6g*15':
  2959. if '999' not in product_title or temp_search_key not in product_title :
  2960. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2961. continue
  2962. elif '6g*15' not in product_title:
  2963. print(f"当前商品名称:{product_title} 不包含6g*15品规")
  2964. continue
  2965. elif self.search_key == '999感冒灵颗粒':
  2966. if '999' not in product_title or temp_search_key not in product_title :
  2967. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2968. continue
  2969. elif '10g*9' not in product_title:
  2970. print(f"当前商品名称:{product_title} 不包含10g*9品规")
  2971. continue
  2972. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  2973. if '999' not in product_title or temp_search_key not in product_title :
  2974. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2975. continue
  2976. elif '20' not in product_title:
  2977. print(f"当前商品名称:{product_title} 不包含20品规")
  2978. continue
  2979. elif self.search_key == '999糠酸莫米松凝胶10':
  2980. if '999' not in product_title or temp_search_key not in product_title :
  2981. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2982. continue
  2983. elif '10' not in product_title:
  2984. print(f"当前商品名称:{product_title} 不包含10品规")
  2985. continue
  2986. elif self.search_key == '999板蓝根颗粒10g*20':
  2987. if '999' not in product_title or temp_search_key not in product_title :
  2988. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2989. continue
  2990. elif '10g*20' not in product_title:
  2991. print(f"当前商品名称:{product_title} 不包含10g*20品规")
  2992. continue
  2993. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  2994. if '999' not in product_title or temp_search_key not in product_title :
  2995. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  2996. continue
  2997. elif '10' not in product_title:
  2998. print(f"当前商品名称:{product_title} 不包含10品规")
  2999. continue
  3000. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  3001. if '999' not in product_title or temp_search_key not in product_title :
  3002. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3003. continue
  3004. elif '12' not in product_title:
  3005. print(f"当前商品名称:{product_title} 不包含12品规")
  3006. continue
  3007. elif self.search_key == '999复方氨酚烷胺胶囊':
  3008. if '999' not in product_title or temp_search_key not in product_title :
  3009. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3010. continue
  3011. elif self.search_key == '999咽炎片0.26g*12片*2板':
  3012. if '999' not in product_title or temp_search_key not in product_title :
  3013. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3014. continue
  3015. elif '0.26g*12' not in product_title:
  3016. print(f"当前商品名称:{product_title} 不包含0.26g*12品规")
  3017. continue
  3018. elif self.search_key == '999小儿止咳糖浆120':
  3019. if '999' not in product_title or temp_search_key not in product_title :
  3020. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3021. continue
  3022. elif '120' not in product_title:
  3023. print(f"当前商品名称:{product_title} 不包含120品规")
  3024. continue
  3025. elif self.search_key == '999小儿止咳糖浆225':
  3026. if '999' not in product_title or temp_search_key not in product_title :
  3027. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3028. continue
  3029. elif '225' not in product_title:
  3030. print(f"当前商品名称:{product_title} 不包含225品规")
  3031. continue
  3032. elif self.search_key == '999小儿感冒颗粒6g*10':
  3033. if '999' not in product_title or temp_search_key not in product_title :
  3034. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3035. continue
  3036. elif '6g*10' not in product_title:
  3037. print(f"当前商品名称:{product_title} 不包含6g*10品规")
  3038. continue
  3039. elif self.search_key == '999小儿感冒颗粒6g*24':
  3040. if '999' not in product_title or temp_search_key not in product_title :
  3041. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3042. continue
  3043. elif '6g*24' not in product_title:
  3044. print(f"当前商品名称:{product_title} 不包含6g*24品规")
  3045. continue
  3046. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  3047. if '999' not in product_title or temp_search_key not in product_title :
  3048. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3049. continue
  3050. elif '6g*10' not in product_title:
  3051. print(f"当前商品名称:{product_title} 不包含6g*10品规")
  3052. continue
  3053. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  3054. if '999' not in product_title or temp_search_key not in product_title :
  3055. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3056. continue
  3057. elif '6g*20' not in product_title:
  3058. print(f"当前商品名称:{product_title} 不包含6g*20品规")
  3059. continue
  3060. elif self.search_key == '999感冒灵胶囊':
  3061. if '999' not in product_title or temp_search_key not in product_title :
  3062. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3063. continue
  3064. elif '0.5g*12' not in product_title:
  3065. print(f"当前商品名称:{product_title} 不包含0.5g*12品规")
  3066. continue
  3067. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  3068. if '999' not in product_title or temp_search_key not in product_title :
  3069. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3070. continue
  3071. elif '8g*10' not in product_title:
  3072. print(f"当前商品名称:{product_title} 不包含8g*10品规")
  3073. continue
  3074. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  3075. if '999' not in product_title or temp_search_key not in product_title :
  3076. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3077. continue
  3078. elif '2.5g*10' not in product_title:
  3079. print(f"当前商品名称:{product_title} 不包含2.5g*10品规")
  3080. continue
  3081. elif self.search_key == '999感冒清热颗粒12g*18':
  3082. if '999' not in product_title or temp_search_key not in product_title :
  3083. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3084. continue
  3085. elif '12g*18' not in product_title:
  3086. print(f"当前商品名称:{product_title} 不包含12g*18品规")
  3087. continue
  3088. elif self.search_key == '999小柴胡颗粒10g*15':
  3089. if '999' not in product_title or temp_search_key not in product_title :
  3090. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3091. continue
  3092. elif '10g*15' not in product_title:
  3093. print(f"当前商品名称:{product_title} 不包含10g*15品规")
  3094. continue
  3095. elif self.search_key == '999藿香正气合剂':
  3096. if '999' not in product_title or temp_search_key not in product_title :
  3097. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3098. continue
  3099. elif '10ml*6' not in product_title and '10ml*10' not in product_title:
  3100. print(f"当前商品名称:{product_title} 不包含10ml*6 和 10ml*10品规")
  3101. continue
  3102. else:
  3103. if '999' not in product_title or temp_search_key not in product_title:
  3104. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3105. continue
  3106. else:
  3107. if self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  3108. temp_search_key = self.search_key.replace('史达功', '')
  3109. temp_search_key = temp_search_key.replace('120', '')
  3110. if '史达功' not in product_title or temp_search_key not in product_title :
  3111. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3112. continue
  3113. elif '120' not in product_title:
  3114. print(f"当前商品名称:{product_title} 不包含120品规")
  3115. continue
  3116. elif self.search_key == '三九胃泰养胃舒颗粒8袋':
  3117. temp_search_key = self.search_key.replace('三九胃泰', '')
  3118. temp_search_key = temp_search_key.replace('8袋', '')
  3119. if '三九胃泰' not in product_title or temp_search_key not in product_title :
  3120. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3121. continue
  3122. elif '8' not in product_title:
  3123. print(f"当前商品名称:{product_title} 不包含8品规")
  3124. continue
  3125. elif self.search_key == '今维多赐多康牌蛋白粉':
  3126. temp_search_key = self.search_key.replace('今维多', '')
  3127. if '今维多' not in product_title or temp_search_key not in product_title :
  3128. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3129. continue
  3130. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  3131. temp_search_key = self.search_key.replace('佳美舒', '')
  3132. temp_search_key = temp_search_key.replace('4', '')
  3133. if '佳美舒' not in product_title or temp_search_key not in product_title :
  3134. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3135. continue
  3136. elif '4' not in product_title and '8' not in product_title:
  3137. print(f"当前商品名称:{product_title} 不包含4和或8品规")
  3138. continue
  3139. elif self.search_key == '三九胃泰颗粒20g*10':
  3140. temp_search_key = self.search_key.replace('20g*10', '')
  3141. if temp_search_key not in product_title :
  3142. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3143. continue
  3144. elif '20g*10' not in product_title:
  3145. print(f"当前商品名称:{product_title} 不包含20g*10品规")
  3146. continue
  3147. elif self.search_key == '三九胃泰颗粒20g*6袋':
  3148. temp_search_key = self.search_key.replace('20g*6袋', '')
  3149. if temp_search_key not in product_title :
  3150. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3151. continue
  3152. elif '20g*6' not in product_title:
  3153. print(f"当前商品名称:{product_title} 不包含20g*6品规")
  3154. continue
  3155. elif self.search_key == '顺峰康王酮康他索乳膏':
  3156. temp_search_key = self.search_key.replace('顺峰康王', '')
  3157. if '顺峰康王' not in product_title or temp_search_key not in product_title :
  3158. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3159. continue
  3160. else:
  3161. if self.search_key not in product_title.replace(' ', ''):
  3162. continue
  3163. elif self.d.xpath(product_tittle_xpath2).exists:
  3164. product_title = self.d.xpath(product_tittle_xpath2).text
  3165. product_title = product_title[1:] if product_title.startswith('0') else product_title
  3166. print(f"product_tittle_xpath2列表当前商品名称:{product_title}")
  3167. if '999' in self.search_key:
  3168. if self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  3169. temp_search_key = self.search_key.replace('999皮炎平', '')
  3170. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  3171. temp_search_key = self.search_key.replace('999必无忧', '')
  3172. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  3173. temp_search_key = self.search_key.replace('999必无忧', '')
  3174. elif self.search_key == '999速复康布洛芬缓释胶囊':
  3175. temp_search_key = self.search_key.replace('999速复康', '')
  3176. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  3177. temp_search_key = self.search_key.replace('999选平', '')
  3178. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  3179. temp_search_key = self.search_key.replace('999皮炎平', '')
  3180. else:
  3181. temp_search_key = self.search_key.replace('999', '')
  3182. if self.search_key == '999糠酸莫米松凝胶15':
  3183. temp_search_key = temp_search_key.replace('15', '')
  3184. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  3185. temp_search_key = temp_search_key.replace('30', '')
  3186. elif self.search_key == '999复方金银花颗粒10g':
  3187. temp_search_key = temp_search_key.replace('10g', '')
  3188. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  3189. temp_search_key = temp_search_key.replace('15g*15袋/盒', '')
  3190. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  3191. temp_search_key = temp_search_key.replace('6粒', '')
  3192. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  3193. temp_search_key = temp_search_key.replace('50', '')
  3194. elif self.search_key == '999止泻利颗粒15g*8':
  3195. temp_search_key = temp_search_key.replace('15g*8', '')
  3196. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  3197. temp_search_key = temp_search_key.replace('30', '')
  3198. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  3199. temp_search_key = temp_search_key.replace('15g', '')
  3200. elif self.search_key == '999复方苦参肠炎康片12片':
  3201. temp_search_key = temp_search_key.replace("12片", "")
  3202. elif self.search_key == '999强力枇杷露16袋':
  3203. temp_search_key = temp_search_key.replace("16袋", "")
  3204. elif self.search_key == '999三蛇胆川贝膏138':
  3205. temp_search_key = temp_search_key.replace("138", "")
  3206. elif self.search_key == '999抗病毒口服液10ml*12':
  3207. temp_search_key = temp_search_key.replace("10ml*12", "")
  3208. elif self.search_key == '999抗病毒口服液10ml*10':
  3209. temp_search_key = temp_search_key.replace("10ml*10", "")
  3210. elif self.search_key == '999强力枇杷露120ml':
  3211. temp_search_key = temp_search_key.replace("120ml", "")
  3212. elif self.search_key == '999强力枇杷露150ml':
  3213. temp_search_key = temp_search_key.replace("150ml", "")
  3214. elif self.search_key == '999糠酸莫米松乳膏10g支':
  3215. temp_search_key = temp_search_key.replace("10g支", "")
  3216. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  3217. temp_search_key = temp_search_key.replace("20g", "")
  3218. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  3219. temp_search_key = temp_search_key.replace("(无糖)6g", "")
  3220. elif self.search_key == '999壮骨关节丸6g*20':
  3221. temp_search_key = temp_search_key.replace("6g*20", "")
  3222. elif self.search_key == '999正天丸6g*15':
  3223. temp_search_key = temp_search_key.replace("6g*15", "")
  3224. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  3225. temp_search_key = temp_search_key.replace("20", "")
  3226. elif self.search_key == '999糠酸莫米松凝胶10':
  3227. temp_search_key = temp_search_key.replace("10", "")
  3228. elif self.search_key == '999板蓝根颗粒10g*20':
  3229. temp_search_key = temp_search_key.replace("10g*20", "")
  3230. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  3231. temp_search_key = temp_search_key.replace("10粒", "")
  3232. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  3233. temp_search_key = temp_search_key.replace("12粒", "")
  3234. elif self.search_key == '999咽炎片0.26g*12片*2板':
  3235. temp_search_key = temp_search_key.replace("0.26g*12片*2板", "")
  3236. elif self.search_key == '999小儿止咳糖浆120':
  3237. temp_search_key = temp_search_key.replace("120", "")
  3238. elif self.search_key == '999小儿止咳糖浆225':
  3239. temp_search_key = temp_search_key.replace("225", "")
  3240. elif self.search_key == '999小儿感冒颗粒6g*10':
  3241. temp_search_key = temp_search_key.replace("6g*10", "")
  3242. elif self.search_key == '999小儿感冒颗粒6g*24':
  3243. temp_search_key = temp_search_key.replace("6g*24", "")
  3244. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  3245. temp_search_key = temp_search_key.replace("6g*10袋", "")
  3246. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  3247. temp_search_key = temp_search_key.replace("6g*20袋", "")
  3248. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  3249. temp_search_key = temp_search_key.replace("8g*10袋", "")
  3250. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  3251. temp_search_key = temp_search_key.replace("2.5g*10袋", "")
  3252. elif self.search_key == '999感冒清热颗粒12g*18':
  3253. temp_search_key = temp_search_key.replace("12g*18", "")
  3254. elif self.search_key == '999小柴胡颗粒10g*15':
  3255. temp_search_key = temp_search_key.replace("10g*15", "")
  3256. if self.search_key == '999抗病毒口服液':
  3257. if '999' not in product_title or temp_search_key not in product_title :
  3258. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3259. continue
  3260. elif '10ml*12' not in product_title and '10ml*10' not in product_title:
  3261. print(f"当前商品名称:{product_title} 不包含10ml*12和10ml*10品规")
  3262. continue
  3263. elif self.search_key == '999抗病毒口服液10ml*12':
  3264. if '999' not in product_title or temp_search_key not in product_title :
  3265. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3266. continue
  3267. elif '10ml*12' not in product_title:
  3268. print(f"当前商品名称:{product_title} 不包含10ml*12品规")
  3269. continue
  3270. elif self.search_key == '999抗病毒口服液10ml*10':
  3271. if '999' not in product_title or temp_search_key not in product_title :
  3272. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3273. continue
  3274. elif '10ml*10' not in product_title:
  3275. print(f"当前商品名称:{product_title} 不包含10ml*10品规")
  3276. continue
  3277. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  3278. if '999' not in product_title or temp_search_key not in product_title :
  3279. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3280. continue
  3281. elif '30' not in product_title:
  3282. print(f"当前商品名称:{product_title} 不包含30品规")
  3283. continue
  3284. elif self.search_key == '999复方感冒灵颗粒':
  3285. if '999' not in product_title or temp_search_key not in product_title :
  3286. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3287. continue
  3288. # elif '14g*15' not in product_title and '14g*9' not in product_title:
  3289. elif '14g*15' not in product_title:
  3290. print(f"当前商品名称:{product_title} 不包含14g*15品规")
  3291. continue
  3292. elif self.search_key == '999养胃舒颗粒':
  3293. if '999' not in product_title or temp_search_key not in product_title :
  3294. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3295. continue
  3296. elif '10g*10' not in product_title:
  3297. print(f"当前商品名称:{product_title} 不包含10g*10品规")
  3298. continue
  3299. elif self.search_key == '999糠酸莫米松凝胶15':
  3300. if '999' not in product_title or temp_search_key not in product_title :
  3301. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3302. continue
  3303. elif '15' not in product_title:
  3304. print(f"当前商品名称:{product_title} 不包含15品规")
  3305. continue
  3306. elif self.search_key == '999复方金银花颗粒10g':
  3307. if '999' not in product_title or temp_search_key not in product_title :
  3308. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3309. continue
  3310. elif '10g' not in product_title:
  3311. print(f"当前商品名称:{product_title} 不包含10g品规")
  3312. continue
  3313. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  3314. if '999' not in product_title or temp_search_key not in product_title :
  3315. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3316. continue
  3317. elif '15g*15' not in product_title:
  3318. print(f"当前商品名称:{product_title} 不包含15g*15品规")
  3319. continue
  3320. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  3321. if '999' not in product_title or temp_search_key not in product_title :
  3322. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3323. continue
  3324. elif '6' not in product_title:
  3325. print(f"当前商品名称:{product_title} 不包含6品规")
  3326. continue
  3327. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  3328. if '999' not in product_title or temp_search_key not in product_title :
  3329. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3330. continue
  3331. elif '50' not in product_title:
  3332. print(f"当前商品名称:{product_title} 不包含50品规")
  3333. continue
  3334. elif self.search_key == '999止泻利颗粒15g*8':
  3335. if '999' not in product_title or temp_search_key not in product_title :
  3336. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3337. continue
  3338. elif '15g*8' not in product_title:
  3339. print(f"当前商品名称:{product_title} 不包含15g*8品规")
  3340. continue
  3341. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  3342. if '999' not in product_title or temp_search_key not in product_title :
  3343. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3344. continue
  3345. elif '30' not in product_title:
  3346. print(f"当前商品名称:{product_title} 不包含30品规")
  3347. continue
  3348. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  3349. if '999' not in product_title or temp_search_key not in product_title :
  3350. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3351. continue
  3352. elif '15' not in product_title:
  3353. print(f"当前商品名称:{product_title} 不包含15品规")
  3354. continue
  3355. elif self.search_key == '999复方苦参肠炎康片12片':
  3356. if '999' not in product_title or temp_search_key not in product_title :
  3357. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3358. continue
  3359. elif '12' not in product_title:
  3360. print(f"当前商品名称:{product_title} 不包含12品规")
  3361. continue
  3362. elif self.search_key == '999强力枇杷露16袋':
  3363. if '999' not in product_title or temp_search_key not in product_title :
  3364. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3365. continue
  3366. elif '16' not in product_title:
  3367. print(f"当前商品名称:{product_title} 不包含16品规")
  3368. continue
  3369. elif self.search_key == '999三蛇胆川贝膏138':
  3370. if '999' not in product_title or temp_search_key not in product_title :
  3371. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3372. continue
  3373. elif '138' not in product_title:
  3374. print(f"当前商品名称:{product_title} 不包含138品规")
  3375. elif self.search_key == '999速复康布洛芬缓释胶囊':
  3376. if '999' not in product_title or temp_search_key not in product_title :
  3377. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3378. continue
  3379. elif self.search_key == '999维生素C咀嚼片':
  3380. if '999' not in product_title or temp_search_key not in product_title :
  3381. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3382. continue
  3383. elif '80' not in product_title:
  3384. print(f"当前商品名称:{product_title} 不包含80品规")
  3385. continue
  3386. elif self.search_key == '999精氨酸布洛芬颗粒':
  3387. if '999' not in product_title or temp_search_key not in product_title :
  3388. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3389. continue
  3390. elif '9' not in product_title:
  3391. print(f"当前商品名称:{product_title} 不包含9品规")
  3392. continue
  3393. elif self.search_key == '999强力枇杷露120ml':
  3394. if '999' not in product_title or temp_search_key not in product_title :
  3395. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3396. continue
  3397. elif '120' not in product_title:
  3398. print(f"当前商品名称:{product_title} 不包含120品规")
  3399. continue
  3400. elif self.search_key == '999强力枇杷露150ml':
  3401. if '999' not in product_title or temp_search_key not in product_title :
  3402. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3403. continue
  3404. elif '150' not in product_title:
  3405. print(f"当前商品名称:{product_title} 不包含150品规")
  3406. continue
  3407. elif self.search_key == '999糠酸莫米松乳膏10g支':
  3408. if '999' not in product_title or temp_search_key not in product_title :
  3409. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3410. continue
  3411. elif '10' not in product_title:
  3412. print(f"当前商品名称:{product_title} 不包含10品规")
  3413. continue
  3414. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  3415. if '999' not in product_title or temp_search_key not in product_title :
  3416. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3417. continue
  3418. elif '20' not in product_title:
  3419. print(f"当前商品名称:{product_title} 不包含20品规")
  3420. continue
  3421. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  3422. if '999' not in product_title or temp_search_key not in product_title :
  3423. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3424. continue
  3425. elif '6' not in product_title:
  3426. print(f"当前商品名称:{product_title} 不包含6品规")
  3427. continue
  3428. elif self.search_key == '999阿奇霉素片':
  3429. if '999' not in product_title or temp_search_key not in product_title :
  3430. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3431. continue
  3432. elif '0.25g*6' not in product_title:
  3433. print(f"当前商品名称:{product_title} 不包含0.25g*6品规")
  3434. continue
  3435. elif self.search_key == '999补脾益肠丸':
  3436. if '999' not in product_title or temp_search_key not in product_title :
  3437. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3438. continue
  3439. elif '6g*15' not in product_title:
  3440. print(f"当前商品名称:{product_title} 不包含6g*15品规")
  3441. continue
  3442. elif self.search_key == '999壮骨关节丸6g*20':
  3443. if '999' not in product_title or temp_search_key not in product_title :
  3444. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3445. continue
  3446. elif '6g*20' not in product_title:
  3447. print(f"当前商品名称:{product_title} 不包含6g*20品规")
  3448. continue
  3449. elif self.search_key == '999正天丸6g*15':
  3450. if '999' not in product_title or temp_search_key not in product_title :
  3451. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3452. continue
  3453. elif '6g*15' not in product_title:
  3454. print(f"当前商品名称:{product_title} 不包含6g*15品规")
  3455. continue
  3456. elif self.search_key == '999感冒灵颗粒':
  3457. if '999' not in product_title or temp_search_key not in product_title :
  3458. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3459. continue
  3460. elif '10g*9' not in product_title:
  3461. print(f"当前商品名称:{product_title} 不包含10g*9品规")
  3462. continue
  3463. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  3464. if '999' not in product_title or temp_search_key not in product_title :
  3465. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3466. continue
  3467. elif '20' not in product_title:
  3468. print(f"当前商品名称:{product_title} 不包含20品规")
  3469. continue
  3470. elif self.search_key == '999糠酸莫米松凝胶10':
  3471. if '999' not in product_title or temp_search_key not in product_title :
  3472. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3473. continue
  3474. elif '10' not in product_title:
  3475. print(f"当前商品名称:{product_title} 不包含10品规")
  3476. continue
  3477. elif self.search_key == '999板蓝根颗粒10g*20':
  3478. if '999' not in product_title or temp_search_key not in product_title :
  3479. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3480. continue
  3481. elif '10g*20' not in product_title:
  3482. print(f"当前商品名称:{product_title} 不包含10g*20品规")
  3483. continue
  3484. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  3485. if '999' not in product_title or temp_search_key not in product_title :
  3486. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3487. continue
  3488. elif '10' not in product_title:
  3489. print(f"当前商品名称:{product_title} 不包含10品规")
  3490. continue
  3491. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  3492. if '999' not in product_title or temp_search_key not in product_title :
  3493. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3494. continue
  3495. elif '12' not in product_title:
  3496. print(f"当前商品名称:{product_title} 不包含12品规")
  3497. continue
  3498. elif self.search_key == '999复方氨酚烷胺胶囊':
  3499. if '999' not in product_title or temp_search_key not in product_title :
  3500. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3501. continue
  3502. elif self.search_key == '999咽炎片0.26g*12片*2板':
  3503. if '999' not in product_title or temp_search_key not in product_title :
  3504. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3505. continue
  3506. elif '0.26g*12' not in product_title:
  3507. print(f"当前商品名称:{product_title} 不包含0.26g*12品规")
  3508. continue
  3509. elif self.search_key == '999小儿止咳糖浆120':
  3510. if '999' not in product_title or temp_search_key not in product_title :
  3511. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3512. continue
  3513. elif '120' not in product_title:
  3514. print(f"当前商品名称:{product_title} 不包含120品规")
  3515. continue
  3516. elif self.search_key == '999小儿止咳糖浆225':
  3517. if '999' not in product_title or temp_search_key not in product_title :
  3518. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3519. continue
  3520. elif '225' not in product_title:
  3521. print(f"当前商品名称:{product_title} 不包含225品规")
  3522. continue
  3523. elif self.search_key == '999小儿感冒颗粒6g*10':
  3524. if '999' not in product_title or temp_search_key not in product_title :
  3525. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3526. continue
  3527. elif '6g*10' not in product_title:
  3528. print(f"当前商品名称:{product_title} 不包含6g*10品规")
  3529. continue
  3530. elif self.search_key == '999小儿感冒颗粒6g*24':
  3531. if '999' not in product_title or temp_search_key not in product_title :
  3532. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3533. continue
  3534. elif '6g*24' not in product_title:
  3535. print(f"当前商品名称:{product_title} 不包含6g*24品规")
  3536. continue
  3537. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  3538. if '999' not in product_title or temp_search_key not in product_title :
  3539. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3540. continue
  3541. elif '6g*10' not in product_title:
  3542. print(f"当前商品名称:{product_title} 不包含6g*10品规")
  3543. continue
  3544. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  3545. if '999' not in product_title or temp_search_key not in product_title :
  3546. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3547. continue
  3548. elif '6g*20' not in product_title:
  3549. print(f"当前商品名称:{product_title} 不包含6g*20品规")
  3550. continue
  3551. elif self.search_key == '999感冒灵胶囊':
  3552. if '999' not in product_title or temp_search_key not in product_title :
  3553. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3554. continue
  3555. elif '0.5g*12' not in product_title:
  3556. print(f"当前商品名称:{product_title} 不包含0.5g*12品规")
  3557. continue
  3558. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  3559. if '999' not in product_title or temp_search_key not in product_title :
  3560. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3561. continue
  3562. elif '8g*10' not in product_title:
  3563. print(f"当前商品名称:{product_title} 不包含8g*10品规")
  3564. continue
  3565. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  3566. if '999' not in product_title or temp_search_key not in product_title :
  3567. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3568. continue
  3569. elif '2.5g*10' not in product_title:
  3570. print(f"当前商品名称:{product_title} 不包含2.5g*10品规")
  3571. continue
  3572. elif self.search_key == '999感冒清热颗粒12g*18':
  3573. if '999' not in product_title or temp_search_key not in product_title :
  3574. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3575. continue
  3576. elif '12g*18' not in product_title:
  3577. print(f"当前商品名称:{product_title} 不包含12g*18品规")
  3578. continue
  3579. elif self.search_key == '999小柴胡颗粒10g*15':
  3580. if '999' not in product_title or temp_search_key not in product_title :
  3581. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3582. continue
  3583. elif '10g*15' not in product_title:
  3584. print(f"当前商品名称:{product_title} 不包含10g*15品规")
  3585. continue
  3586. elif self.search_key == '999藿香正气合剂':
  3587. if '999' not in product_title or temp_search_key not in product_title :
  3588. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3589. continue
  3590. elif '10ml*6' not in product_title and '10ml*10' not in product_title:
  3591. print(f"当前商品名称:{product_title} 不包含10ml*6 和 10ml*10品规")
  3592. continue
  3593. else:
  3594. if '999' not in product_title or temp_search_key not in product_title:
  3595. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3596. continue
  3597. # if '999' not in product_title or temp_search_key not in product_title:
  3598. # print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3599. # continue
  3600. else:
  3601. if self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  3602. temp_search_key = self.search_key.replace('史达功', '')
  3603. temp_search_key = temp_search_key.replace('120', '')
  3604. if '史达功' not in product_title or temp_search_key not in product_title :
  3605. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3606. continue
  3607. elif '120' not in product_title:
  3608. print(f"当前商品名称:{product_title} 不包含120品规")
  3609. continue
  3610. elif self.search_key == '三九胃泰养胃舒颗粒8袋':
  3611. temp_search_key = self.search_key.replace('三九胃泰', '')
  3612. temp_search_key = temp_search_key.replace('8袋', '')
  3613. if '三九胃泰' not in product_title or temp_search_key not in product_title :
  3614. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3615. continue
  3616. elif '8' not in product_title:
  3617. print(f"当前商品名称:{product_title} 不包含8品规")
  3618. continue
  3619. elif self.search_key == '今维多赐多康牌蛋白粉':
  3620. temp_search_key = self.search_key.replace('今维多', '')
  3621. if '今维多' not in product_title or temp_search_key not in product_title :
  3622. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3623. continue
  3624. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  3625. temp_search_key = self.search_key.replace('佳美舒', '')
  3626. temp_search_key = temp_search_key.replace('4', '')
  3627. if '佳美舒' not in product_title or temp_search_key not in product_title :
  3628. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3629. continue
  3630. elif '4' not in product_title and '8' not in product_title:
  3631. print(f"当前商品名称:{product_title} 不包含4和或8品规")
  3632. continue
  3633. elif self.search_key == '三九胃泰颗粒20g*10':
  3634. temp_search_key = self.search_key.replace('20g*10', '')
  3635. if temp_search_key not in product_title :
  3636. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3637. continue
  3638. elif '20g*10' not in product_title:
  3639. print(f"当前商品名称:{product_title} 不包含20g*10品规")
  3640. continue
  3641. elif self.search_key == '三九胃泰颗粒20g*6袋':
  3642. temp_search_key = self.search_key.replace('20g*6袋', '')
  3643. if temp_search_key not in product_title :
  3644. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3645. continue
  3646. elif '20g*6' not in product_title:
  3647. print(f"当前商品名称:{product_title} 不包含20g*6品规")
  3648. continue
  3649. elif self.search_key == '顺峰康王酮康他索乳膏':
  3650. temp_search_key = self.search_key.replace('顺峰康王', '')
  3651. if '顺峰康王' not in product_title or temp_search_key not in product_title :
  3652. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3653. continue
  3654. else:
  3655. if self.search_key not in product_title.replace(' ', ''):
  3656. continue
  3657. else:
  3658. print(f"列表当前商品名称不存在")
  3659. #价格
  3660. price_xpath = f'//android.support.v7.widget.RecyclerView/android.widget.FrameLayout[{idxx}]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView'
  3661. price_xpath3 = f'//android.support.v7.widget.RecyclerView/android.widget.FrameLayout[{idxx}]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView'
  3662. price_xpath1 = f'//android.support.v7.widget.RecyclerView/android.widget.FrameLayout[{idxx}]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView'
  3663. if self.d.xpath(price_xpath).exists:
  3664. price_str = self.d.xpath(price_xpath).text
  3665. print(f"price_xpath列表当前商品价格:{price_str}")
  3666. if price_str:
  3667. price = float(re.search(r'[\d\.]+', price_str).group())
  3668. elif self.d.xpath(price_xpath3).exists:
  3669. price_str = self.d.xpath(price_xpath3).text
  3670. print(f"price_xpath3列表当前商品价格:{price_str}")
  3671. if price_str:
  3672. price = float(re.search(r'[\d\.]+', price_str).group())
  3673. elif self.d.xpath(price_xpath1).exists:
  3674. price_str = self.d.xpath(price_xpath1).text
  3675. print(f"price_xpath1列表当前商品价格:{price_str}")
  3676. if price_str:
  3677. price = float(re.search(r'[\d\.]+', price_str).group())
  3678. else:
  3679. price_xpath2 = f'//android.support.v7.widget.RecyclerView/android.widget.FrameLayout[{idxx}]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.TextView'
  3680. if self.d.xpath(price_xpath2).exists:
  3681. price_str = self.d.xpath(price_xpath2).text
  3682. print(f"price_xpath2列表当前商品价格:{price_str}")
  3683. if price_str:
  3684. price = float(re.search(r'[\d\.]+', price_str).group())
  3685. else:
  3686. print(f"列表当前商品价格不存在")
  3687. # price_str = self.d.xpath(f'//android.support.v7.widget.RecyclerView/android.widget.FrameLayout[{idxx}]//*[starts-with(@text,"¥")]').text
  3688. print(f'列表获取到价格:{price}')
  3689. #店铺名称的xpath
  3690. shop_name_xpath = f'//android.support.v7.widget.RecyclerView/android.widget.FrameLayout[{idxx}]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.FrameLayout[last()]/android.widget.TextView[1]'
  3691. shop_name_xpath2 = f'//android.support.v7.widget.RecyclerView/android.widget.FrameLayout[{idxx}]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.widget.FrameLayout[last()]/android.widget.TextView[1]'
  3692. if self.d.xpath(shop_name_xpath).exists:
  3693. shop_name = self.d.xpath(shop_name_xpath).text
  3694. print(f"shop_name_xpath列表当前商品店铺名称:{shop_name}")
  3695. elif self.d.xpath(shop_name_xpath2).exists:
  3696. shop_name = self.d.xpath(shop_name_xpath2).text
  3697. print(f"shop_name_xpath2列表当前商品店铺名称:{shop_name}")
  3698. else:
  3699. print(f"列表当前商品店铺名称不存在")
  3700. #如果商品的名称、价格和生产厂家都不存在则直接下一条数据。 跳过一些不是商品的数据。
  3701. if product_title == '' and price == '' and shop_name == '':
  3702. continue
  3703. scrape_date = self.get_current_date()
  3704. if product_title and price and shop_name:
  3705. #判断数据表中是否存在
  3706. dup_data = {'product': product_title, 'min_price': price, 'shop': shop_name, 'scrape_date': scrape_date,'platform': '美团'}
  3707. if self.data_is_exists(dup_data):
  3708. print('列表存在相同数据不入库')
  3709. continue
  3710. self.safe_exec(drug_one.click)
  3711. print('点击目标药品完毕')
  3712. time.sleep(2)
  3713. # 采集药品信息
  3714. try:
  3715. # self.integrate_data()
  3716. self.safe_exec(self.integrate_data)
  3717. # 检测下是否回退到列表页
  3718. if self.distinct_target():
  3719. print('回退到列表页', True)
  3720. else:
  3721. if self.d.xpath('//*[@text="搜索"]').exists:
  3722. print("检测到搜索按钮,重新开始采集流程")
  3723. if retry_count < MAX_RETRY:
  3724. # 停止当前监控线程
  3725. self.monitor.stop()
  3726. self.monitor.join()
  3727. # 递归重启采集
  3728. return self.main(device_id, retry_count+1)
  3729. else:
  3730. print("超过最大重试次数,终止程序")
  3731. return
  3732. else:
  3733. print("无法恢复页面,终止采集")
  3734. return
  3735. # print('回退到列表页失败,终止采集')
  3736. # return
  3737. time.sleep(self.get_sleep_time())
  3738. spider_no += 1
  3739. except Exception as e:
  3740. print(f'采集药品详情数据出错:{e}')
  3741. #增加阻塞的方法:
  3742. if not self.distinct_target():
  3743. for i in range(1):
  3744. self.swipe_back(1)
  3745. # 最外部有个定位按钮
  3746. if self.distinct_target():
  3747. break
  3748. if i == 0 and not self.distinct_target():
  3749. print('页面出错,退出采集')
  3750. return
  3751. else:
  3752. continue
  3753. if self.d.xpath('//*[@text="已经到底啦"]').exists:
  3754. print('已经到达列表页最底部')
  3755. return
  3756. search_list = self.d.xpath('//android.support.v7.widget.RecyclerView').info
  3757. bounds = search_list['bounds']
  3758. #print('搜索列表高度', 1400 + bounds['top'] - bounds['bottom'])
  3759. # self.d.swipe(200, 1400, 200, 1400 + bounds['top'] - bounds['bottom'])
  3760. # 计算滑动距离
  3761. scroll_distance = bounds['bottom'] - bounds['top'] # 正数
  3762. start_y = 1600
  3763. end_y = start_y - scroll_distance # 向上滑动,y 坐标减小
  3764. # 确保 end_y 不小于 0
  3765. end_y = max(end_y, 304) # 留出一点边距,避免滑出屏幕
  3766. # print('滑动起点 y:', start_y, '终点 y:', end_y)
  3767. # self.d.swipe(200, start_y, 200, end_y, 0.4)
  3768. print('开始滑动')
  3769. self.d.drag(300, 1400, 300, 400, 1)
  3770. # self.safe_exec(self.d.drag, 300, 1400, 300, 400, 1)
  3771. print('滑动结束')
  3772. #print('搜索列表高度', 1400 + bounds['top'] - bounds['bottom'])
  3773. # self.d.swipe(200, 1400, 200, 1400 + bounds['top'] - bounds['bottom'])
  3774. # self.d.swipe(200, 1400, 200, 1400 + bounds['top'] - bounds['bottom'], 0.4)
  3775. time.sleep(self.get_sleep_time())
  3776. finally:
  3777. # 确保监控线程被停止
  3778. self.monitor.stop()
  3779. self.monitor.join()
  3780. def unitest(self):
  3781. # # 创建目录
  3782. # SCREENSHOT_DIR = Path('screenshot') # 注意这里的变化
  3783. # SCREENSHOT_DIR.mkdir(parents=True, exist_ok=True)
  3784. # # 创建测试图片路径
  3785. # qualification_number = '12345'
  3786. # file_path = SCREENSHOT_DIR / f'{qualification_number}.png'
  3787. # # 创建图片
  3788. # img = Image.new('RGB', (300, 200), color='lightblue')
  3789. # draw = ImageDraw.Draw(img)
  3790. # # 添加文字
  3791. # try:
  3792. # font = ImageFont.truetype("arial.ttf", 20)
  3793. # except:
  3794. # font = ImageFont.load_default()
  3795. # draw.text((50, 80), f"测试图片: {qualification_number}", fill='black', font=font)
  3796. # draw.text((50, 110), "保存路径测试", fill='darkred', font=font)
  3797. # draw.text((50, 140), f"目录: {SCREENSHOT_DIR}", fill='green', font=font)
  3798. # # 保存图片
  3799. # img.save(file_path)
  3800. # print(f"✅ 图片已保存到: {file_path.resolve()}")
  3801. # print(f"📁 文件大小: {file_path.stat().st_size} 字节")
  3802. # print(f"📏 图片尺寸: {img.size}")
  3803. # SCREENSHOT_DIR = Path('drug_data/mt/screenshot')
  3804. # # 使用
  3805. # qualification_number = '12345'
  3806. # file_path = SCREENSHOT_DIR / f'{qualification_number}.png'
  3807. # print(f"file_path={file_path}")
  3808. # print("=== 路径信息 ===")
  3809. # print(f"当前工作目录: {Path.cwd()}")
  3810. # print(f"SCREENSHOT_DIR: {SCREENSHOT_DIR}")
  3811. # print(f"file_path: {file_path}")
  3812. # print(f"绝对路径: {file_path.resolve()}")
  3813. # print(f"父目录: {file_path.parent}")
  3814. # print(f"文件名: {file_path.name}")
  3815. # print(f"是否存在: {file_path.exists()}")
  3816. time.sleep(100000)
  3817. """
  3818. 单元测试
  3819. :return:
  3820. """
  3821. save_data = {
  3822. 'product':"[昆中药]舒肝颗粒(低糖型)",
  3823. 'min_price': 14.0,
  3824. 'manufacture_date': '',
  3825. 'expiry_date': '36个月',
  3826. 'shop': '美团自营大药房(快递电商)',
  3827. 'business_license_company': '',
  3828. 'province': '',
  3829. 'city': '',
  3830. 'manufacturer': '昆明中药厂有限公司',
  3831. 'specification': '3g*16袋/盒',
  3832. 'approval_number': '国药准字Z53021161',
  3833. 'product_link': '',
  3834. 'scrape_date': '2025/07/09',
  3835. 'scrape_province': '广东',
  3836. 'availability': '',
  3837. 'credit_code': '',
  3838. 'platform': '美团',
  3839. 'search_key':'',
  3840. 'sales': '',
  3841. 'inventory':'',
  3842. 'snapshot_url':''
  3843. }
  3844. self.save_to_database(save_data)
  3845. time.sleep(100000)
  3846. pass
  3847. def main():
  3848. keys_list = [
  3849. # '三九胃泰颗粒',
  3850. # '999小柴胡颗粒',
  3851. # '999强力枇杷露',
  3852. # '[999]感冒清热颗粒',
  3853. # '999抗病毒口服液',
  3854. # '999皮炎平',
  3855. # '999盐酸特比萘芬乳膏',
  3856. # '999盐酸特比萘芬',
  3857. # '999藿香正气合剂',
  3858. # '999必无忧盐酸特比萘芬乳膏',
  3859. # '999复方感冒灵颗粒',
  3860. # '999糠酸莫米松凝胶',
  3861. # '999铝碳酸镁咀嚼片',
  3862. # '999阿奇霉素片',
  3863. # '999选平硝酸咪康唑乳膏',
  3864. # 按需继续添加,
  3865. #暂时不需要
  3866. # '999必无忧盐酸特比萘芬喷雾剂30'
  3867. # '999冰连清咽'
  3868. # '999复方金银花颗粒10g'
  3869. # '999复方苦参肠炎康片12片'
  3870. # '999强力枇杷露16袋'
  3871. # '999三蛇胆川贝膏138'
  3872. # '999维生素ec颗粒'
  3873. # '三九胃泰养胃舒颗粒8袋'
  3874. # '999止泻利颗粒15g*8'
  3875. # '999阿奇霉素片'
  3876. # '999可调式生理性海水鼻腔喷雾50'
  3877. # '999小儿止咳糖浆120' #不低于19.8
  3878. # '999小儿止咳糖浆225' #禁止挂网
  3879. # '999小儿感冒颗粒6g*10' #不低于24.9
  3880. # '999小儿感冒颗粒6g*24' #禁止挂网
  3881. # '999小儿氨酚黄那敏颗粒6g*10袋' #不低于15.8
  3882. # '999小儿氨酚黄那敏颗粒6g*20袋' #禁止挂网
  3883. # '999小儿咽扁颗粒8g*10袋' #仅限999官旗店
  3884. # '佳美舒阿奇霉素肠溶胶囊4'
  3885. # '999维生素C咀嚼片'
  3886. # '999黄芪精',
  3887. # '999皮炎平曲安奈德益康唑乳膏30',
  3888. # '999葡萄糖酸锌口服溶液',
  3889. # '今维多赐多康牌蛋白粉',
  3890. #OTC
  3891. # '999强力枇杷露150ml'
  3892. # '999糠酸莫米松乳膏10g支'
  3893. # '999选平硝酸咪康唑乳膏20g'
  3894. # '999感冒清热颗粒(无糖)6g'
  3895. # '999银菊清咽颗粒' #只有一条数据
  3896. # '999补脾益肠丸'
  3897. # '999壮骨关节丸6g*20'
  3898. # '999壮骨关节胶囊'
  3899. # '999正天丸6g*15'
  3900. # '999正天胶囊'
  3901. # '三九胃泰胶囊'
  3902. # '三九胃泰颗粒20g*10'
  3903. # '三九胃泰颗粒(无糖)2.5g*6' # 没有数据
  3904. # '999复方氨酚烷胺胶囊12粒' #不低于17.9
  3905. # '999复方氨酚烷胺胶囊10粒' #禁止挂网
  3906. # '999复方氨酚烷胺胶囊6粒' #禁止挂网
  3907. # '顺峰康王酮康他索乳膏' #包含10g和20g两个规格 10g 不低于7.5 20g 不低于12.5
  3908. # '999咽炎片0.26g*12片*2板' #不低于13.5
  3909. # '999板蓝根颗粒10g*20' #不低于26.9
  3910. # '999小儿感冒宁颗粒2.5g*10袋' #禁止挂网
  3911. # '999抗病毒口服液'
  3912. # '999蒲地蓝消炎片',
  3913. # '999小柴胡颗粒10g*15',
  3914. # '999必无忧盐酸特比萘芬乳膏15g'
  3915. # '999复方板蓝根颗粒15g*15袋/盒'
  3916. # '999速复康布洛芬缓释胶囊'
  3917. # '999精氨酸布洛芬颗粒'
  3918. # '999强力枇杷露120ml'
  3919. # '999小儿感冒颗粒6g*10' #不低于24.9
  3920. # '999感冒灵颗粒' #不低于15.5
  3921. # '999皮炎平复方醋酸地塞米松乳膏20' #不低于12.5
  3922. # '三九胃泰颗粒20g*6袋' #不低于13.5
  3923. # '999复方氨酚烷胺胶囊'
  3924. # '999感冒灵胶囊' #仅限999官旗店
  3925. # '999荆防颗粒' #美团没有数据 #禁止挂网
  3926. # '史达功右美沙芬愈创甘油醚糖浆120' #仅限999官旗店
  3927. # '999磷酸奥司他韦胶囊75mg*10' #仅限999官旗店
  3928. #2025-08-01最新 其中 藿香正气合剂两种规格 10支和6支 抗病毒口服液 12支和18支 蒲地蓝 24片 36片和44片 枇杷露225ml 小柴胡颗粒9袋和15袋 养胃舒 6袋 复方感冒灵颗粒15袋,
  3929. #曲安奈德益康唑乳膏 30g 葡萄糖酸锌口服溶液 12支 18支 24支和30支,
  3930. # 1、999止泻利颗粒15g*8 没有数据 2、999维生素ec颗粒 没有数据 3、999三蛇胆川贝膏138 没有数据 4、999强力枇杷露16袋 没有数据 5、999冰连清咽 没有数据
  3931. #############以下是日常采集的品规start##################
  3932. # '999养胃舒颗粒',
  3933. # '999复方感冒灵颗粒',
  3934. # '999感冒清热颗粒12g*18', #禁止挂网
  3935. # '999小儿感冒宁颗粒2.5g*10袋', #禁止挂网 999小儿感冒宁颗粒2.5g*10袋 改成 999小儿感冒宁颗粒
  3936. # '999小儿咽扁颗粒8g*10袋', #仅限999官旗店
  3937. # '999布洛芬混悬液',
  3938. # '999板蓝根颗粒10g*20',
  3939. #########2026春节采集的品规start################################
  3940. # '999藿香正气合剂',
  3941. # '999糠酸莫米松凝胶15',
  3942. # '999抗病毒口服液10ml*10',
  3943. # '999抗病毒口服液10ml*12',
  3944. # '999强力枇杷露225ml',
  3945. '999糠酸莫米松凝胶10' #不低于26.9
  3946. #########2026春节采集的品规end################################
  3947. ############以下是日常采集的品规start###########################
  3948. ]
  3949. #美团手机号:
  3950. #美团手机号:
  3951. # device_id = '21885f5' # 设备序列号
  3952. # device_id = '2e58510' # 设备序列号
  3953. # device_id = 'KNNNEMNVWCJZQOLZ'
  3954. # device_id = 'B6JVE6AYSWU4LRLZ'
  3955. # device_id = '656DTOPRZDEALZ5X'
  3956. # device_id = 'GIOFIBRKZTUGJJAE'
  3957. # device_id = 'fcb3c749'
  3958. # device_id = 'UCQGF6CQFMU8WKHI'
  3959. # device_id = '4TZDUGTOAIFMJVGU'
  3960. device_id = '95b2c764'
  3961. # device_id = 'ZDQWUSSWBEDI896T'
  3962. # device_id = 'R4SKMJPNBQAENRAM'
  3963. # device_id = '1462a51f'
  3964. # device_id = '97ae80e0' # 设备序列号
  3965. # device_id = 'IZTOWWDQT45D49BU'
  3966. # device_id = 'N7ZXBITOSOGMYXQS'
  3967. # device_id = '369dcf96'
  3968. # device_id = 'GQIRKB7LVOONM7VW'
  3969. # device_id = 'T4UCEQGQEEYP65ZL'
  3970. # device_id = '49L7GMPRVS85LJHE'
  3971. # device_id = 'WWRO9LTGG6KFGQCM'
  3972. # device_id = 'DYF67TM7KJ4POJLF'
  3973. # device_id = '21885f5'
  3974. # device_id = 'U8ONIJJJS4CELVD6'
  3975. # device_id = 'LJ9PN7A6K7HQ9DPF'
  3976. cycle_no = 0 # 轮次计数
  3977. while True:
  3978. cycle_no += 1
  3979. logging.info(f'========== 第 {cycle_no} 轮采集开始 ==========')
  3980. for idx, key in enumerate(keys_list, 1):
  3981. logging.info(f'[{idx}/{len(keys_list)}] 开始采集关键字:{key}')
  3982. try:
  3983. mt = MT(key) # 用当前关键字实例化
  3984. # mt.unitest()
  3985. mt.main(device_id) # 执行一次完整采集
  3986. logging.info(f'关键字 {key} 本轮采集完成')
  3987. except Exception as e:
  3988. # 发生异常直接跳过该关键字,继续下一轮
  3989. logging.exception(f'关键字 {key} 采集异常:{e}')
  3990. finally:
  3991. # 关闭当前 MT 实例资源(如有需要)
  3992. if hasattr(mt, 'close'):
  3993. time.sleep(10000)
  3994. mt.close()
  3995. # logging.info('本轮全部关键字采集完成,等待 2 小时后下一轮...')
  3996. # time.sleep(1 * 3600) # 2 小时 = 7200 秒
  3997. # keys = '小柴胡颗粒' # 参苓健脾胃颗粒 舒肝颗粒 清肺化痰丸 香砂平胃颗粒 小柴胡颗粒
  3998. # mt = MT(keys) # 参苓健脾胃颗粒 舒肝颗粒 清肺化痰丸 香砂平胃颗粒
  3999. # # mt.main('95b2c764')
  4000. # mt.main('fcb3c749')
  4001. if __name__ == '__main__':
  4002. main()
  4003. # scheduler = BlockingScheduler()
  4004. # scheduler.add_job(main, 'cron', hour=21, minute=30, misfire_grace_time=120)
  4005. # try:
  4006. # scheduler.start()
  4007. # except (KeyboardInterrupt, SystemExit):
  4008. # pass