mt_auto_scrape_bak20260126.py 299 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462
  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 pyperclip
  20. from config import Config
  21. from logger import setup_logger
  22. import logging
  23. from contextlib import contextmanager
  24. # from database import MySQLClient
  25. # 配置日志
  26. # logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  27. setup_logger("mt_spider") # 初始化日志
  28. class SpiderMonitor(threading.Thread):
  29. """全局弹窗监控线程(增强版)"""
  30. def __init__(self, spider_instance):
  31. super().__init__(daemon=True)
  32. self.spider = spider_instance
  33. self.running = True
  34. self.pausing = threading.Event() # 主线程同步事件
  35. self.last_verification_time = 0
  36. self.verification_count = 0
  37. self.MAX_VERIFICATION_RETRY = 10
  38. self.recent_clicks = deque(maxlen=10) # 防重复点击
  39. self.logger = logging.getLogger("SpiderMonitor")
  40. self.TOKEN = "1nDVocTE2mJ0yLEYb2sZJ5uUY2VIEoGTkIpW44X7Kgk"
  41. self.API_URL = "http://api.jfbym.com/api/YmServer/customApi"
  42. self.d = self.spider.d
  43. self.verification_in_progress = threading.Event()
  44. self.loggerMT = logging.getLogger()
  45. self.verification_retry_count = 0 # 当前验证码重试次数
  46. self.last_verification_type = None
  47. # 可配置化弹窗规则
  48. self.popup_rules = {
  49. "simple": [
  50. ('//*[@text="确定"]', "点击确定"),
  51. ('//*[@text="允许"]', "点击允许"),
  52. ('//*[@text="关闭"]', "点击关闭"),
  53. ('//*[@resource-id="com.sankuai.meituan:id/close"]', "关闭按钮"),
  54. ('//*[@resource-id="com.sankuai.meituan:id/address_center_location_close"]', "关闭按钮"),
  55. ('//*[@resource-id="com.sankuai.meituan:id/location_close"]', "关闭按钮"),
  56. ],
  57. # "verification": [
  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(@text, "请向右滑动滑块")]',
  67. # '//*[contains(@text, "请拖动下方滑块完成拼图")]',
  68. # '//*[contains(@resource-id, "captcha")]'
  69. # ]
  70. "verification": [
  71. ('//*[contains(@text, "请点击")]', "click_side"),
  72. ('//*[contains(@text, "请输入图片中的内容")]', "Numbers_English"),
  73. ('//*[contains(@text, "请向右滑动滑块")]', "Swipe_right"),
  74. ('//*[contains(@text, "请依次点击下图图标")]', "Click_images"),
  75. ('//*[contains(@text, "请拖动下方滑块完成拼图")]', "slider"),
  76. ('//*[contains(@text, "拖动滑块刚")]', "complexs"), # 这个需要拖动滑块至最右边,然后再截图
  77. ('//*[contains(@text, "请按语序依次点击")]', "Click_images"),
  78. ('//*[contains(@text, "用最短线连接")]', "Shortest_connection"),
  79. ]
  80. }
  81. def run(self):
  82. while self.running:
  83. try:
  84. handled = self.check_and_handle_popup()
  85. time.sleep(2 if handled else 1)
  86. except Exception as e:
  87. self.logger.exception("监控线程异常: %s", e)
  88. time.sleep(3)
  89. def _is_recent_click(self, xpath):
  90. """防止重复点击同一个弹窗"""
  91. key = f"{xpath}_{int(time.time())}"
  92. if key in self.recent_clicks:
  93. return True
  94. self.recent_clicks.append(key)
  95. return False
  96. @staticmethod
  97. def get_sleep_time():
  98. # return random.randint(5, 8)
  99. return random.randint(1, 3)
  100. def human_slide(self, start_x, start_y, end_x, end_y, hold_time=0):
  101. """模拟真实人类滑动轨迹 - 连续变化的速度曲线,微小偏差"""
  102. points = []
  103. # 随机参数
  104. total_steps = random.randint(60, 85) # 更多步数使曲线更平滑
  105. # 计算滑动距离
  106. distance_x = end_x - start_x
  107. distance_y = end_y - start_y
  108. total_distance = math.sqrt(distance_x ** 2 + distance_y ** 2)
  109. # 微小偏差设置 - 人类不完美的对齐
  110. # X方向偏差:1-6像素,70%概率过冲,30%欠冲
  111. if random.random() < 0.7:
  112. offset_x = random.randint(1, min(5, int(total_distance * 0.01)))
  113. else:
  114. offset_x = -random.randint(1, min(3, int(total_distance * 0.008)))
  115. # # Y方向微小偏差:±0-2像素
  116. # offset_y = random.randint(-2, 2)
  117. # 实际停止位置
  118. stop_x = end_x + offset_x
  119. stop_y = end_y
  120. # 物理参数:模拟手指滑动的物理过程
  121. # 使用加速度、最大速度、减速度模型
  122. accel_time_ratio = random.uniform(0.25, 0.35) # 加速阶段占总时间的比例
  123. decel_time_ratio = random.uniform(0.25, 0.35) # 减速阶段占总时间的比例
  124. max_speed = random.uniform(1.5, 2.2) # 最大速度倍数
  125. # 生成轨迹
  126. for i in range(total_steps):
  127. t = i / (total_steps - 1) # 时间进度 0-1
  128. # 物理速度曲线:连续变化的加速度过程
  129. if t < accel_time_ratio:
  130. # 加速阶段:从0加速到最大速度
  131. phase_t = t / accel_time_ratio
  132. # 使用平滑的加速曲线(二次函数)
  133. speed_factor = max_speed * phase_t * phase_t
  134. elif t < 1 - decel_time_ratio:
  135. # 匀速阶段:保持最大速度
  136. speed_factor = max_speed
  137. # 加入轻微的随机波动,模拟人类手部自然抖动
  138. speed_factor += random.uniform(-0.05, 0.05)
  139. else:
  140. # 减速阶段:从最大速度减速到0
  141. phase_t = (t - (1 - decel_time_ratio)) / decel_time_ratio
  142. # 使用平滑的减速曲线(二次函数,末尾更平缓)
  143. speed_factor = max_speed * (1 - phase_t * phase_t)
  144. # 计算位移(积分速度得到位置)
  145. # 使用贝塞尔曲线计算位置,让运动更自然
  146. if t < accel_time_ratio:
  147. # 加速阶段的位置
  148. phase_t = t / accel_time_ratio
  149. progress = (max_speed / 3) * phase_t * phase_t * phase_t
  150. elif t < 1 - decel_time_ratio:
  151. # 匀速阶段的位置
  152. phase_t = (t - accel_time_ratio) / (1 - accel_time_ratio - decel_time_ratio)
  153. # 匀速阶段的位移加上加速阶段完成的位移
  154. accel_distance = (max_speed / 3) # 加速阶段完成的位移
  155. progress = accel_distance + (1 - 2 * accel_distance) * phase_t
  156. else:
  157. # 减速阶段的位置
  158. phase_t = (t - (1 - decel_time_ratio)) / decel_time_ratio
  159. # 从减速起点平滑过渡到终点
  160. progress = 1 - (max_speed / 3) * (1 - phase_t) * (1 - phase_t) * (1 - phase_t)
  161. # 限制进度在0-1之间
  162. progress = max(0, min(1, progress))
  163. # 添加自然的手部抖动
  164. if t < 0.1 or t > 0.9:
  165. # 开始和结束:非常小的抖动
  166. jitter_x = random.randint(-1, 1)
  167. jitter_y = random.randint(-1, 1)
  168. elif t < 0.3 or t > 0.7:
  169. # 过渡阶段:小抖动
  170. jitter_x = random.randint(-2, 2)
  171. jitter_y = random.randint(-2, 2)
  172. else:
  173. # 中间快速阶段:稍大抖动
  174. jitter_x = random.randint(-2, 2) if random.random() < 0.3 else 0
  175. jitter_y = random.randint(-2, 2) if random.random() < 0.3 else 0
  176. # 计算当前位置
  177. current_x = start_x + (stop_x - start_x) * progress + jitter_x
  178. current_y = start_y + (stop_y - start_y) * progress + jitter_y
  179. # 确保轨迹单调性(不会回退)
  180. if points:
  181. if distance_x > 0: # 向右滑动
  182. current_x = max(points[-1][0], current_x)
  183. elif distance_x < 0: # 向左滑动
  184. current_x = min(points[-1][0], current_x)
  185. points.append((int(current_x), int(current_y)))
  186. # 时间延迟 - 基于当前速度计算
  187. # 速度越快,延迟越短
  188. if t < 0.1: # 开始阶段
  189. delay = random.uniform(0.012, 0.020)
  190. elif t < 0.9: # 中间阶段
  191. # 延迟与速度成反比
  192. base_delay = 0.008
  193. speed_delay_factor = 1.0 / (speed_factor + 0.5)
  194. delay = base_delay * speed_delay_factor + random.uniform(-0.002, 0.002)
  195. delay = max(0.005, min(delay, 0.015))
  196. else: # 结束阶段
  197. # 逐渐增加延迟
  198. slow_factor = 1.0 + (t - 0.9) * 10
  199. delay = random.uniform(0.015, 0.025) * slow_factor
  200. time.sleep(delay)
  201. # 确保最后一点是实际停止位置
  202. if points:
  203. points[-1] = (stop_x, stop_y)
  204. # 执行滑动
  205. if points:
  206. # 按下起点
  207. self.d.touch.down(points[0][0], points[0][1])
  208. time.sleep(random.uniform(0.02, 0.06))
  209. # 移动轨迹
  210. for i, point in enumerate(points[1:]):
  211. self.d.touch.move(point[0], point[1])
  212. # 最后阶段可能的微小停顿(人类犹豫)
  213. progress = (i + 1) / len(points[1:])
  214. if progress > 0.98 and random.random() < 0.08:
  215. time.sleep(random.uniform(0.01, 0.03))
  216. # 抬起手指
  217. self.d.touch.up(points[-1][0], points[-1][1])
  218. # 滑动后的随机延迟
  219. hold_time = random.uniform(1, 2)
  220. time.sleep(hold_time)
  221. return points
  222. # 数英
  223. def Numbers_English_verify(self):
  224. time.sleep(5)
  225. rotate_image_xpath = '//*[@text="身份核实"]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.widget.Image[1]'
  226. if not self.d.xpath(rotate_image_xpath).exists:
  227. print("数英图片元素不存在")
  228. rotate_img_path = "Numbers_English.png"
  229. try:
  230. rotate_image = self.d.xpath(rotate_image_xpath)
  231. rotate_image.screenshot().save(rotate_img_path)
  232. print(f"数英图片截图保存成功: {rotate_img_path}")
  233. except Exception as e:
  234. print(f"数英图片截图失败: {e}")
  235. try:
  236. with open(rotate_img_path, 'rb') as f:
  237. image_data = base64.b64encode(f.read()).decode()
  238. url = "http://api.jfbym.com/api/YmServer/customApi"
  239. data = {
  240. "token": "1nDVocTE2mJ0yLEYb2sZJ5uUY2VIEoGTkIpW44X7Kgk",
  241. "type": 10103,
  242. "image": image_data
  243. }
  244. headers = {
  245. "Content-Type": "application/json"
  246. }
  247. response = requests.post(url, headers=headers, json=data, timeout=30)
  248. response.raise_for_status()
  249. result = response.json()
  250. if result.get("code") == 10000 and result.get("data", {}).get("code") == 0:
  251. Numbers_English_verify_data = result["data"]
  252. print(f"API返回: {Numbers_English_verify_data}")
  253. captcha_text = Numbers_English_verify_data.get("data")
  254. print(f"验证码: {captcha_text}")
  255. if self.d.xpath(
  256. '//*[@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.view.View[1]/android.widget.EditText[1]| //*[@resource-id="com.sankuai.meituan:id/titans_webview_container"]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.widget.EditText[1]').exists:
  257. self.d.xpath(
  258. '//*[@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.view.View[1]/android.widget.EditText[1]| //*[@resource-id="com.sankuai.meituan:id/titans_webview_container"]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.widget.EditText[1]').click()
  259. time.sleep(1)
  260. self.d.send_keys(captcha_text)
  261. time.sleep(5)
  262. self.d.xpath(
  263. '//*[@text="验证"] | //*[@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.view.View[2]/android.widget.Button[1]').click()
  264. time.sleep(3)
  265. return True
  266. else:
  267. print("API返回错误")
  268. return False
  269. except Exception as e:
  270. print(f"数英验证码处理异常: {e}")
  271. return False
  272. # 滑块
  273. def slider_verify(self):
  274. time.sleep(5)
  275. try:
  276. slider_slot_xpath = '//*[@resource-id="puzzleSliderDrag"] | //*[@text="身份核实"]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.widget.TextView[1]'
  277. slider_main_xpath = '//*[@resource-id="puzzleImageMain"] | //*[@text="身份核实"]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]'
  278. slider_slot_img_path = "slider_slot.png"
  279. slider_main_img_path = "slider_main.png"
  280. if self.d.xpath(slider_slot_xpath).exists:
  281. self.d.xpath(slider_slot_xpath).screenshot().save("slider_slot.png")
  282. else:
  283. print("slider_slot_xpath not exist")
  284. self.logger.info(f"slider_slot_xpath not exist")
  285. if self.d.xpath(slider_main_xpath).exists:
  286. self.d.xpath(slider_main_xpath).screenshot().save("slider_main.png")
  287. else:
  288. print("slider_main_xpath not exist")
  289. self.logger.info(f"slider_main_xpath not exist")
  290. slider_slide_distance = 0
  291. try:
  292. with open('slider_main.png', 'rb') as f:
  293. b = base64.b64encode(f.read()).decode()
  294. # API请求配置
  295. url = "http://api.jfbym.com/api/YmServer/customApi"
  296. data = {
  297. "token": "1nDVocTE2mJ0yLEYb2sZJ5uUY2VIEoGTkIpW44X7Kgk",
  298. "type": 22222,
  299. "image": b
  300. }
  301. headers = {
  302. "Content-Type": "application/json"
  303. }
  304. response = requests.post(url, headers=headers, json=data, timeout=30)
  305. response.raise_for_status()
  306. result = response.json()
  307. if result.get("code") == 10000 and result.get("data", {}).get("code") == 0:
  308. slider_verify_data = result["data"]
  309. print(f"slider_verify_data={slider_verify_data}")
  310. slider_slide_distance = slider_verify_data["data"]
  311. print(slider_slide_distance)
  312. else:
  313. print("api 返回错误 此时滑块验证可能呈图片形式存在")
  314. except Exception as e:
  315. return {
  316. "success": False,
  317. "error_msg": f"处理异常: {str(e)}"
  318. }
  319. slider_slide_distance = slider_verify_data["data"]
  320. # 获取滑块元素
  321. try:
  322. slider_xpath = '//*[@resource-id="puzzleSliderBox"] | //*[@text="身份核实"]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.view.View[1]'
  323. slider = self.d.xpath(slider_xpath)
  324. slider_info = slider.info
  325. bounds = slider_info['bounds']
  326. start_x = (bounds['left'] + bounds['right']) // 2
  327. start_y = (bounds['top'] + bounds['bottom']) // 2
  328. end_x = start_x + int(slider_slide_distance) # ●
  329. end_y = start_y
  330. self.human_slide(start_x, start_y, end_x, end_y)
  331. time.sleep(2) #
  332. return True
  333. except Exception as e:
  334. print(f"滑动操作时出错: {e}")
  335. return False
  336. time.sleep(2)
  337. # 检查验证码是否消失
  338. slider_xpaths = [
  339. '//*[@text="请拖动下方滑块完成拼图"]',
  340. ]
  341. for xpath in slider_xpaths:
  342. if self.d.xpath(xpath).exists:
  343. return False
  344. return True
  345. except Exception as e:
  346. self.logger.error(f"滑块验证失败: {e}")
  347. return False
  348. # 点击
  349. def click_side(self):
  350. try:
  351. image_xpath = '//*[@resource-id="com.sankuai.meituan:id/titans_main_layout"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]'
  352. image_element = self.d.xpath(image_xpath)
  353. image_element_info = image_element.info
  354. bounds = image_element_info['bounds']
  355. image_left = bounds['left']
  356. image_top = bounds['top']
  357. image_width = bounds['right'] - bounds['left']
  358. image_height = bounds['bottom'] - bounds['top']
  359. print(f"图片位置: left={image_left}, top={image_top}, width={image_width}, height={image_height}")
  360. #
  361. image_img_path = "image.png"
  362. if self.d.xpath(image_xpath).exists:
  363. self.d.xpath(image_xpath).screenshot().save("image.png")
  364. else:
  365. print("image_xpath not exist")
  366. try:
  367. with open('image.png', 'rb') as f:
  368. c = base64.b64encode(f.read()).decode()
  369. url = "http://api.jfbym.com/api/YmServer/customApi"
  370. data = {
  371. "token": "1nDVocTE2mJ0yLEYb2sZJ5uUY2VIEoGTkIpW44X7Kgk",
  372. "type": 50009,
  373. "image": c
  374. }
  375. headers = {
  376. "Content-Type": "application/json"
  377. }
  378. # 发送请求
  379. response = requests.post(url, headers=headers, json=data, timeout=30)
  380. response.raise_for_status()
  381. result = response.json()
  382. if result.get("code") == 10000 and result.get("data", {}).get("code") == 0:
  383. side_data = result["data"]
  384. print(f"verify_data={side_data}")
  385. big_click_xpath = side_data["data"]
  386. else:
  387. print("api 返回错误")
  388. return False
  389. except Exception as e:
  390. return {
  391. "success": False,
  392. "error_msg": f"处理异常: {str(e)}"
  393. }
  394. coords_str = side_data["data"]
  395. if coords_str:
  396. x_img_str, y_img_str = coords_str.split(',')
  397. x_img = int(x_img_str.strip())
  398. y_img = int(y_img_str.strip())
  399. x_screen = image_left + x_img
  400. y_screen = image_top + y_img
  401. self.d.click(x_screen, y_screen)
  402. time.sleep(self.get_sleep_time())
  403. return True
  404. except Exception as e:
  405. self.logger.error(f"空间推理验证失败: {e}")
  406. return False
  407. # 向右滑动
  408. def Swipe_right(self):
  409. time.sleep(5)
  410. start_x = 0
  411. start_y = 0
  412. end_x = 0
  413. end_y = 0
  414. distance = 0
  415. if self.d.xpath(
  416. '//*[@resource-id="yodaBoxWrapper"] | //*[@text="身份核实"]/android.view.View[1]/android.view.View[1]/android.view.View[1]').exists:
  417. Swipe = self.d.xpath(
  418. '//*[@resource-id="yodaBoxWrapper"] | //*[@text="身份核实"]/android.view.View[1]/android.view.View[1]/android.view.View[1]')
  419. Swipe_info = Swipe.info
  420. bound = Swipe_info['bounds']
  421. Swipe_distance = bound['right'] - bound['left']
  422. if self.d.xpath(
  423. '//*[@resource-id="yodaBox"] | //*[@text="身份核实"]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1] ').exists:
  424. xpath = self.d.xpath(
  425. '//*[@resource-id="yodaBox"] | //*[@text="身份核实"]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]')
  426. xpath_info = xpath.info
  427. bounds = xpath_info['bounds']
  428. start_x = (bounds['left'] + bounds['right']) // 2
  429. start_y = (bounds['top'] + bounds['bottom']) // 2
  430. distance = Swipe_distance - (bounds['right'] - bounds['left'])
  431. end_x = start_x + distance
  432. end_y = start_y
  433. print(f"滑动距离: {distance}像素")
  434. print(f"起点: ({start_x}, {start_y}), 终点: ({end_x}, {end_y})")
  435. # 确保滑块到达最右端
  436. end_x += 10
  437. self.Swipe_right_human_slide(start_x, start_y, end_x, end_y)
  438. time.sleep(2)
  439. return True
  440. else:
  441. print("未找到滑块元素")
  442. return False
  443. else:
  444. print("未找到轨道元素")
  445. return False
  446. def Swipe_right_human_slide(self, start_x, start_y, end_x, end_y):
  447. """模拟人类滑动轨迹"""
  448. # 生成带加速度的轨迹
  449. points = []
  450. total_steps = 50
  451. distance_x = end_x - start_x
  452. distance_y = end_y - start_y
  453. previous_x = start_x # 用于记录上一个 x 坐标值
  454. for i in range(total_steps):
  455. # 非线性进度(慢-快-慢)
  456. ratio = (i / total_steps)
  457. if ratio < 0.3:
  458. progress = 0.5 * (ratio / 0.3) ** 2
  459. elif ratio < 0.7:
  460. progress = 0.5 + (ratio - 0.3) * 1.25
  461. else:
  462. progress = 0.9 + 0.5 * ((ratio - 0.7) / 0.3) ** 0.5
  463. # 添加随机抖动
  464. # offset_x = np.random.randint(-2, 3)
  465. # offset_y = np.random.randint(-2, 3)
  466. offset_x = np.random.randint(-1, 1) # 控制抖动范围
  467. offset_y = np.random.randint(-1, 1)
  468. x = start_x + distance_x * min(progress, 0.99) + offset_x
  469. y = start_y + distance_y * min(progress, 0.99) + offset_y
  470. # 确保 x 坐标单调递增
  471. if x < previous_x and x < end_x:
  472. x = previous_x + 1
  473. if x > end_x:
  474. x = end_x
  475. previous_x = x
  476. points.append((x, y))
  477. # 变速延迟(移动越快延迟越短)
  478. delay = 0.002 + 0.01 * (1 - abs(0.5 - ratio))
  479. time.sleep(delay)
  480. print(f"points: {points}")
  481. self.loggerMT.info(f"points: {points}")
  482. # 执行滑动轨迹
  483. self.d.touch.down(points[0][0], points[0][1])
  484. for point in points[1:]:
  485. self.d.touch.move(point[0], point[1])
  486. self.d.touch.up(points[-1][0] + 2, points[-1][1])
  487. # print(f"points: {points}")
  488. # self.loggerPdd.info(f"points: {points}")
  489. # self.d.swipe_points(points, duration=0.05)
  490. # 拖动滑块刚
  491. def complexs(self):
  492. time.sleep(5)
  493. try:
  494. slider_xpath = '//*[@text="身份核实"]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.view.View[1]'
  495. track_xpath = '//*[@text="请按照说明拖动滑块"]'
  496. if not self.d.xpath(slider_xpath).exists:
  497. print("滑块元素不存在")
  498. self.logger.info("滑块元素不存在")
  499. return False
  500. if not self.d.xpath(track_xpath).exists:
  501. print("滑轨元素不存在")
  502. self.logger.info("滑轨元素不存在")
  503. return False
  504. #
  505. slider_element = self.d.xpath(slider_xpath)
  506. slider_info = slider_element.info
  507. slider_bounds = slider_info['bounds']
  508. slider_left = slider_bounds['left']
  509. slider_top = slider_bounds['top']
  510. slider_right = slider_bounds['right']
  511. slider_bottom = slider_bounds['bottom']
  512. slider_width = slider_right - slider_left
  513. slider_height = slider_bottom - slider_top
  514. slider_center_x = slider_left + slider_width / 2
  515. slider_center_y = slider_top + slider_height / 2
  516. print(f"滑块中心: ({slider_center_x}, {slider_center_y})")
  517. #
  518. track_element = self.d.xpath(track_xpath)
  519. track_info = track_element.info
  520. track_bounds = track_info['bounds']
  521. track_left = track_bounds['left']
  522. track_right = track_bounds['right']
  523. track_width = track_right - track_left
  524. # 2. 滑到滑轨最右端
  525. right_end_center_x = track_right - slider_width / 2
  526. right_end_center_y = slider_center_y
  527. print(f"最右端滑块中心坐标: ({right_end_center_x}, {right_end_center_y})")
  528. try:
  529. self.d.touch.down(slider_center_x, slider_center_y)
  530. time.sleep(0.1)
  531. # 生成滑动轨迹
  532. points = self.Swipe_trajectory(slider_center_x, slider_center_y, right_end_center_x,
  533. right_end_center_y)
  534. for point in points[1:]:
  535. self.d.touch.move(point[0], point[1])
  536. time.sleep(0.002)
  537. print("滑块已到达最右端")
  538. except Exception as e:
  539. print(f"滑动到最右端失败: {e}")
  540. return
  541. capture_xpath1 = '// *[ @ text = "身份核实"] / android.view.View[1] / android.view.View[1] / android.widget.TextView[1]'
  542. capture_xpath2 = '// * [ @ text = "身份核实"] / android.view.View[1] / android.view.View[1] / android.view.View[1]'
  543. capture_element1 = self.d.xpath(capture_xpath1)
  544. capture_element2 = self.d.xpath(capture_xpath2)
  545. capture_info1 = capture_element1.info
  546. capture_info2 = capture_element2.info
  547. capture_info1_bounds = capture_info1['bounds']
  548. capture_info2_bounds = capture_info2['bounds']
  549. capture_left = capture_info1_bounds['left']
  550. capture_top = capture_info1_bounds['top']
  551. capture_right = capture_info2_bounds['right']
  552. capture_bottom = capture_info2_bounds['bottom']
  553. capture_width = capture_right - capture_left
  554. capture_height = capture_bottom - capture_top
  555. print(f"截图区域: left={capture_left}, top={capture_top}, width={capture_width}, height={capture_height}")
  556. # 截图并保存
  557. screenshot_path = "capture_area.png"
  558. try:
  559. full_screenshot = self.d.screenshot()
  560. from PIL import Image
  561. import io
  562. if isinstance(full_screenshot, bytes):
  563. img = Image.open(io.BytesIO(full_screenshot))
  564. else:
  565. img = full_screenshot
  566. # 裁剪指定区域
  567. cropped_img = img.crop((capture_left, capture_top, capture_right, capture_bottom))
  568. cropped_img.save(screenshot_path)
  569. print(f"截图已保存: {screenshot_path}")
  570. except Exception as e:
  571. print(f"截图失败: {e}")
  572. self.d.touch.up(right_end_center_x, right_end_center_y)
  573. return
  574. try:
  575. with open(screenshot_path, 'rb') as f:
  576. image_data = base64.b64encode(f.read()).decode()
  577. # API请求配置
  578. url = "http://api.jfbym.com/api/YmServer/customApi"
  579. data = {
  580. "token": "1nDVocTE2mJ0yLEYb2sZJ5uUY2VIEoGTkIpW44X7Kgk",
  581. "type": 20226,
  582. "image": image_data
  583. }
  584. headers = {
  585. "Content-Type": "application/json"
  586. }
  587. # 发送请求
  588. response = requests.post(url, headers=headers, json=data, timeout=30)
  589. response.raise_for_status()
  590. result = response.json()
  591. print(f"API返回结果: {result}")
  592. if result.get("code") == 10000 and result.get("data", {}).get("code") == 0:
  593. verify_data = result.get("data", {})
  594. print(f"verify_data={verify_data}")
  595. data_str = verify_data.get("data", "")
  596. if not data_str:
  597. print("云码未返回有效的data值")
  598. # 抬起并返回
  599. self.d.touch.up(right_end_center_x, right_end_center_y)
  600. return
  601. try:
  602. data_value = int(data_str)
  603. print(f"云码返回的像素距离: {data_value}")
  604. gray_line_target_x = 108 + data_value - 44
  605. slider_target_center_x = gray_line_target_x
  606. print(f"滑块中心目标X坐标: {slider_target_center_x}")
  607. # 5. 计算需要往回拖动的距离
  608. # 当前滑块在最右端,中心X = 612
  609. current_slider_center_x = 612
  610. back_distance = 504 - data_value - 44
  611. print(f"需要往回拖动的距离: {back_distance}")
  612. # 执行往回拖动
  613. slider_element = self.d.xpath(slider_xpath)
  614. slider_info = slider_element.info
  615. slider_bounds = slider_info['bounds']
  616. current_slider_center_x = (slider_bounds['left'] + slider_bounds['right']) / 2
  617. # 确保目标位置在滑轨范围内
  618. min_x = track_left + slider_width / 2
  619. max_x = track_right - slider_width / 2
  620. slider_target_center_x = max(min_x, min(slider_target_center_x, max_x))
  621. # 计算实际需要滑动的距离
  622. actual_distance = slider_target_center_x - current_slider_center_x
  623. print(f"实际需要滑动的距离: {actual_distance}")
  624. # 往回拖动
  625. try:
  626. # 往回拖动的轨迹
  627. back_points = self.Swipe_trajectory(
  628. right_end_center_x, right_end_center_y,
  629. slider_target_center_x, right_end_center_y
  630. )
  631. # 移动到每个点
  632. for point in back_points[1:]:
  633. self.d.touch.move(point[0], point[1])
  634. time.sleep(0.002)
  635. time.sleep(3)
  636. self.d.touch.up(slider_target_center_x, right_end_center_y)
  637. except Exception as e:
  638. print(f"往回拖动失败: {e}")
  639. self.d.touch.up(right_end_center_x, right_end_center_y)
  640. except ValueError as e:
  641. print(f"解析云码返回的data值失败: {e}")
  642. return False
  643. except Exception as e:
  644. print(f"计算滑块位置失败: {e}")
  645. return False
  646. else:
  647. error_msg = result.get("msg", "未知错误")
  648. print(f"识别失败: {error_msg}")
  649. return False
  650. except requests.exceptions.RequestException as e:
  651. print(f"API请求失败: {e}")
  652. return False
  653. except Exception as e:
  654. print(f"识别过程出错: {e}")
  655. return False
  656. except Exception as e:
  657. self.logger.error(f"滑块验证失败: {e}")
  658. return False
  659. def Swipe_trajectory(self, start_x, start_y, end_x, end_y):
  660. """生成滑动轨迹点"""
  661. points = []
  662. total_steps = 50
  663. distance_x = end_x - start_x
  664. distance_y = end_y - start_y
  665. previous_x = start_x
  666. for i in range(total_steps):
  667. # 非线性进度(慢-快-慢)
  668. ratio = (i / total_steps)
  669. if ratio < 0.3:
  670. progress = 0.5 * (ratio / 0.3) ** 2
  671. elif ratio < 0.7:
  672. progress = 0.5 + (ratio - 0.3) * 1.25
  673. else:
  674. progress = 0.9 + 0.5 * ((ratio - 0.7) / 0.3) ** 0.5
  675. # 添加随机抖动
  676. offset_x = np.random.randint(-1, 1)
  677. offset_y = np.random.randint(-1, 1)
  678. x = start_x + distance_x * min(progress, 0.99) + offset_x
  679. y = start_y + distance_y * min(progress, 0.99) + offset_y
  680. # 确保 x 坐标单调递增
  681. if x < previous_x and x < end_x:
  682. x = previous_x + 1
  683. if x > end_x:
  684. x = end_x
  685. previous_x = x
  686. points.append((x, y))
  687. return points
  688. # 最短线连接
  689. def Shortest_connection(self):
  690. time.sleep(5)
  691. try:
  692. art_text_xpath = '//*[@text="身份核实"]/android.view.View[1]/android.view.View[1]/android.view.View[1]'
  693. color_points_xpath = '//*[@text="身份核实"]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.view.View[1]/android.widget.Image[1]'
  694. art_text_img_path = "art_text.png"
  695. color_points_img_path = "color_points.png"
  696. if self.d.xpath(art_text_xpath).exists:
  697. self.d.xpath(art_text_xpath).screenshot().save(art_text_img_path)
  698. self.logger.info("艺术字截图成功")
  699. else:
  700. self.logger.warning("艺术字元素不存在")
  701. return False
  702. if self.d.xpath(color_points_xpath).exists:
  703. self.d.xpath(color_points_xpath).screenshot().save(color_points_img_path)
  704. self.logger.info("颜色元素截图成功")
  705. else:
  706. self.logger.warning("颜色点元素不存在")
  707. return False
  708. color_name = ""
  709. try:
  710. with open('art_text.png', 'rb') as f:
  711. c = base64.b64encode(f.read()).decode()
  712. url = "http://api.jfbym.com/api/YmServer/customApi"
  713. data = {
  714. "token": "1nDVocTE2mJ0yLEYb2sZJ5uUY2VIEoGTkIpW44X7Kgk",
  715. "type": 10118,
  716. "image": c
  717. }
  718. headers = {
  719. "Content-Type": "application/json"
  720. }
  721. response = requests.post(url, headers=headers, json=data, timeout=30)
  722. response.raise_for_status()
  723. result = response.json()
  724. self.logger.info(f"云码API返回结果: {result}")
  725. if result.get("code") == 0:
  726. color_name = result.get("data", "")
  727. if "鼗" in color_name:
  728. color_name == "紫色"
  729. elif result.get("code") == 10000 and "data" in result:
  730. inner_data = result.get("data", {})
  731. if isinstance(inner_data, dict) and inner_data.get("code") == 0:
  732. color_name = inner_data.get("data", "")
  733. if "鼗" in color_name:
  734. color_name == "紫色"
  735. elif isinstance(inner_data, str):
  736. color_name = inner_data
  737. if "鼗" in color_name:
  738. color_name == "紫色"
  739. else:
  740. self.logger.error(f"云码API返回异常: {result}")
  741. return False
  742. if not color_name:
  743. self.logger.error("未识别到颜色名称")
  744. return False
  745. self.logger.info(f"识别到的颜色名称: {color_name}")
  746. except Exception as e:
  747. self.logger.error(f"云码API调用异常: {e}")
  748. return False
  749. # 颜色的坐标
  750. relative_coordinates = self.find_color_coordinates(color_points_img_path, color_name)
  751. if not relative_coordinates:
  752. self.logger.warning(f"未在图片中找到 {color_name} 颜色的坐标")
  753. return False
  754. color_element = self.d.xpath(color_points_xpath).info
  755. element_bounds = color_element['bounds']
  756. element_left = element_bounds['left']
  757. element_top = element_bounds['top']
  758. element_width = element_bounds['right'] - element_bounds['left']
  759. element_height = element_bounds['bottom'] - element_bounds['top']
  760. # 读取截图
  761. try:
  762. color_points_img = cv2.imread(color_points_img_path)
  763. if color_points_img is None:
  764. self.logger.error("无法读取截图")
  765. return False
  766. screenshot_height, screenshot_width = color_points_img.shape[:2]
  767. except Exception as e:
  768. self.logger.error(f"读取截图尺寸失败: {e}")
  769. # 如果无法读取,使用元素尺寸作为默认值
  770. screenshot_width = element_width
  771. screenshot_height = element_height
  772. # 屏幕坐标
  773. screen_coordinates = []
  774. for (rx, ry) in relative_coordinates:
  775. if screenshot_width > 0 and screenshot_height > 0:
  776. # 计算缩放比例
  777. scale_x = element_width / screenshot_width
  778. scale_y = element_height / screenshot_height
  779. sx = element_left + int(rx * scale_x)
  780. sy = element_top + int(ry * scale_y)
  781. else:
  782. # 如果无法获取截图尺寸,直接使用相对坐标加上元素起始位置
  783. sx = element_left + rx
  784. sy = element_top + ry
  785. screen_coordinates.append((sx, sy))
  786. self.logger.info(f"相对坐标({rx}, {ry}) -> 屏幕坐标({sx}, {sy})")
  787. # . 计算最短路径
  788. if len(screen_coordinates) < 2:
  789. self.logger.warning("需要至少2个点才能连接")
  790. return False
  791. unvisited = screen_coordinates.copy()
  792. path = [unvisited.pop(0)]
  793. while unvisited:
  794. last_point = path[-1]
  795. nearest_idx = min(range(len(unvisited)),
  796. key=lambda i: ((last_point[0] - unvisited[i][0]) ** 2 +
  797. (last_point[1] - unvisited[i][1]) ** 2) ** 0.5)
  798. path.append(unvisited.pop(nearest_idx))
  799. self.logger.info(f"最短路径点顺序: {path}")
  800. curved_path = self.human_like_path(path)
  801. # 连接
  802. result = self.simulate_human_drawing(curved_path)
  803. if result:
  804. self.logger.info("最短线连接成功")
  805. time.sleep(3)
  806. return True
  807. else:
  808. self.logger.warning("最短线连接失败")
  809. return False
  810. except Exception as e:
  811. self.logger.error(f"最短线连接验证失败: {e}")
  812. return False
  813. def find_color_coordinates(self, image_path, color_name):
  814. """查找指定颜色"""
  815. self.logger.info(f"开始查找颜色: {color_name}, 图片路径: {image_path}")
  816. # 颜色HSV范围映射
  817. color_ranges = {
  818. "红色": (([0, 120, 70], [10, 255, 255]), ([170, 120, 70], [180, 255, 255])),
  819. "红的": (([0, 120, 70], [10, 255, 255]), ([170, 120, 70], [180, 255, 255])),
  820. "绿色": (([35, 50, 50], [85, 255, 255]),),
  821. "蓝色": (([90, 50, 50], [130, 255, 255]),),
  822. "黄色": (([20, 100, 100], [30, 255, 255]),),
  823. "橙色": (([5, 100, 100], [15, 255, 255]),),
  824. "紫色": (([130, 50, 50], [160, 255, 255]),),
  825. "黑色": (([0, 0, 0], [180, 255, 50]),),
  826. "白色": (([0, 0, 200], [180, 30, 255]),),
  827. "黑色": (([0, 0, 0], [180, 255, 50]),),
  828. "褐色": (([10, 100, 20], [20, 255, 200]),),
  829. "橘色": (([5, 150, 150], [15, 255, 255])),
  830. "褐色": (([10, 50, 20], [20, 255, 150])),
  831. }
  832. if color_name not in color_ranges:
  833. self.logger.warning(f"不支持的颜色: {color_name}")
  834. return []
  835. # 读取图像
  836. image = cv2.imread(image_path)
  837. if image is None:
  838. self.logger.error(f"无法读取图像: {image_path}")
  839. return []
  840. # 转换到HSV颜色空间
  841. hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  842. # 根据颜色名称获取HSV范围
  843. color_range = color_ranges[color_name]
  844. # 创建颜色掩码
  845. if color_name == "红色":
  846. lower1 = np.array(color_range[0][0])
  847. upper1 = np.array(color_range[0][1])
  848. lower2 = np.array(color_range[1][0])
  849. upper2 = np.array(color_range[1][1])
  850. mask1 = cv2.inRange(hsv, lower1, upper1)
  851. mask2 = cv2.inRange(hsv, lower2, upper2)
  852. mask = cv2.bitwise_or(mask1, mask2)
  853. else:
  854. lower = np.array(color_range[0][0])
  855. upper = np.array(color_range[0][1])
  856. mask = cv2.inRange(hsv, lower, upper)
  857. # 形态学操作去除噪点
  858. kernel = np.ones((3, 3), np.uint8)
  859. mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
  860. mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
  861. # 查找轮廓
  862. contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  863. # 获取每个轮廓的中心点(相对坐标)
  864. coordinates = []
  865. min_area = 30 # 最小面积阈值
  866. for i, contour in enumerate(contours):
  867. area = cv2.contourArea(contour)
  868. if area > min_area:
  869. # 计算轮廓的中心点
  870. M = cv2.moments(contour)
  871. if M["m00"] != 0:
  872. cx = int(M["m10"] / M["m00"])
  873. cy = int(M["m01"] / M["m00"])
  874. coordinates.append((cx, cy))
  875. self.logger.info(f"轮廓{i}: 面积={area}, 中心点=({cx}, {cy})")
  876. else:
  877. self.logger.info(f"轮廓{i}: 面积={area}, 无法计算中心点")
  878. self.logger.info(f"找到 {len(coordinates)} 个 {color_name} 坐标点")
  879. return coordinates
  880. def human_like_path(self, points):
  881. """生成模拟人类的弯曲路径"""
  882. if len(points) < 2:
  883. return points
  884. curved_path = []
  885. for i in range(len(points) - 1):
  886. start = points[i]
  887. end = points[i + 1]
  888. # 在两点之间添加弯曲点
  889. mid_x = (start[0] + end[0]) / 2
  890. mid_y = (start[1] + end[1]) / 2
  891. # 计算随机偏移,模拟人类手绘误差
  892. if abs(end[0] - start[0]) > abs(end[1] - start[1]):
  893. # 水平方向为主,在垂直方向添加偏移
  894. offset_x = 0
  895. offset_y = random.uniform(-15, 15)
  896. else:
  897. # 垂直方向为主,在水平方向添加偏移
  898. offset_x = random.uniform(-15, 15)
  899. offset_y = 0
  900. # 控制点(在中间点添加偏移)
  901. control_x = mid_x + offset_x
  902. control_y = mid_y + offset_y
  903. # 使用二次贝塞尔曲线生成弯曲路径
  904. curved_path.append(start)
  905. for t in np.arange(0.1, 1.0, 0.1):
  906. # 二次贝塞尔曲线公式
  907. x = (1 - t) ** 2 * start[0] + 2 * (1 - t) * t * control_x + t ** 2 * end[0]
  908. y = (1 - t) ** 2 * start[1] + 2 * (1 - t) * t * control_y + t ** 2 * end[1]
  909. curved_path.append((int(x), int(y)))
  910. # 添加最后一个点
  911. curved_path.append(points[-1])
  912. return curved_path
  913. def simulate_human_drawing(self, path):
  914. """模拟人类绘制路径"""
  915. if len(path) < 2:
  916. return False
  917. try:
  918. # 获取第一个点
  919. start_x, start_y = path[0]
  920. self.d.touch.down(start_x, start_y)
  921. time.sleep(random.uniform(0.05, 0.1))
  922. # 依次移动到路径中的每个点
  923. for i in range(1, len(path)):
  924. target_x, target_y = path[i]
  925. # 添加随机抖动,模拟人类手部颤抖
  926. jitter_x = random.randint(-2, 2)
  927. jitter_y = random.randint(-2, 2)
  928. self.d.touch.move(target_x + jitter_x, target_y + jitter_y)
  929. # 添加随机延迟
  930. delay = random.uniform(0.01, 0.03)
  931. time.sleep(delay)
  932. time.sleep(random.uniform(0.1, 0.2))
  933. self.d.touch.up(path[-1][0], path[-1][1])
  934. print("模拟人类绘制完成")
  935. return True
  936. except Exception as e:
  937. print(f"模拟绘制失败: {e}")
  938. return False
  939. # 空间推理
  940. def click_side(self):
  941. try:
  942. image_xpath = '//*[@resource-id="com.sankuai.meituan:id/titans_main_layout"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.webkit.WebView[1]/android.view.View[1]'
  943. image_element = self.d.xpath(image_xpath)
  944. image_element_info = image_element.info
  945. bounds = image_element_info['bounds']
  946. image_left = bounds['left']
  947. image_top = bounds['top']
  948. image_width = bounds['right'] - bounds['left']
  949. image_height = bounds['bottom'] - bounds['top']
  950. print(f"图片位置: left={image_left}, top={image_top}, width={image_width}, height={image_height}")
  951. #
  952. image_img_path = "image.png"
  953. if self.d.xpath(image_xpath).exists:
  954. self.d.xpath(image_xpath).screenshot().save("image.png")
  955. else:
  956. print("image_xpath not exist")
  957. try:
  958. with open('image.png', 'rb') as f:
  959. c = base64.b64encode(f.read()).decode()
  960. url = "http://api.jfbym.com/api/YmServer/customApi"
  961. data = {
  962. "token": "1nDVocTE2mJ0yLEYb2sZJ5uUY2VIEoGTkIpW44X7Kgk",
  963. "type": 50009,
  964. "image": c
  965. }
  966. headers = {
  967. "Content-Type": "application/json"
  968. }
  969. # 发送请求
  970. response = requests.post(url, headers=headers, json=data, timeout=30)
  971. response.raise_for_status()
  972. result = response.json()
  973. if result.get("code") == 10000 and result.get("data", {}).get("code") == 0:
  974. verify_data = result["data"]
  975. print(f"verify_data={verify_data}")
  976. big_click_xpath = verify_data["data"]
  977. else:
  978. print("api 返回错误")
  979. except Exception as e:
  980. return {
  981. "success": False,
  982. "error_msg": f"处理异常: {str(e)}"
  983. }
  984. coords_str = verify_data["data"]
  985. if coords_str:
  986. x_img_str, y_img_str = coords_str.split(',')
  987. x_img = int(x_img_str.strip())
  988. y_img = int(y_img_str.strip())
  989. x_screen = image_left + x_img
  990. y_screen = image_top + y_img
  991. # 点击转换后的屏幕坐标
  992. self.d.click(x_screen, y_screen)
  993. time.sleep(self.get_sleep_time())
  994. return True
  995. except Exception as e:
  996. self.logger.error(f"空间推理验证失败: {e}")
  997. return False
  998. # 人工处理
  999. def _handle_generic_captcha(self, xpath):
  1000. """处理通用验证码"""
  1001. time.sleep(5)
  1002. self.logger.warning("通用验证码弹窗触发,等待人工处理...")
  1003. start_time = time.time()
  1004. timeout = 60 * 60
  1005. while time.time() - start_time < timeout:
  1006. if not self.d.xpath(xpath).exists:
  1007. self.logger.info("验证码已处理完成")
  1008. return True
  1009. time.sleep(2)
  1010. self.logger.warning("验证码处理超时")
  1011. return False
  1012. def check_and_handle_popup(self):
  1013. d = self.spider.d
  1014. exists, captcha_type, xpath = self.is_any_verification_popup_exists(d)
  1015. if not exists:
  1016. for simple_xpath, desc in self.popup_rules["simple"]:
  1017. if d.xpath(simple_xpath).exists and not self._is_recent_click(simple_xpath):
  1018. self.logger.info(f"检测到简单弹窗: {desc}")
  1019. d.xpath(simple_xpath).click()
  1020. return True
  1021. # 处理广告弹窗
  1022. if d.xpath('//*[contains(@text, "广告")]').exists:
  1023. w, h = d.info['displayWidth'], d.info['displayHeight']
  1024. d.click(w - 50, 50)
  1025. self.logger.info("关闭广告弹窗")
  1026. return True
  1027. return False
  1028. # 开始处理
  1029. now = time.time()
  1030. # 防止过于频繁触发
  1031. if now - self.last_verification_time < 30 and self.verification_in_progress.is_set():
  1032. return False
  1033. self.last_verification_time = now
  1034. self.verification_count += 1
  1035. if captcha_type != self.last_verification_type:
  1036. self.logger.info(f"验证码类型变化: {self.last_verification_type} -> {captcha_type}")
  1037. self.verification_retry_count = 0
  1038. self.last_verification_type = captcha_type
  1039. self.verification_retry_count += 1
  1040. self.logger.warning(
  1041. f"检测到验证码弹窗,类型: {captcha_type},重试次数: {self.verification_retry_count}/{self.MAX_VERIFICATION_RETRY}")
  1042. if self.verification_retry_count > self.MAX_VERIFICATION_RETRY:
  1043. self.logger.error("重试次数超限,重启应用")
  1044. self._handle_verification_failure()
  1045. return False
  1046. self.verification_in_progress.set()
  1047. self.pausing.set()
  1048. self.logger.info("已设置主线程暂停事件")
  1049. # 处理
  1050. try:
  1051. if captcha_type == "Numbers_English":
  1052. self.logger.info(f"开始处理通用数验证")
  1053. result = self.Numbers_English_verify()
  1054. elif captcha_type == "Swipe_right":
  1055. self.logger.info(f"开始处理向右滑动")
  1056. result = self.Swipe_right()
  1057. elif captcha_type == "Click_images":
  1058. self.logger.info(f"开始处理依次点击图片或语序")
  1059. result = self.Click_images()
  1060. elif captcha_type == "slider":
  1061. self.logger.info(f"开始处理滑块验证")
  1062. result = self.slider_verify()
  1063. elif captcha_type == "complexs":
  1064. self.logger.info(f"开始处理拖动滑块刚")
  1065. result = self.complexs()
  1066. elif captcha_type == "Shortest_connection":
  1067. self.logger.info(f"开始处理最短距离连接")
  1068. result = self.Shortest_connection()
  1069. elif captcha_type == "click_side":
  1070. self.logger.info(f"开始处理空间推理")
  1071. result = self.click_side()
  1072. else:
  1073. self.logger.info(f"等待人工处理")
  1074. result = self._handle_generic_captcha(xpath)
  1075. except Exception as e:
  1076. self.logger.error(f"验证码处理异常: {e}")
  1077. result = False
  1078. time.sleep(5)
  1079. verification_cleared, remaining_type = self.wait_for_verification_clear(d, timeout=7)
  1080. if verification_cleared:
  1081. self.logger.info(f"第{self.verification_retry_count}次验证成功")
  1082. time.sleep(3)
  1083. self._handle_verification_success()
  1084. return True
  1085. else:
  1086. self.logger.warning(f"第{self.verification_retry_count}次验证失败,仍有验证码: {remaining_type}")
  1087. if self.verification_retry_count >= self.MAX_VERIFICATION_RETRY:
  1088. self._handle_verification_failure()
  1089. return False
  1090. else:
  1091. self.verification_in_progress.clear()
  1092. self.pausing.clear()
  1093. time.sleep(2)
  1094. return self.check_and_handle_popup()
  1095. '''
  1096. # 1. 处理简单弹窗
  1097. for xpath, desc in self.popup_rules["simple"]:
  1098. if d.xpath(xpath).exists and not self._is_recent_click(xpath):
  1099. self.logger.info("检测到弹窗: %s", desc)
  1100. d.xpath(xpath).click()
  1101. return True
  1102. # 2. 处理验证码弹窗
  1103. for xpath in self.popup_rules["verification"]:
  1104. if d.xpath(xpath).exists:
  1105. now = time.time()
  1106. if now - self.last_verification_time < 30:
  1107. return False # 30秒内不重复触发
  1108. self.last_verification_time = now
  1109. self.verification_count += 1
  1110. self.logger.warning("验证码弹窗触发,等待人工处理...")
  1111. if self.verification_count > self.MAX_VERIFICATION_RETRY:
  1112. self.logger.error("验证码重试超限,终止任务")
  1113. self.spider.stop_all()
  1114. return True
  1115. self.pausing.set() # 通知主线程暂停
  1116. d.toast.show("需要人工处理验证码", 120)
  1117. # 等待人工处理
  1118. start = time.time()
  1119. # while time.time() - start < 120*60:
  1120. # if not d.xpath(xpath).exists:
  1121. # self.logger.info("验证码已处理")
  1122. # d.toast.show("验证完成", 2)
  1123. # self.pausing.clear() # 放行主线程
  1124. # return True
  1125. # time.sleep(5)
  1126. while True:
  1127. if not d.xpath(xpath).exists:
  1128. self.logger.info("验证码已处理")
  1129. d.toast.show("验证完成", 2)
  1130. self.pausing.clear() # 放行主线程
  1131. return True
  1132. time.sleep(5)
  1133. self.logger.warning("验证码超时,重启APP")
  1134. self.spider.restart_app()
  1135. return True
  1136. # 3. 处理广告弹窗(点击右上角)
  1137. if d.xpath('//*[contains(@text, "广告")]').exists:
  1138. w, h = d.info['displayWidth'], d.info['displayHeight']
  1139. d.click(w - 50, 50)
  1140. self.logger.info("关闭广告弹窗")
  1141. return True
  1142. return False
  1143. '''
  1144. def is_any_verification_popup_exists(self, d=None):
  1145. """
  1146. 检查是否存在任何类型的验证码弹窗
  1147. """
  1148. if d is None:
  1149. d = self.d
  1150. for xpath, captcha_type in self.popup_rules["verification"]:
  1151. if d.xpath(xpath).exists:
  1152. return True, captcha_type, xpath
  1153. # 特殊的验证码
  1154. additional_indicators = [
  1155. ('//*[contains(@resource-id, "com.sankuai.meituan:id/yoda_activity_rootView")]', "complexs"),
  1156. ('//*[contains(@text, "拖动滑块刚")]', "complexs"),
  1157. ]
  1158. for xpath, captcha_type in additional_indicators:
  1159. if d.xpath(xpath).exists:
  1160. return True, captcha_type, xpath
  1161. return False, None, None
  1162. def wait_for_verification_clear(self, d=None, timeout=10):
  1163. """
  1164. 等待验证码完全消失
  1165. """
  1166. if d is None:
  1167. d = self.d
  1168. start_time = time.time()
  1169. while time.time() - start_time < timeout:
  1170. exists, captcha_type, _ = self.is_any_verification_popup_exists(d)
  1171. if not exists:
  1172. return True, None
  1173. time.sleep(1)
  1174. exists, captcha_type, _ = self.is_any_verification_popup_exists(d)
  1175. if exists:
  1176. self.logger.info(f"超时,类型: {captcha_type}")
  1177. return False, captcha_type
  1178. else:
  1179. return True, None
  1180. def _handle_verification_success(self):
  1181. """验证成功后的处理"""
  1182. time.sleep(5)
  1183. self.verification_retry_count = 0
  1184. self.last_verification_type = None
  1185. self.verification_in_progress.clear()
  1186. self.pausing.clear()
  1187. self.verification_count = 0
  1188. self.logger.info("验证成功,清除暂停状态")
  1189. def _handle_verification_failure(self):
  1190. """验证失败 - 等待人工处理"""
  1191. self.logger.error("验证码处理失败,等待人工处理...")
  1192. # 重置验证状态,但不重启应用
  1193. self.verification_retry_count = 0
  1194. self.last_verification_type = None
  1195. self.verification_in_progress.clear()
  1196. # 设置等待人工处理的超时时间(30分钟)
  1197. timeout = 30 * 60
  1198. start_time = time.time()
  1199. # 持续监控验证码状态,等待人工处理
  1200. while time.time() - start_time < timeout:
  1201. # 检查是否还有验证码存在
  1202. exists, captcha_type, xpath = self.is_any_verification_popup_exists()
  1203. if not exists:
  1204. # 验证码已消失,可能是人工处理成功
  1205. self.logger.info("验证码已消失,人工处理成功")
  1206. # 额外等待确保页面稳定
  1207. time.sleep(3)
  1208. # 清除暂停状态,放行线程
  1209. self.pausing.clear()
  1210. self.logger.info("人工处理完成,放行线程")
  1211. return
  1212. # 每隔10秒检查一次
  1213. time.sleep(10)
  1214. # 超时处理
  1215. self.logger.warning("等待人工处理超时,尝试继续执行")
  1216. # 强制清除暂停状态,放行线程
  1217. self.pausing.clear()
  1218. self.logger.warning("已超时,强制清除暂停状态,放行线程")
  1219. def stop(self):
  1220. self.running = False
  1221. def get_access_token():
  1222. AppKey = "tRK2RhyItCSh6BzyT4CNVXQa"
  1223. AppSrcret = "TDgKiPo94i2mOM1sDqOuDnlcK1bG66jh"
  1224. token_url = 'https://aip.baidubce.com/oauth/2.0/token'
  1225. url = f"{token_url}?grant_type=client_credentials&client_id={AppKey}&client_secret={AppSrcret}"
  1226. payload = ""
  1227. headers = {
  1228. 'Content-Type': 'application/json',
  1229. 'Accept': 'application/json'
  1230. }
  1231. response = requests.request("POST", url, headers=headers, data=payload)
  1232. try:
  1233. return response.json()['access_token']
  1234. except:
  1235. return None
  1236. def get_mysql():
  1237. """
  1238. 建立并返回一个到数据库的连接对象
  1239. """
  1240. import pymysql
  1241. return pymysql.connect(
  1242. host = Config.DB_HOST, #"localhost", # 修改后的主机
  1243. port = Config.DB_PORT, #3306, # 添加端口号
  1244. user = Config.DB_USER, #'root', # 修改后的用户名
  1245. password = Config.DB_PASSWORD, # 修改后的密码
  1246. db = Config.DB_NAME, #"drug_data", # 修改后的数据库名
  1247. charset='utf8mb4'
  1248. )
  1249. class MT:
  1250. def __init__(self, key):
  1251. # self.package_name = 'com.sankuai.meituan'
  1252. self.package_name = Config.PACKAGE_NAME
  1253. self.access_token = get_access_token()
  1254. self.city2province = self.get_city_info()
  1255. self.APP_ID = '116857964'
  1256. self.API_KEY = '1gAzACJOAr7BeILKqkqPOETh'
  1257. self.SECRET_KEY = 'ZNArANb9GwJYgLKg4EfYhukKBfPdl1n3'
  1258. self.client = AipOcr(self.APP_ID, self.API_KEY, self.SECRET_KEY)
  1259. # host = Config.DB_HOST #"localhost"
  1260. # user = Config.DB_USER #"root"
  1261. # password = Config.DB_PASSWORD #"dfwy2025"
  1262. # database = Config.DB_NAME #"drug_data"
  1263. # port = Config.DB_PORT#3306
  1264. # print(f'数据库配置:host:{host},user:{user},password:{password},database:{database},port:{port}')
  1265. self.table_name = Config.DB_TABLE #"mt_drug"
  1266. self.shop_table_name = Config.DB_SHOP_TABLE
  1267. # print(f'数据库表名:table_name:{self.table_name},shop_table_name:{self.shop_table_name}')
  1268. # self.mysql_client = mysqlClient(host, user, password, database, port)
  1269. self.loggerMT = logging.getLogger()
  1270. self.search_key = key # 参苓健脾胃颗粒 舒肝颗粒 清肺化痰丸 香砂平胃颗粒
  1271. self.unrelated_data = 0 # 无关数据数量
  1272. self.shop_data_num = 0 # 店铺数据数量
  1273. def stop_app(self):
  1274. self.d.app_stop(self.package_name)
  1275. time.sleep(5)
  1276. def start_app(self):
  1277. self.d.app_start(self.package_name)
  1278. time.sleep(5)
  1279. def restart_app(self):
  1280. """
  1281. 重启app
  1282. :return:
  1283. """
  1284. self.stop_app()
  1285. self.start_app()
  1286. @staticmethod
  1287. def get_sleep_time():
  1288. # return random.randint(5, 8)
  1289. return random.randint(1, 3)
  1290. @staticmethod
  1291. def get_current_date():
  1292. return datetime.datetime.now().strftime('%Y/%m/%d')
  1293. @staticmethod
  1294. def get_city_info():
  1295. """
  1296. 获取所有的省市数据
  1297. :return:
  1298. """
  1299. file_path = '../kailin_city.json'
  1300. with open(file_path, 'r', encoding='utf-8') as f:
  1301. data = json.load(f)
  1302. province = {province_one["id"]: province_one for province_one in data['province']}
  1303. city2province = dict()
  1304. city = data['city']
  1305. for city_one in city:
  1306. name = city_one['name']
  1307. pid = city_one['pid']
  1308. if len(str(pid)) > 2:
  1309. pid = int(re.match('^\d{2}', str(pid)).group())
  1310. city2province[name] = province[pid]['name']
  1311. return city2province
  1312. #将30分钟后采集的数据上报到服务端
  1313. def up_data_to_service(self,collect_task_allocate_id,status,finish_status,real_count,start_time,end_time,start_page,end_page):
  1314. # 1. 请求地址
  1315. url = 'http://schedule.dfwy.tech/api/collect_equipment_execute/result_report'
  1316. # 2. 请求参数(字典形式)
  1317. json_data = {
  1318. "collect_task_allocate_id":4,
  1319. "status":3,
  1320. "finish_status":0,
  1321. 'real_count':20,
  1322. 'start_time':1769166394,
  1323. 'end_time':1769168194,
  1324. 'start_page':1,
  1325. 'end_page':3
  1326. }
  1327. # 3. 发送 POST 并拿到结果
  1328. # resp = requests.post(url, json=json_data, headers=headers)
  1329. resp = requests.post(url, json=json_data)
  1330. # 4. 打印结果
  1331. print("状态码:", resp.status_code)
  1332. # print("响应体(文本):\n", resp.text) # 纯文本
  1333. print("响应体(JSON):\n", resp.json()) # 如果返回的是 JSON,可改用这行
  1334. def get_shop_name(self):
  1335. """
  1336. 获取店铺名
  1337. :return:
  1338. """
  1339. try:
  1340. shop_name = self.d.xpath(
  1341. '//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
  1342. print(f'获取到店铺名:{shop_name}')
  1343. return shop_name
  1344. except:
  1345. try:
  1346. shop_name = self.d.xpath(
  1347. '//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
  1348. print(f'获取到店铺名2:{shop_name}')
  1349. return shop_name
  1350. except Exception as e:
  1351. #点击店铺曲获取店铺名称
  1352. print("点击店铺进入后获取店铺名称")
  1353. self.enter_shop()
  1354. 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'
  1355. if self.d.xpath(shop_xpath).exists:
  1356. shop_name = self.d.xpath(shop_xpath).text
  1357. self.swipe_back(1)
  1358. return shop_name
  1359. else:
  1360. print(f'获取店铺名出错:{e}')
  1361. return None
  1362. def get_qualification_number(self):
  1363. """
  1364. 获取资质编号
  1365. :return:
  1366. """
  1367. try:
  1368. qualification_number_str = self.d.xpath(
  1369. '//*[@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
  1370. qualification_number = qualification_number_str.strip('资质编号:').strip()
  1371. return qualification_number
  1372. except:
  1373. return None
  1374. def get_shop_address(self):
  1375. try:
  1376. 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'
  1377. if self.d.xpath(xpath).exists:
  1378. shop_address = self.d.xpath(xpath).text
  1379. print(f'111-获取到店铺地址:{shop_address}')
  1380. if '发货时间' in shop_address:
  1381. print(f'店铺地址包含发货时间,再次获取店铺地址')
  1382. 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'
  1383. if self.d.xpath(xpath2).exists:
  1384. shop_address = self.d.xpath(xpath2).text
  1385. print(f'222-获取到店铺地址:{shop_address}')
  1386. else:
  1387. print(f'222-xpath2获取店铺地址失败')
  1388. else:
  1389. shop_address = ''
  1390. print(f'333-获取到店铺地址:{shop_address}')
  1391. return shop_address
  1392. except:
  1393. print(f'获取店铺地址出错-get_shop_address')
  1394. return None
  1395. def enter_detail(self):
  1396. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/recycler"]/android.widget.FrameLayout[1]').click()
  1397. time.sleep(self.get_sleep_time())
  1398. def save_to_database(self, data):
  1399. print(f'保存数据到数据库:{data}')
  1400. # 连接数据库
  1401. conn = get_mysql()
  1402. # 创建游标对象
  1403. cur = conn.cursor()
  1404. # add_sql = "insert into delete_friend_table(delete_user_name,delete_user_id,delete_content,delete_time) value(%s,%s,%s,%s)"
  1405. add_sql = f"""
  1406. INSERT INTO {self.table_name}
  1407. (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)
  1408. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  1409. """
  1410. # 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']))
  1411. 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']))
  1412. conn.commit() # 提交数据
  1413. #self.mysql_client.insert(self.table_name, data)
  1414. print(f"存入数据库成功")
  1415. def save_shop_info_to_database(self, data):
  1416. print(f'保存店铺数据到数据库:{data}')
  1417. # 连接数据库
  1418. conn = get_mysql()
  1419. # 创建游标对象
  1420. cur = conn.cursor()
  1421. add_sql = f"""
  1422. INSERT INTO {self.shop_table_name}
  1423. (shop, contact_address, qualification_number, business_license_company, business_license_address, scrape_date, platform)
  1424. VALUES (%s, %s, %s, %s, %s, %s, %s)
  1425. """
  1426. 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']))
  1427. conn.commit() # 提交数据
  1428. #self.mysql_client.insert(self.shop_table_name, data)
  1429. print(f'存入店铺信息到数据库成功')
  1430. def swipe_up(self):
  1431. """
  1432. 上滑
  1433. :return:
  1434. """
  1435. screen_width = self.d.info['displayWidth']
  1436. screen_height = self.d.info['displayHeight']
  1437. duration_rate = random.uniform(0, 0.3)
  1438. self.d.swipe(screen_width // 2, screen_height - 100, screen_width // 2, 100, duration=duration_rate)
  1439. no = random.uniform(0, 1)
  1440. if no > 0.85:
  1441. # 有的时候卡着 再稍微往上滑一点点
  1442. self.d.swipe_ext("up", 0.1)
  1443. time.sleep(self.get_sleep_time())
  1444. def swipe_back(self, no):
  1445. """
  1446. 返回
  1447. :param no: 回退次数
  1448. :return:
  1449. """
  1450. for idx in range(no):
  1451. self.d.press('back')
  1452. time.sleep(self.get_sleep_time())
  1453. def drug_price(self):
  1454. """
  1455. 获取药品价格
  1456. :return:
  1457. """
  1458. try:
  1459. price_str = self.d.xpath('//*[starts-with(@text,"¥")]').text
  1460. price = float(re.search('[\d\.]+', price_str).group())
  1461. print(f'获取到价格:{price}')
  1462. return price
  1463. except Exception as e:
  1464. print(f'提取价格出错-->{e}')
  1465. return None
  1466. def restart_uiautomator_services(self, device_id):
  1467. """
  1468. 重启atx的uiautomator 服务
  1469. :param device_id:
  1470. :return:
  1471. """
  1472. stop_uiautomator_services = f'adb -s {device_id} shell /data/local/tmp/atx-agent server -d --stop'
  1473. start_uiautomator_services = f'adb -s {device_id} shell /data/local/tmp/atx-agent server -d'
  1474. # result = subprocess.run(stop_uiautomator_services, capture_output=True, text=True, shell=True)
  1475. # print(result.stdout)
  1476. subprocess.run(stop_uiautomator_services, capture_output=True, text=True, shell=True)
  1477. time.sleep(self.get_sleep_time())
  1478. subprocess.run(start_uiautomator_services, capture_output=True, text=True, shell=True)
  1479. time.sleep(self.get_sleep_time())
  1480. def connect_devices(self, device_id):
  1481. """
  1482. 连接设备
  1483. :return:
  1484. """
  1485. try:
  1486. self.d = u2.connect_usb(device_id)
  1487. # 设置隐形等待时间
  1488. # self.d.implicitly_wait(5)
  1489. self.restart_uiautomator_services(device_id)
  1490. print(f'连接到设备:{device_id}')
  1491. except Exception as e:
  1492. print(f'{device_id} 连接错误: {e}')
  1493. raise Exception(e)
  1494. def get_ocr_res(self, img):
  1495. try:
  1496. #img地址
  1497. print(f'开始识别图片:{img}')
  1498. request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license"
  1499. # 二进制方式打开图片文件
  1500. f = open(img, 'rb')
  1501. img = base64.b64encode(f.read())
  1502. params = {"image": img}
  1503. # access_token = get_access_token()
  1504. request_url = request_url + "?access_token=" + self.access_token
  1505. headers = {'content-type': 'application/x-www-form-urlencoded'}
  1506. response = requests.post(request_url, data=params, headers=headers)
  1507. if response:
  1508. res = response.json()
  1509. new_dic = dict()
  1510. for ite in res['words_result'].keys():
  1511. new_dic[ite] = res['words_result'][ite]['words']
  1512. print('资质数据信息', new_dic)
  1513. return new_dic
  1514. else:
  1515. return None
  1516. except:
  1517. return None
  1518. def remove_watermark(self, img_path):
  1519. """
  1520. 图片去水印(将水印部分变成白色背景)并将数据转化为二进制数据
  1521. :param img_path: 图片路径
  1522. :return: 二进制图片数据
  1523. """
  1524. img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), -1)
  1525. endswith = os.path.splitext(img_path)[1]
  1526. new = np.clip(1.4057577998008846 * img - 38.33089999653017, 0, 255).astype(np.uint8)
  1527. _, img_binary = cv2.imencode(endswith, new)
  1528. return img_binary
  1529. def get_ocr_res_image(self, img):
  1530. try:
  1531. image = self.remove_watermark(img)
  1532. # image_file = open(img,'wb')
  1533. # image_file.write(image)
  1534. # res_image = self.client.basicAccurate(image) # 高精度
  1535. res_image = self.client.basicGeneral(image)
  1536. # print(f'百度api返回结果:{res_image}')
  1537. # print(res_image.get('words_result', ''))
  1538. # new_dic = dict()
  1539. data = res_image.get('words_result', '')
  1540. print(f'百度api返回结果:{data}')
  1541. # full_text = ';'.join(item['words'] for item in data)
  1542. # address = ''
  1543. # for item in data:
  1544. # if '企业注册号' in item['words']:
  1545. # print('come in 111')
  1546. # reg_number = item['words'].split(':', 1)[1].strip()
  1547. # elif '企业名称' in item['words']:
  1548. # print('come in 222')
  1549. # company_name = item['words'].split(':', 1)[1].strip()
  1550. # elif '所:' in item['words']:
  1551. # print('come in 333')
  1552. # address = item['words'].split(':', 1)[1].strip()
  1553. # # 输出结果
  1554. # print("企业注册号:", reg_number)
  1555. # print("企业名称:", company_name)
  1556. # print("住所:", address)
  1557. return data
  1558. except:
  1559. return None
  1560. def screenshot_the_business_license(self, qualification_number):
  1561. screenshot_path = 'screenshot1.png'
  1562. self.d.screenshot(screenshot_path)
  1563. img = cv2.imread(screenshot_path)
  1564. # 指定裁剪区域 (left, top, right, bottom)
  1565. left = 0
  1566. top = 480
  1567. right = 720
  1568. bottom = 1420
  1569. cropped_img = img[top:bottom, left:right]
  1570. if qualification_number:
  1571. cropped_screenshot_path = 'D:\\work\\dfwy_spider\\drug_data\\mt\\screenshot\\' + qualification_number + '.png'
  1572. else:
  1573. cropped_screenshot_path = 'cropped_screenshot.png'
  1574. cv2.imwrite(cropped_screenshot_path, cropped_img)
  1575. return cropped_screenshot_path
  1576. def screenshot_instruction(self):
  1577. # 获取当前时间
  1578. current_time = datetime.datetime.now()
  1579. # 格式化为时分秒
  1580. time_str = current_time.strftime("%H-%M-%S")
  1581. # 生成随机的 8 位字符串
  1582. random_str = secrets.token_hex(4) # 生成 4 个字节的随机字符串,转换为 8 位十六进制字符串
  1583. print(time_str)
  1584. screenshot_path = 'instructionscreenshot1-' + time_str + '-' + random_str + '.png'
  1585. self.d.screenshot(screenshot_path)
  1586. return screenshot_path
  1587. #获取商品title
  1588. def get_title(self):
  1589. # try:
  1590. # title = self.d.xpath(
  1591. # '//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
  1592. # except:
  1593. # title = self.d.xpath(
  1594. # '//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
  1595. # title = self.d.xpath('//*[contains(@text, "舒肝颗粒")]').text
  1596. def _inner():
  1597. temp_search_key = self.search_key
  1598. if "999" in self.search_key:
  1599. if self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  1600. temp_search_key = self.search_key.replace("999皮炎平", "")
  1601. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  1602. temp_search_key = self.search_key.replace("999必无忧", "")
  1603. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  1604. temp_search_key = self.search_key.replace("999必无忧", "")
  1605. elif self.search_key == '999速复康布洛芬缓释胶囊':
  1606. temp_search_key = self.search_key.replace("999速复康", "")
  1607. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  1608. temp_search_key = self.search_key.replace("999选平", "")
  1609. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  1610. temp_search_key = self.search_key.replace("999皮炎平", "")
  1611. else:
  1612. temp_search_key = self.search_key.replace("999", "")
  1613. else:
  1614. if self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  1615. temp_search_key = self.search_key.replace("史达功", "")
  1616. temp_search_key = temp_search_key.replace("120", "")
  1617. elif self.search_key == '三九胃泰养胃舒颗粒8袋':
  1618. temp_search_key = self.search_key.replace("三九胃泰", "")
  1619. temp_search_key = temp_search_key.replace("8袋", "")
  1620. elif self.search_key == '今维多赐多康牌蛋白粉':
  1621. temp_search_key = self.search_key.replace("今维多", "")
  1622. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  1623. temp_search_key = self.search_key.replace("佳美舒", "")
  1624. temp_search_key = temp_search_key.replace("4", "")
  1625. elif self.search_key == '三九胃泰颗粒20g*10':
  1626. temp_search_key = self.search_key.replace("20g*10", "")
  1627. elif self.search_key == '三九胃泰颗粒20g*6袋':
  1628. temp_search_key = self.search_key.replace("20g*6袋", "")
  1629. elif self.search_key == '顺峰康王酮康他索乳膏':
  1630. temp_search_key = self.search_key.replace("顺峰康王", "")
  1631. if self.search_key == '999糠酸莫米松凝胶15':
  1632. temp_search_key = temp_search_key.replace("15", "")
  1633. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  1634. temp_search_key = temp_search_key.replace("30", "")
  1635. elif self.search_key == '999复方金银花颗粒10g':
  1636. temp_search_key = temp_search_key.replace("10g", "")
  1637. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  1638. temp_search_key = temp_search_key.replace("15g*15袋/盒", "")
  1639. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  1640. temp_search_key = temp_search_key.replace("6粒", "")
  1641. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  1642. temp_search_key = temp_search_key.replace("50", "")
  1643. elif self.search_key == '999止泻利颗粒15g*8':
  1644. temp_search_key = temp_search_key.replace("15g*8", "")
  1645. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  1646. temp_search_key = temp_search_key.replace("30", "")
  1647. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  1648. temp_search_key = temp_search_key.replace("15g", "")
  1649. elif self.search_key == '999复方苦参肠炎康片12片':
  1650. temp_search_key = temp_search_key.replace("12片", "")
  1651. elif self.search_key == '999强力枇杷露16袋':
  1652. temp_search_key = temp_search_key.replace("16袋", "")
  1653. elif self.search_key == '999三蛇胆川贝膏138':
  1654. temp_search_key = temp_search_key.replace("138", "")
  1655. elif self.search_key == '999强力枇杷露120ml':
  1656. temp_search_key = temp_search_key.replace("120ml", "")
  1657. elif self.search_key == '999强力枇杷露150ml':
  1658. temp_search_key = temp_search_key.replace("150ml", "")
  1659. elif self.search_key == '999抗病毒口服液10ml*10':
  1660. temp_search_key = temp_search_key.replace("10ml*10", "")
  1661. elif self.search_key == '999抗病毒口服液10ml*12':
  1662. temp_search_key = temp_search_key.replace("10ml*12", "")
  1663. elif self.search_key == '999糠酸莫米松乳膏10g支':
  1664. temp_search_key = temp_search_key.replace("10g支", "")
  1665. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  1666. temp_search_key = temp_search_key.replace("20g", "")
  1667. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  1668. temp_search_key = temp_search_key.replace("(无糖)6g", "")
  1669. elif self.search_key == '999壮骨关节丸6g*20':
  1670. temp_search_key = temp_search_key.replace("6g*20", "")
  1671. elif self.search_key == '999正天丸6g*15':
  1672. temp_search_key = temp_search_key.replace("6g*15", "")
  1673. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  1674. temp_search_key = temp_search_key.replace("20", "")
  1675. elif self.search_key == '999糠酸莫米松凝胶10':
  1676. temp_search_key = temp_search_key.replace("10", "")
  1677. elif self.search_key == '999板蓝根颗粒10g*20':
  1678. temp_search_key = temp_search_key.replace("10g*20", "")
  1679. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  1680. temp_search_key = temp_search_key.replace("10粒", "")
  1681. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  1682. temp_search_key = temp_search_key.replace("12粒", "")
  1683. elif self.search_key == '999咽炎片0.26g*12片*2板':
  1684. temp_search_key = temp_search_key.replace("0.26g*12片*2板", "")
  1685. elif self.search_key == '999小儿止咳糖浆120':
  1686. temp_search_key = temp_search_key.replace("120", "")
  1687. elif self.search_key == '999小儿止咳糖浆225':
  1688. temp_search_key = temp_search_key.replace("225", "")
  1689. elif self.search_key == '999小儿感冒颗粒6g*10':
  1690. temp_search_key = temp_search_key.replace("6g*10", "")
  1691. elif self.search_key == '999小儿感冒颗粒6g*24':
  1692. temp_search_key = temp_search_key.replace("6g*24", "")
  1693. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  1694. temp_search_key = temp_search_key.replace("6g*10袋", "")
  1695. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  1696. temp_search_key = temp_search_key.replace("6g*20袋", "")
  1697. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  1698. temp_search_key = temp_search_key.replace("8g*10袋", "")
  1699. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  1700. temp_search_key = temp_search_key.replace("2.5g*10袋", "")
  1701. elif self.search_key == '999感冒清热颗粒12g*18':
  1702. temp_search_key = temp_search_key.replace("12g*18", "")
  1703. # elif self.search_key == '三九胃泰颗粒':
  1704. # self.search_key = '三九胃泰' #兼容三九胃泰 温胃舒颗粒
  1705. print(f'获取商品title时的搜索关键字:{temp_search_key}')
  1706. # title = self.d.xpath(f'//*[contains(@text, "{self.search_key}")]').text
  1707. #初始化
  1708. drugs_name = ''
  1709. specifications = ''
  1710. title = ''
  1711. #循环的获取title为了有时间来处理人机验证
  1712. for m in range(1, 6000) :
  1713. if self.d.xpath(f'//*[contains(@text, "{temp_search_key}")]').exists:
  1714. title = self.safe_exec(
  1715. lambda: self.d.xpath(f'//*[contains(@text, "{temp_search_key}")]').text
  1716. )
  1717. print(f"第{m}次获取title成功")
  1718. break
  1719. else:
  1720. time.sleep(3)
  1721. # return drugs_name, specifications
  1722. # drugs_name = ''
  1723. # specifications = ''
  1724. # try:
  1725. # 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'
  1726. # 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'
  1727. # if self.d.xpath(title_xpath).exists:
  1728. # title = self.d.xpath(title_xpath).text
  1729. # print(f"title_xpath获取的title={title}")
  1730. # if temp_search_key not in title:
  1731. # return drugs_name, specifications
  1732. # elif self.d.xpath(title_xpath_2).exists:
  1733. # title = self.d.xpath(title_xpath_2).text
  1734. # print(f"title_xpath_2获取的title={title}")
  1735. # if temp_search_key not in title:
  1736. # return drugs_name, specifications
  1737. # else:
  1738. # print('title_xpath不存在,请确认')
  1739. # return drugs_name, specifications
  1740. # # title = self.d.xpath(f'//*[contains(@text, "{temp_search_key}")]').text
  1741. # except Exception as e:
  1742. # print(f"发生异常: {e}")
  1743. # return drugs_name, specifications
  1744. #奇怪:有的时候title取出来的记过第一位会多一个0
  1745. # title = self.safe_exec(self.d.xpath(f'//*[contains(@text, "{self.search_key}")]').text)
  1746. # 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
  1747. title = title[1:] if title.startswith('0') else title
  1748. print(f'获取到药品标题:{title}')
  1749. # 从里面匹配出药品名和规格
  1750. # drugs_name
  1751. # specifications
  1752. # match = re.search(r'([^\d]+)([\d\D]+)', title)
  1753. if self.search_key == '999赐多康大豆':
  1754. return title, '1罐'
  1755. if self.search_key == "999感冒清热颗粒" :
  1756. match = re.search(r'(\[[^\]]+\])(.+?)(\d+.*)', title)
  1757. else:
  1758. match = re.match(r'(\[[^\]]+\])(.*?)\s*((?:\d+\S*|\(.+))$', title)
  1759. if match:
  1760. #drugs_name = match.group(1).strip() + match.group(2).strip()
  1761. drugs_name = title
  1762. specifications = match.group(3).strip()
  1763. print("药品名:", drugs_name)
  1764. print("规格:", specifications)
  1765. # print('完整药名:', drugs_name + specifications)
  1766. return drugs_name, specifications
  1767. else:
  1768. if title == '999抗病毒口服液10ml*12' or title == '999抗病毒口服液':
  1769. drugs_name = title
  1770. specifications = '10ml*12支/盒'
  1771. return drugs_name, specifications
  1772. elif title == '999抗病毒口服液10ml*10':
  1773. drugs_name = title
  1774. specifications = '10ml*10支/盒'
  1775. return drugs_name, specifications
  1776. elif title == '999小柴胡颗粒':
  1777. drugs_name = title
  1778. specifications = '10g*9袋/盒'
  1779. return drugs_name, specifications
  1780. elif title == '999养胃舒颗粒':
  1781. drugs_name = title
  1782. specifications = '10g*6袋/盒'
  1783. return drugs_name, specifications
  1784. elif title == '三九胃泰胶囊':
  1785. drugs_name = title
  1786. specifications = '0.5g*24粒/盒'
  1787. return drugs_name, specifications
  1788. elif title == '999补脾益肠丸':
  1789. drugs_name = title
  1790. specifications = '6g*15袋/盒'
  1791. return drugs_name, specifications
  1792. elif title == '999复方感冒灵颗粒':
  1793. drugs_name = title
  1794. specifications = '14g*9袋/盒'
  1795. return drugs_name, specifications
  1796. else:
  1797. print("没有匹配到预期格式")
  1798. drugs_name = title
  1799. specifications = ''
  1800. return drugs_name, specifications
  1801. # 用 safe_exec 包装内部逻辑,确保验证码阻塞
  1802. return self.safe_exec(_inner)
  1803. def enter_shop(self):
  1804. """
  1805. 进店,方便提取资质环境
  1806. :return:
  1807. """
  1808. # self.d.xpath('//*[@text="进店"]').click()
  1809. self.d.xpath('//*[@text="店铺"]').click()
  1810. time.sleep(self.get_sleep_time())
  1811. def enter_shoper(self):
  1812. """
  1813. 进入商家
  1814. :return:
  1815. """
  1816. is_shoper_exists = 0
  1817. for i in range(10):
  1818. if self.d.xpath('//*[@text="商家"]').exists:
  1819. print(f'第{i}次商家存在')
  1820. is_shoper_exists = 1
  1821. break
  1822. else:
  1823. print(f'第{i}次商家不存在')
  1824. time.sleep(self.get_sleep_time())
  1825. if is_shoper_exists == 1:
  1826. self.d.xpath('//*[@text="商家"]').click()
  1827. time.sleep(self.get_sleep_time())
  1828. return True
  1829. else:
  1830. return False
  1831. #点击查看商家资质
  1832. def scan_shoper_license(self):
  1833. exist_shoper = 0
  1834. for i in range(10):
  1835. if self.d.xpath('//*[@text="查看商家资质"]').exists:
  1836. print(f'第{i}次查看商家资质存在')
  1837. exist_shoper = 1
  1838. break
  1839. else:
  1840. print(f'第{i}次查看商家资质不存在')
  1841. time.sleep(self.get_sleep_time())
  1842. if exist_shoper == 1:
  1843. self.d.xpath('//*[@text="查看商家资质"]').click()
  1844. time.sleep(self.get_sleep_time())
  1845. else:
  1846. self.swipe_back(1)
  1847. #验证商品的信息是否在数据库中已存在
  1848. def data_is_exists(self, data):
  1849. """
  1850. 检查指定数据是否已存在于数据库表中(仅检查存在性)
  1851. 参数:
  1852. data: 包含查询条件的字典,键为列名,值为条件值
  1853. 返回:
  1854. True: 数据存在
  1855. False: 数据不存在
  1856. None: 检查过程中出错
  1857. """
  1858. # dup_data = {'product': product, 'min_price': min_price, 'shop': shop, 'scrape_date': scrape_date,
  1859. # 'platform': '美团'}
  1860. # 1. 验证必要字段
  1861. required_keys = ['product', 'min_price', 'shop', 'scrape_date', 'platform']
  1862. if not all(key in data for key in required_keys):
  1863. missing = [key for key in required_keys if key not in data]
  1864. logging.error(f"缺少必要字段: {', '.join(missing)}")
  1865. return None
  1866. try:
  1867. # 连接数据库
  1868. conn = get_mysql()
  1869. # 创建游标对象
  1870. cur = conn.cursor()
  1871. # 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']}'"
  1872. # cur.execute(query_sql)
  1873. query_sql = """
  1874. SELECT * FROM {}
  1875. WHERE product = %s
  1876. AND min_price = %s
  1877. AND shop = %s
  1878. AND scrape_date = %s
  1879. AND platform = %s
  1880. """.format(self.table_name)
  1881. cur.execute(query_sql, (
  1882. data['product'],
  1883. data['min_price'],
  1884. data['shop'],
  1885. data['scrape_date'],
  1886. data['platform']
  1887. ))
  1888. result = cur.fetchone()
  1889. return bool(result) # 如果存在返回True,否则False
  1890. except Exception as e:
  1891. print(f"MySQL 错误: {str(e)}")
  1892. #验证店铺信息是否在数据库中已存在
  1893. def shop_is_exists_database(self, shop):
  1894. try:
  1895. # 连接数据库
  1896. conn = get_mysql()
  1897. # 创建游标对象
  1898. cur = conn.cursor()
  1899. query_sql = """
  1900. SELECT * FROM {}
  1901. WHERE shop = %s
  1902. """.format(self.shop_table_name)
  1903. cur.execute(query_sql, (
  1904. shop
  1905. ))
  1906. result = cur.fetchone()
  1907. return bool(result) # 如果存在返回True,否则False
  1908. except Exception as e:
  1909. print(f"MySQL 错误: {str(e)}")
  1910. def wait_if_verifying(self, monitor, timeout=120):
  1911. """验证码处理期间阻塞主线程"""
  1912. start = time.time()
  1913. while monitor.pausing.is_set() and time.time() - start < timeout:
  1914. time.sleep(1)
  1915. # def safe_xpath(self, xpath, timeout=10):
  1916. # """线程安全 xpath 查找"""
  1917. # self.wait_if_verifying(self.monitor)
  1918. # return self.d.xpath(xpath).wait(timeout=timeout)
  1919. def wait_for_ready(self, monitor, timeout=86400):
  1920. """进入每一页前都先等验证码"""
  1921. start = time.time()
  1922. while monitor.pausing.is_set() and time.time() - start < timeout:
  1923. time.sleep(1)
  1924. # 额外保险:如果验证码突然在这一秒才弹,再主动扫一次
  1925. monitor.check_and_handle_popup()
  1926. def safe_list(self, xpath, monitor):
  1927. """线程安全地拿商品列表"""
  1928. self.wait_for_ready(monitor)
  1929. return self.d.xpath(xpath).all()
  1930. def safe_exec(self, func, *args, **kwargs):
  1931. """
  1932. 万能安全壳:执行 func 前检查验证码,
  1933. 若监控线程已置位 pausing,则一直阻塞直到放行。
  1934. """
  1935. while self.monitor.pausing.is_set():
  1936. time.sleep(1)
  1937. # 执行真正逻辑
  1938. return func(*args, **kwargs)
  1939. def get_next_data(self, data, target):
  1940. for i, item in enumerate(data):
  1941. if item['words'] == target:
  1942. if i + 1 < len(data):
  1943. return data[i + 1]['words']
  1944. return None
  1945. def delete_instruction_screenshot(self, screenshot_path):
  1946. # 删除截图文件
  1947. try:
  1948. os.remove(screenshot_path)
  1949. print(f"截图文件已删除:{screenshot_path}")
  1950. except FileNotFoundError:
  1951. print(f"文件未找到,无法删除:{screenshot_path}")
  1952. except Exception as e:
  1953. print(f"删除文件时出错:{e}")
  1954. '''
  1955. def get_instructions_data(self):
  1956. """
  1957. 确定有说明书之后,提取所有的说明书数据
  1958. :return:
  1959. """
  1960. self.d.xpath('//*[@text="说明"]').click()
  1961. # time.sleep(random.randint(3, 5))
  1962. time.sleep(0.5)
  1963. self.d.xpath('//*[@text="查看详细说明"]').click()
  1964. # time.sleep(random.randint(3, 5))
  1965. time.sleep(0.5)
  1966. self.d.xpath('//*[@text="加载更多"]').click_exists()
  1967. loop_page = 5
  1968. # new_list = list()
  1969. new_list = []
  1970. for i in range(loop_page):
  1971. self.d.xpath('//*[@text="加载更多"]').click_exists()
  1972. time.sleep(0.2)
  1973. if i == 0:
  1974. self.d.swipe(200, 1000, 200, 300, 0.4)
  1975. else:
  1976. self.d.swipe(200, 1000, 200, 62)
  1977. time.sleep(0.2)
  1978. if self.d.xpath('//*[@text="加载更多"]').exists:
  1979. self.d.xpath('//*[@text="加载更多"]').click()
  1980. time.sleep(0.2)
  1981. all_tt = self.d.xpath(
  1982. '//*[@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()
  1983. for idx in range(1, len(all_tt) + 1):
  1984. all_tt1 = self.d.xpath(
  1985. 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()
  1986. # print(f'当前说明书列表数据:{all_tt1}')
  1987. for tt in all_tt1:
  1988. if tt.text and tt.text != '展开全文':
  1989. new_list.append(tt.text)
  1990. if i == 0:
  1991. height = 938
  1992. else:
  1993. drug_box = self.d.xpath(
  1994. '//*[@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
  1995. bounds = drug_box['bounds']
  1996. height = bounds['bottom'] - bounds['top']
  1997. if height < 938:
  1998. # print('说明书翻页到底部')
  1999. break
  2000. # 展开全文
  2001. new_list = [item for item in new_list if item != '展开全文']
  2002. print(f'当前说明书列表数据:{new_list}')
  2003. # expiry_date_index = next(idx for idx, i in enumerate(new_list) if i == '有效期')
  2004. # manufacturer_index = next(idx for idx, i in enumerate(new_list) if i == '生产单位')
  2005. # approval_number_index = next(idx for idx, i in enumerate(new_list) if i == '批准文号')
  2006. # res_data = {
  2007. # "有效期": new_list[expiry_date_index + 1],
  2008. # "生产单位": new_list[manufacturer_index + 1],
  2009. # "批准文号": new_list[approval_number_index + 1]
  2010. # }
  2011. res_data = {
  2012. "有效期": (new_list[new_list.index("有效期") + 1]) if "有效期" in new_list and new_list.index("有效期") + 1 < len(new_list) else "",
  2013. "生产单位": (new_list[new_list.index("生产单位") + 1]) if "生产单位" in new_list and new_list.index("生产单位") + 1 < len(new_list) else "",
  2014. "批准文号": (new_list[new_list.index("批准文号") + 1]) if "批准文号" in new_list and new_list.index("批准文号") + 1 < len(new_list) else ""
  2015. }
  2016. print(f'当前说明书字典数据:{res_data}')
  2017. return res_data
  2018. '''
  2019. '''
  2020. def get_instructions_data(self):
  2021. """
  2022. 确定有说明书之后,提取所有的说明书数据
  2023. :return:
  2024. """
  2025. self.d.xpath('//*[@text="说明"]').click()
  2026. # time.sleep(random.randint(3, 5))
  2027. time.sleep(0.5)
  2028. self.d.xpath('//*[@text="查看详细说明"]').click()
  2029. # time.sleep(random.randint(3, 5))
  2030. time.sleep(0.5)
  2031. # 1) 先向上滑动一次,触发“加载更多”出现
  2032. self.d.swipe(200, 1000, 200, 300, 0.4)
  2033. time.sleep(0.3)
  2034. # 2) 再进入“出现就点”的循环
  2035. while self.d.xpath('//*[@text="加载更多"]').click_exists(timeout=1):
  2036. time.sleep(0.2)
  2037. self.d.swipe(200, 1000, 200, 300, 0.4)
  2038. # self.d.swipe(200, 1000, 200, 62)
  2039. time.sleep(0.2)
  2040. # 一次性获取所有文本
  2041. texts = [
  2042. node.text.strip()
  2043. # for node in self.d.xpath('//android.widget.TextView').all()
  2044. for node in self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]//android.widget.TextView').all()
  2045. if node.text and node.text.strip() and node.text != '加载更多'
  2046. ]
  2047. print(f'当前说明书列表数据:{texts}')
  2048. # 提取关键字段
  2049. def safe_get(key):
  2050. # try:
  2051. # idx = texts.index(key)
  2052. # return texts[idx + 1] if idx + 1 < len(texts) else ""
  2053. # except ValueError:
  2054. # return ""
  2055. try:
  2056. idx = next(i for i, text in enumerate(texts) if text == key)
  2057. return texts[idx + 1] if idx + 1 < len(texts) else ""
  2058. except StopIteration:
  2059. return ""
  2060. res_data = {
  2061. "有效期": safe_get("有效期"),
  2062. "生产单位": safe_get("生产单位"),
  2063. "批准文号": safe_get("批准文号")
  2064. }
  2065. print(f'当前说明书字典数据:{res_data}')
  2066. return res_data
  2067. '''
  2068. '''
  2069. def get_instructions_data(self):
  2070. """
  2071. 说明书键值对采集:连续两个 TextView 为一对,精确提取
  2072. """
  2073. # 1. 进入说明书
  2074. self.d(text="说明").click()
  2075. time.sleep(0.5)
  2076. self.d(text="查看详细说明").click()
  2077. time.sleep(0.5)
  2078. # self.d(text="加载更多").click_exists(timeout=0.5)
  2079. # 2. 找到说明书最外层 ScrollView(页面主体)
  2080. scroll_view = self.d(resourceId="com.sankuai.meituan:id/container") .child(className="android.widget.ScrollView")
  2081. count = scroll_view.count
  2082. print(f"找到的 ScrollView 数量: {count}")
  2083. if not scroll_view.exists:
  2084. return {"有效期": "", "生产单位": "", "批准文号": ""}
  2085. # 3. 在 ScrollView 内再定位真正包含键值对的容器
  2086. # 绝大多数美团说明书页面对应的是 ScrollView > ViewGroup > 若干 TextView
  2087. kv_container = scroll_view.child(className="android.view.ViewGroup")
  2088. if not kv_container.exists:
  2089. kv_container = scroll_view # 降级:直接对 ScrollView 取子孙 TextView
  2090. # 4. 滑动到底并收集所有 TextView(保留顺序)
  2091. all_texts = []
  2092. max_swipe = 5
  2093. last_length = 0
  2094. for _ in range(max_swipe):
  2095. texts = kv_container.child(className="android.widget.TextView")
  2096. #获取texts中的文本
  2097. print(f'当前说明书列表数据:{texts}')
  2098. current_texts = []
  2099. self.loggerMT.info(f'说明书111')
  2100. for tv in texts:
  2101. try:
  2102. txt = tv.get_text().strip()
  2103. # txt = tv.info['text'].strip()
  2104. except Exception:
  2105. continue
  2106. if txt and txt != "展开全文":
  2107. current_texts.append(txt)
  2108. self.loggerMT.info(f'说明书222')
  2109. print(f'当前说明书列表数据:{current_texts}')
  2110. # 去重
  2111. if current_texts:
  2112. current_texts = [t for t in current_texts if t not in all_texts]
  2113. all_texts.extend(current_texts)
  2114. # 判断是否到底
  2115. # if not scroll_view.info.get("scrollable"):
  2116. # break
  2117. # 判断是否到底
  2118. if len(all_texts) == last_length:
  2119. break
  2120. last_length = len(all_texts)
  2121. # self.d.swipe_ext("up", scale=0.7)
  2122. #向上滑动一次
  2123. self.d.swipe(200, 1000, 200, 300, 0.2)
  2124. time.sleep(0.2)
  2125. if self.d.xpath('//*[@text="加载更多"]').exists:
  2126. self.d.xpath('//*[@text="加载更多"]').click()
  2127. # 5. 成对解析
  2128. res_data = {"有效期": "", "生产单位": "", "批准文号": ""}
  2129. for i in range(len(all_texts) - 1):
  2130. key = all_texts[i]
  2131. val = all_texts[i + 1]
  2132. if key in res_data:
  2133. res_data[key] = val
  2134. print(f'说明书文本共 {len(all_texts)} 条,提取结果: {res_data}')
  2135. # time.sleep(1000000)
  2136. return res_data
  2137. '''
  2138. def get_instructions_data(self):
  2139. """
  2140. 确定有说明书之后,提取所有的说明书数据
  2141. :return:
  2142. """
  2143. self.d.xpath('//*[@text="说明"]').click()
  2144. # time.sleep(random.randint(3, 5))
  2145. time.sleep(0.5)
  2146. if self.d.xpath('//*[@text="查看详细说明"]').exists:
  2147. self.d.xpath('//*[@text="查看详细说明"]').click()
  2148. else:
  2149. for i in range(8):
  2150. if self.d.xpath('//*[@text="查看全部"]').exists:
  2151. print('开始点击查看全部')
  2152. break
  2153. self.d.swipe_ext('down', 0.3)
  2154. time.sleep(1)
  2155. if self.d.xpath('//*[@text="查看全部"]').exists:
  2156. print('开始点击查看全部2')
  2157. break
  2158. if self.d.xpath('//*[@text="查看全部"]').exists:
  2159. self.d.xpath('//*[@text="查看全部"]').click()
  2160. else:
  2161. res_data = {
  2162. "有效期": '',
  2163. "生产单位": '',
  2164. "批准文号": ''
  2165. }
  2166. self.loggerMT.info('获取到的说明书信息为空。')
  2167. return res_data
  2168. # time.sleep(random.randint(3, 5))
  2169. time.sleep(0.5)
  2170. # self.d.xpath('//*[@text="加载更多"]').click_exists()
  2171. # loop_page = 5
  2172. # new_list = list()
  2173. # new_list = []
  2174. for ii in range(8):
  2175. if self.d.xpath('//*[@text="加载更多"]').exists:
  2176. self.d.xpath('//*[@text="加载更多"]').click()
  2177. time.sleep(0.2)
  2178. break
  2179. else:
  2180. self.d.swipe(200, 1000, 200, 300, 0.3)
  2181. for iii in range(10):
  2182. if self.d.xpath('//*[@text="生产单位"]').exists and self.d.xpath('//*[@text="批准文号"]').exists:
  2183. break
  2184. else:
  2185. self.d.swipe(200, 1300, 200, 300, 0.3)
  2186. instruction_path = self.screenshot_instruction()
  2187. print(f"instruction_path= {instruction_path}")
  2188. time.sleep(2)
  2189. ocr_res = self.get_ocr_res_image(instruction_path)
  2190. # print(f'ocr_res:{ocr_res}')
  2191. if ocr_res:
  2192. # 获取有效期的下一个数据
  2193. validity = self.get_next_data(ocr_res, '有效期')
  2194. # 获取批准文号的下一个数据
  2195. approval_number = self.get_next_data(ocr_res, '批准文号')
  2196. # 获取生产单位的下一个数据
  2197. manufacturer = self.get_next_data(ocr_res, '生产单位')
  2198. else:
  2199. validity = ''
  2200. approval_number = ''
  2201. manufacturer = ''
  2202. # print("有效期:", validity)
  2203. # print("批准文号:", approval_number)
  2204. # print("生产单位:", manufacturer)
  2205. res_data = {
  2206. "有效期": validity,
  2207. "生产单位": manufacturer,
  2208. "批准文号": approval_number
  2209. }
  2210. print(f"res_data={res_data}")
  2211. time.sleep(1)
  2212. self.delete_instruction_screenshot(instruction_path)
  2213. return res_data
  2214. def has_instructions(self):
  2215. """
  2216. 是否有说明书
  2217. :return:
  2218. """
  2219. # 没有说明书的无法采集具体数据
  2220. time.sleep(self.get_sleep_time())
  2221. is_has_instructions = False
  2222. for i in range(8):
  2223. if self.d.xpath('//*[@text="说明"]').exists:
  2224. print(f"第{i}次有说明书1")
  2225. is_has_instructions = True
  2226. break
  2227. self.d.swipe_ext('down', 0.3)
  2228. time.sleep(1)
  2229. # detail_info = self.d.xpath(
  2230. # '//android.widget.ScrollView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[6]').info
  2231. # bounds = detail_info['bounds']
  2232. # height = bounds['bottom'] - bounds['top']
  2233. # if self.d.xpath('//*[@text="进店"]').exists and height > 100:
  2234. if self.d.xpath('//*[@text="说明"]').exists:
  2235. is_has_instructions = True
  2236. print(f"第{i}次有说明书2")
  2237. break
  2238. # is_has_instructions = self.d.xpath('//*[@text="说明"]').exists
  2239. return is_has_instructions
  2240. def has_shop(self):
  2241. """
  2242. 是否有进店按钮
  2243. :return:
  2244. """
  2245. # self.d.swipe_ext('up', 0.1)
  2246. time.sleep(self.get_sleep_time())
  2247. is_has_enter_shop = self.d.xpath('//*[@text="进店"]').exists
  2248. return is_has_enter_shop
  2249. #获取商品对应的店铺信息
  2250. def get_license_info_ex(self):
  2251. # self.enter_shop()
  2252. self.safe_exec(self.enter_shop)
  2253. # self.enter_shoper()
  2254. result = self.safe_exec(self.enter_shoper)
  2255. if result == False:
  2256. license_info_data = {'contact_address': '', 'qualification_number': '', 'business_license_company': '', 'business_license_address': ''}
  2257. return license_info_data
  2258. for i in range(10):
  2259. if self.d.xpath('//*[@text="查看商家资质"]').exists:
  2260. print(f"第{i}次有商家资质")
  2261. break
  2262. else:
  2263. print(f"第{i}次没有商家资质")
  2264. time.sleep(self.get_sleep_time())
  2265. #获取地址
  2266. # contact_address = self.get_shop_address()
  2267. contact_address = self.safe_exec(self.get_shop_address)
  2268. # time.sleep(50000)
  2269. ###
  2270. # self.scan_shoper_license()
  2271. self.safe_exec(self.scan_shoper_license)
  2272. # 获取资质编码
  2273. # qualification_number = self.get_qualification_number()
  2274. qualification_number = self.safe_exec(self.get_qualification_number)
  2275. #qualification_number 不为None继续下一步
  2276. if qualification_number:
  2277. #营业执照公司名称
  2278. business_license_company = ''
  2279. #营业执照地址
  2280. business_license_address = ''
  2281. self.d.click(0.603, 0.27)
  2282. time.sleep(self.get_sleep_time())
  2283. cropped_screenshot_path = self.screenshot_the_business_license(qualification_number)
  2284. print(f'cropped_screenshot_path:{cropped_screenshot_path}')
  2285. # if qualification_number:
  2286. # cropped_screenshot_path = 'D:\\work\\dfwy_spider\\drug_data\\mt\\screenshot\\' + qualification_number + '.png'
  2287. # else:
  2288. # cropped_screenshot_path = 'cropped_screenshot.png'
  2289. # ocr_res = self.get_ocr_res('cropped_screenshot.png')
  2290. ocr_res = self.get_ocr_res(cropped_screenshot_path)
  2291. print(f'ocr_res:{ocr_res}')
  2292. #获取ocr_res 中的地址、单位名称
  2293. if ocr_res:
  2294. if '单位名称' in ocr_res.keys():
  2295. business_license_company = ocr_res['单位名称']
  2296. if '地址' in ocr_res.keys():
  2297. business_license_address = ocr_res['地址']
  2298. license_info_data = {'contact_address': contact_address, 'qualification_number': qualification_number, 'business_license_company': business_license_company, 'business_license_address': business_license_address}
  2299. else:
  2300. license_info_data = {'contact_address': contact_address, 'qualification_number': '', 'business_license_company': '', 'business_license_address': ''}
  2301. return license_info_data
  2302. """暂不用该功能
  2303. def get_license_info(self):
  2304. self.enter_shop()
  2305. self.enter_shoper()
  2306. self.scan_shoper_license()
  2307. # 获取资质编码
  2308. qualification_number = self.get_qualification_number()
  2309. if qualification_number:
  2310. table_license_info = self.get_table_license_info(qualification_number)
  2311. if table_license_info:
  2312. return {
  2313. '单位名称': table_license_info[0],
  2314. '地址': table_license_info[1],
  2315. '社会信用代码': table_license_info[2]
  2316. }
  2317. else:
  2318. # operate_no = random.randint(0, 1)
  2319. self.d.click(0.603, 0.27)
  2320. # if operate_no == 0:
  2321. # self.d.xpath('//*[@text="营业执照"]').click()
  2322. # else:
  2323. # self.d.click(0.603, 0.27)
  2324. time.sleep(self.get_sleep_time())
  2325. self.screenshot_the_business_license()
  2326. ocr_res = self.get_ocr_res('cropped_screenshot.png')
  2327. return ocr_res
  2328. # operate_no = random.randint(0, 1)
  2329. self.d.click(0.603, 0.27)
  2330. # if operate_no == 0:
  2331. # self.d.xpath('//*[@text="营业执照"]').click()
  2332. # else:
  2333. # self.d.click(0.603, 0.27)
  2334. time.sleep(self.get_sleep_time())
  2335. self.screenshot_the_business_license()
  2336. ocr_res = self.get_ocr_res('cropped_screenshot.png')
  2337. return ocr_res
  2338. """
  2339. def distinct_target(self):
  2340. result = False
  2341. 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]'
  2342. 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]'
  2343. is_position = self.d.xpath(position_xpath).exists
  2344. is_position2 = self.d.xpath(position_xpath2).exists
  2345. 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()]'
  2346. 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()]'
  2347. 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()]'
  2348. 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()]'
  2349. is_position5 = self.d.xpath(xpath).exists
  2350. is_position6 = self.d.xpath(xpath2).exists
  2351. is_position7 = self.d.xpath(xpath3).exists
  2352. is_position8 = self.d.xpath(xpath4).exists
  2353. # print(f"is_position = {is_position}")
  2354. # print(f"is_position2 = {is_position2}")
  2355. if is_position or is_position2 or is_position5 or is_position6 or is_position7 or is_position8:
  2356. result = True
  2357. return result
  2358. # return is_position
  2359. def enter_target_page(self):
  2360. self.d.xpath('//*[@content-desc="看病买药"]').click()
  2361. time.sleep(self.get_sleep_time())
  2362. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/vf_search_carousel_text"]').click()
  2363. time.sleep(self.get_sleep_time())
  2364. 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()
  2365. time.sleep(self.get_sleep_time())
  2366. self.d.send_keys(self.search_key, clear=True)
  2367. time.sleep(self.get_sleep_time())
  2368. self.d.xpath('//*[@text="搜索"]').click()
  2369. time.sleep(self.get_sleep_time())
  2370. # content_frame = self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/container"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]').exists
  2371. # print(content_frame)
  2372. # 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]'
  2373. # 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]'
  2374. # if self.d.xpath(position_xpath1).exists:
  2375. # print("position_xpath1 exist")
  2376. # elif self.d.xpath(position_xpath2).exists:
  2377. # print("position_xpath2 exist")
  2378. # else:
  2379. # print("position_xpath not exist")
  2380. # time.sleep(10000)
  2381. #增加点击快递送
  2382. self.click_express_send()
  2383. time.sleep(self.get_sleep_time())
  2384. def click_express_send(self):
  2385. # xpath= '//*[@resource-id="com.sankuai.meituan:id/container"]//android.widget.HorizontalScrollView[last()]'
  2386. 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]'
  2387. 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]'
  2388. 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]'
  2389. 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]'
  2390. for i in range (1,3):
  2391. if self.d.xpath(slide_xpath).exists:
  2392. bounds = self.d.xpath(slide_xpath).info['bounds']
  2393. top = bounds['top']
  2394. bottom = bounds['bottom']
  2395. print(f'top={top}')
  2396. print(f'bottom={bottom}')
  2397. y = (top + bottom) // 2
  2398. print(f'y={y}')
  2399. self.loggerMT.info('开始滑动1')
  2400. self.d.swipe(500, y, 100, y, 0.5)
  2401. time.sleep(self.get_sleep_time())
  2402. break
  2403. elif self.d.xpath(slide_xpath2).exists:
  2404. bounds = self.d.xpath(slide_xpath2).info['bounds']
  2405. top = bounds['top']
  2406. bottom = bounds['bottom']
  2407. print(f'top={top}')
  2408. print(f'bottom={bottom}')
  2409. y = (top + bottom) // 2
  2410. print(f'y={y}')
  2411. self.loggerMT.info('开始滑动2')
  2412. self.d.swipe(500, y, 100, y, 0.5)
  2413. time.sleep(self.get_sleep_time())
  2414. break
  2415. elif self.d.xpath(slide_xpath3).exists:
  2416. bounds = self.d.xpath(slide_xpath3).info['bounds']
  2417. top = bounds['top']
  2418. bottom = bounds['bottom']
  2419. print(f'top={top}')
  2420. print(f'bottom={bottom}')
  2421. y = (top + bottom) // 2
  2422. print(f'y={y}')
  2423. self.loggerMT.info('开始滑动3')
  2424. self.d.swipe(500, y, 100, y, 0.5)
  2425. time.sleep(self.get_sleep_time())
  2426. break
  2427. elif self.d.xpath(slide_xpath4).exists:
  2428. bounds = self.d.xpath(slide_xpath4).info['bounds']
  2429. top = bounds['top']
  2430. bottom = bounds['bottom']
  2431. print(f'top={top}')
  2432. print(f'bottom={bottom}')
  2433. y = (top + bottom) // 2
  2434. print(f'y={y}')
  2435. self.loggerMT.info('开始滑动4')
  2436. self.d.swipe(500, y, 100, y, 0.5)
  2437. time.sleep(self.get_sleep_time())
  2438. break
  2439. max_retry = 5 # 最多尝试次数
  2440. for idx in range(1, max_retry + 1):
  2441. # 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]'
  2442. 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()]'
  2443. 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()]'
  2444. 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()]'
  2445. 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()]'
  2446. # print(f"xpath:{xpath}")
  2447. # scroll_view = self.d(resourceId="com.sankuai.meituan:id/container") .child(className="android.widget.HorizontalScrollView")
  2448. if self.d.xpath(xpath).exists:
  2449. self.d.xpath(xpath).click()
  2450. # time.sleep(self.get_sleep_time())
  2451. print(f"第{idx}次点击xpath快递送成功")
  2452. time.sleep(self.get_sleep_time())
  2453. break
  2454. elif self.d.xpath(xpath2).exists:
  2455. self.d.xpath(xpath2).click()
  2456. # time.sleep(self.get_sleep_time())
  2457. print(f"第{idx}次点击xpath2快递送成功")
  2458. time.sleep(self.get_sleep_time())
  2459. break
  2460. elif self.d.xpath(xpath3).exists:
  2461. self.d.xpath(xpath3).click()
  2462. # time.sleep(self.get_sleep_time())
  2463. print(f"第{idx}次点击xpath3快递送成功")
  2464. time.sleep(self.get_sleep_time())
  2465. break
  2466. elif self.d.xpath(xpath4).exists:
  2467. self.d.xpath(xpath4).click()
  2468. # time.sleep(self.get_sleep_time())
  2469. print(f"第{idx}次点击xpath4快递送成功")
  2470. time.sleep(self.get_sleep_time())
  2471. break
  2472. else:
  2473. print(f"第{idx}次点击xpath或xpath2或xpath3快递送都失败")
  2474. time.sleep(self.get_sleep_time())
  2475. # 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()]'
  2476. # if self.d.xpath(xpath2).exists:
  2477. # self.d.xpath(xpath2).click()
  2478. # print(f"第{idx}次点击xpath2快递送成功")
  2479. # time.sleep(self.get_sleep_time())
  2480. # break
  2481. """暂不用该功能
  2482. def get_table_license_info(self, qualification_number):
  2483. try:
  2484. sql = f'select business_license_company,city,credit_code from mt_drug where credit_code = "{qualification_number}"'
  2485. self.mysql_client.cur.execute(sql)
  2486. res = self.mysql_client.cur.fetchone()
  2487. return res
  2488. except:
  2489. return None
  2490. """
  2491. # def get_clipboard(self):
  2492. # """通过ADB获取Android手机剪贴板内容"""
  2493. # try:
  2494. # result = subprocess.run(
  2495. # ["adb", "shell", "am", "broadcast", "-a", "clipper.get"],
  2496. # capture_output=True,
  2497. # text=True,
  2498. # timeout=5
  2499. # )
  2500. # print(f"获取剪贴板结果: {result.stdout}")
  2501. # # 解析返回信息中的剪贴板内容
  2502. # for line in result.stdout.splitlines():
  2503. # if "data=" in line:
  2504. # return line.split("data=")[1].strip()
  2505. # return ""
  2506. # except Exception as e:
  2507. # print("获取剪贴板失败:", e)
  2508. # return ""
  2509. # def get_clipboard(self):
  2510. # """读取 Android 剪贴板(系统自带命令)"""
  2511. # try:
  2512. # text = subprocess.check_output(
  2513. # ["adb", "shell", "cmd", "clipboard", "get"],
  2514. # text=True, timeout=5, stderr=subprocess.STDOUT
  2515. # ).strip()
  2516. # print(f"获取剪贴板结果: {text}")
  2517. # return text if text else ""
  2518. # except Exception as e:
  2519. # print("获取剪贴板失败:", e)
  2520. # return ""
  2521. def get_clipboard(self):
  2522. time.sleep(1)
  2523. self.loggerMT.info(f"Clipboard content:{self.d.clipboard}") # 打印调试信息
  2524. clipboard_content = self.d.clipboard
  2525. if clipboard_content is None:
  2526. return ''
  2527. return clipboard_content.strip()
  2528. # return self.d.clipboard.strip()
  2529. def clear_clipboard(self):
  2530. self.d.set_clipboard("", "text/plain")
  2531. # def clear_clipboard(self):
  2532. # """清空手机剪贴板:写入空字符串(subprocess 版)"""
  2533. # try:
  2534. # subprocess.run(
  2535. # ["adb", "shell", "am", "broadcast", "-a", "clipper.set", "-e", "text", " "],
  2536. # check=True,
  2537. # capture_output=True,
  2538. # text=True,
  2539. # timeout=5
  2540. # )
  2541. # except subprocess.CalledProcessError as e:
  2542. # print("ADB 清空失败:", e.stderr)
  2543. # def clear_clipboard():
  2544. # """清空手机剪贴板:写入空字符串"""
  2545. # try:
  2546. # adb_shell(["shell", "am", "broadcast", "-a", "clipper.set", "-e", "text", ""])
  2547. # except subprocess.CalledProcessError as e:
  2548. # print("ADB 清空失败:", e.output)
  2549. #获取一个商品的数据、商品对应的店铺的数据
  2550. def get_product_link(self):
  2551. product_link = ''
  2552. # 两种可能的“···”按钮
  2553. dots_xpaths = [
  2554. '//*[@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]',
  2555. '//*[@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]',
  2556. '//*[@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]'
  2557. ]
  2558. max_retry = 5 # 最多尝试次数
  2559. for idx in range(1, max_retry + 1):
  2560. if product_link: # 已经拿到则退出
  2561. break
  2562. for xp in dots_xpaths:
  2563. if self.d.xpath(xp).exists:
  2564. print(f'{idx}-进入分享点点点')
  2565. self.loggerMT.info(f'{idx}-进入分享点点点')
  2566. # #先清空剪贴板的内容
  2567. # self.clear_clipboard()
  2568. # print("清空剪贴板内容成功。")
  2569. self.d.xpath(xp).click()
  2570. time.sleep(0.2)
  2571. self.d.xpath('//*[@text="分享商品"]').click_exists()
  2572. time.sleep(0.2)
  2573. link_xpath = '//*[@text="复制链接"]'
  2574. if self.d.xpath(link_xpath).exists:
  2575. self.d.xpath(link_xpath).click()
  2576. time.sleep(1)
  2577. product_link = self.get_clipboard()
  2578. time.sleep(0.5)
  2579. print(f'{idx}-商品链接:{product_link}')
  2580. self.loggerMT.info(f'{idx}-商品链接:{product_link}')
  2581. break # 找到并执行后跳出内层循环
  2582. else:
  2583. print(f'{idx}-商品链接:{product_link}')
  2584. self.loggerMT.info(f'{idx}-商品链接:{product_link}')
  2585. product_link = ''
  2586. # self.d.xpath('//*[@text="复制链接"]').click_exists()
  2587. # time.sleep(1)
  2588. # product_link = self.get_clipboard()
  2589. # time.sleep(0.5)
  2590. # print(f'{idx}-商品链接:{product_link}')
  2591. # self.loggerMT.info(f'{idx}-商品链接:{product_link}')
  2592. # break # 找到并执行后跳出内层循环
  2593. if not product_link and idx < max_retry:
  2594. time.sleep(0.5) # 最后一次不需要再等待
  2595. return product_link
  2596. def integrate_data(self):
  2597. #测试说明书详情:
  2598. # instructions_info = self.safe_exec(self.get_instructions_data)
  2599. # time.sleep(1000000)
  2600. #测试店铺信息
  2601. # license_info = self.safe_exec(self.get_license_info_ex)
  2602. # time.sleep(1000000)
  2603. #测试定位地址
  2604. #获取链接开始
  2605. #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
  2606. #1、点击页面的... 先判断元素是否存在
  2607. '''
  2608. 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:
  2609. print('1-进入分享点点点111')
  2610. self.loggerMT.info('1-进入分享点点点111')
  2611. 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()
  2612. #点击分享商品
  2613. # 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:
  2614. time.sleep(0.2)
  2615. self.d.xpath('//*[@text="分享商品"]').click_exists()
  2616. time.sleep(0.2)
  2617. self.d.xpath('//*[@text="复制链接"]').click_exists()
  2618. time.sleep(1)
  2619. #获取剪切板的数据
  2620. product_link = self.get_clipboard()
  2621. time.sleep(0.5)
  2622. print(f'1-商品链接:{product_link}')
  2623. self.loggerMT.info(f'1-商品链接:{product_link}')
  2624. #清空剪切板
  2625. # self.clear_clipboard()
  2626. # if self.d.xpath('//*[@text="加载更多"]').click_exists():
  2627. # 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()
  2628. # if self.d.xpath('//android.support.v7.widget.RecyclerView/android.view.ViewGroup[3]/android.widget.ImageView[1]').exists:
  2629. # self.d.xpath('//android.support.v7.widget.RecyclerView/android.view.ViewGroup[3]/android.widget.ImageView[1]').click()
  2630. # #获取剪切板的数据
  2631. # product_link = self.get_clipboard()
  2632. # time.sleep(0.5)
  2633. # print(f'商品链接:{product_link}')
  2634. # #清空剪切板
  2635. # self.clear_clipboard()
  2636. # else:
  2637. # print('未找到分享按钮111')
  2638. 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:
  2639. print('1-进入分享点点点222')
  2640. self.loggerMT.info('1-进入分享点点点222')
  2641. 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()
  2642. time.sleep(0.2)
  2643. self.d.xpath('//*[@text="分享商品"]').click_exists()
  2644. time.sleep(0.2)
  2645. self.d.xpath('//*[@text="复制链接"]').click_exists()
  2646. time.sleep(1)
  2647. #获取剪切板的数据
  2648. product_link = self.get_clipboard()
  2649. time.sleep(0.5)
  2650. print(f'1-商品链接:{product_link}')
  2651. self.loggerMT.info(f'1-商品链接:{product_link}')
  2652. #如果为获取到product_link 则等待0.5秒再获取
  2653. if not product_link:
  2654. time.sleep(0.5)
  2655. 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:
  2656. print('2-进入分享点点点111')
  2657. self.loggerMT.info('2-进入分享点点点111')
  2658. 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()
  2659. #点击分享商品
  2660. # 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:
  2661. time.sleep(0.2)
  2662. self.d.xpath('//*[@text="分享商品"]').click_exists()
  2663. time.sleep(0.2)
  2664. self.d.xpath('//*[@text="复制链接"]').click_exists()
  2665. time.sleep(1)
  2666. #获取剪切板的数据
  2667. product_link = self.get_clipboard()
  2668. time.sleep(0.5)
  2669. print(f'2-商品链接:{product_link}')
  2670. self.loggerMT.info(f'2-商品链接:{product_link}')
  2671. 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:
  2672. print('2-进入分享点点点222')
  2673. self.loggerMT.info('2-进入分享点点点222')
  2674. 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()
  2675. time.sleep(0.2)
  2676. self.d.xpath('//*[@text="分享商品"]').click_exists()
  2677. time.sleep(0.2)
  2678. self.d.xpath('//*[@text="复制链接"]').click_exists()
  2679. time.sleep(1)
  2680. #获取剪切板的数据
  2681. product_link = self.get_clipboard()
  2682. time.sleep(0.5)
  2683. print(f'2-商品链接:{product_link}')
  2684. self.loggerMT.info(f'2-商品链接:{product_link}')
  2685. #如果为获取到product_link 则等待0.5秒再获取
  2686. if not product_link:
  2687. time.sleep(0.5)
  2688. 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:
  2689. print('3-进入分享点点点111')
  2690. self.loggerMT.info('3-进入分享点点点111')
  2691. 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()
  2692. #点击分享商品
  2693. # 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:
  2694. time.sleep(0.2)
  2695. self.d.xpath('//*[@text="分享商品"]').click_exists()
  2696. time.sleep(0.2)
  2697. self.d.xpath('//*[@text="复制链接"]').click_exists()
  2698. time.sleep(1)
  2699. #获取剪切板的数据
  2700. product_link = self.get_clipboard()
  2701. time.sleep(0.5)
  2702. print(f'3-商品链接:{product_link}')
  2703. self.loggerMT.info(f'3-商品链接:{product_link}')
  2704. 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:
  2705. print('3-进入分享点点点222')
  2706. self.loggerMT.info('3-进入分享点点点222')
  2707. 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()
  2708. time.sleep(0.2)
  2709. self.d.xpath('//*[@text="分享商品"]').click_exists()
  2710. time.sleep(0.2)
  2711. self.d.xpath('//*[@text="复制链接"]').click_exists()
  2712. time.sleep(1)
  2713. #获取剪切板的数据
  2714. product_link = self.get_clipboard()
  2715. time.sleep(0.5)
  2716. print(f'3-商品链接:{product_link}')
  2717. self.loggerMT.info(f'3-商品链接:{product_link}')
  2718. '''
  2719. #获取链接结束
  2720. """
  2721. 整合数据
  2722. :return:
  2723. """
  2724. # title_info = self.get_title() # 药品,规格
  2725. # title_info = self.safe_exec(self.get_title) # 药品,规格
  2726. product, specifications = self.safe_exec(self.get_title) # 药品,规格
  2727. if product:
  2728. # product, specifications = title_info
  2729. #如果关键字包含999 则 product必须包含999 和 999后面的那段字符串 ps 999感冒灵颗粒必须包含:"999"和"感冒灵颗粒"
  2730. if '999' in self.search_key:
  2731. if self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  2732. temp_search_key = self.search_key.replace('999皮炎平', '')
  2733. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  2734. temp_search_key = self.search_key.replace('999必无忧', '')
  2735. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  2736. temp_search_key = self.search_key.replace('999必无忧', '')
  2737. elif self.search_key == '999速复康布洛芬缓释胶囊':
  2738. temp_search_key = self.search_key.replace('999速复康', '')
  2739. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  2740. temp_search_key = self.search_key.replace('999选平', '')
  2741. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  2742. temp_search_key = self.search_key.replace('999皮炎平', '')
  2743. else:
  2744. temp_search_key = self.search_key.replace('999', '')
  2745. if self.search_key == '999糠酸莫米松凝胶15':
  2746. temp_search_key = temp_search_key.replace('15', '')
  2747. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  2748. temp_search_key = temp_search_key.replace('30', '')
  2749. elif self.search_key == '999抗病毒口服液10ml*6支/盒':
  2750. temp_search_key = temp_search_key.replace("10ml*6支/盒", "")
  2751. elif self.search_key == '999复方金银花颗粒10g':
  2752. temp_search_key = temp_search_key.replace("10g", "")
  2753. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  2754. temp_search_key = temp_search_key.replace("15g*15袋/盒", "")
  2755. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  2756. temp_search_key = temp_search_key.replace("6粒", "")
  2757. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  2758. temp_search_key = temp_search_key.replace("50", "")
  2759. elif self.search_key == '999止泻利颗粒15g*8':
  2760. temp_search_key = temp_search_key.replace("15g*8", "")
  2761. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  2762. temp_search_key = temp_search_key.replace("30", "")
  2763. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  2764. temp_search_key = temp_search_key.replace("15g", "")
  2765. elif self.search_key == '999复方苦参肠炎康片12片':
  2766. temp_search_key = temp_search_key.replace("12片", "")
  2767. elif self.search_key == '999强力枇杷露16袋':
  2768. temp_search_key = temp_search_key.replace("16袋", "")
  2769. elif self.search_key == '999三蛇胆川贝膏138':
  2770. temp_search_key = temp_search_key.replace("138", "")
  2771. elif self.search_key == '999抗病毒口服液10ml*12':
  2772. temp_search_key = temp_search_key.replace("10ml*12", "")
  2773. elif self.search_key == '999抗病毒口服液10ml*10':
  2774. temp_search_key = temp_search_key.replace("10ml*10", "")
  2775. elif self.search_key == '999强力枇杷露120ml':
  2776. temp_search_key = temp_search_key.replace("120ml", "")
  2777. elif self.search_key == '999强力枇杷露150ml':
  2778. temp_search_key = temp_search_key.replace("150ml", "")
  2779. elif self.search_key == '999糠酸莫米松乳膏10g支':
  2780. temp_search_key = temp_search_key.replace("10g支", "")
  2781. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  2782. temp_search_key = temp_search_key.replace("20g", "")
  2783. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  2784. temp_search_key = temp_search_key.replace("(无糖)6g", "")
  2785. elif self.search_key == '999壮骨关节丸6g*20':
  2786. temp_search_key = temp_search_key.replace("6g*20", "")
  2787. elif self.search_key == '999正天丸6g*15':
  2788. temp_search_key = temp_search_key.replace("6g*15", "")
  2789. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  2790. temp_search_key = temp_search_key.replace("20", "")
  2791. elif self.search_key == '999糠酸莫米松凝胶10':
  2792. temp_search_key = temp_search_key.replace("10", "")
  2793. elif self.search_key == '999板蓝根颗粒10g*20':
  2794. temp_search_key = temp_search_key.replace("10g*20", "")
  2795. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  2796. temp_search_key = temp_search_key.replace("10粒", "")
  2797. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  2798. temp_search_key = temp_search_key.replace("12粒", "")
  2799. elif self.search_key == '999咽炎片0.26g*12片*2板':
  2800. temp_search_key = temp_search_key.replace("0.26g*12片*2板", "")
  2801. elif self.search_key == '999小儿止咳糖浆120':
  2802. temp_search_key = temp_search_key.replace("120", "")
  2803. elif self.search_key == '999小儿止咳糖浆225':
  2804. temp_search_key = temp_search_key.replace("225", "")
  2805. elif self.search_key == '999小儿感冒颗粒6g*10':
  2806. temp_search_key = temp_search_key.replace("6g*10", "")
  2807. elif self.search_key == '999小儿感冒颗粒6g*24':
  2808. temp_search_key = temp_search_key.replace("6g*24", "")
  2809. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  2810. temp_search_key = temp_search_key.replace("6g*10袋", "")
  2811. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  2812. temp_search_key = temp_search_key.replace("6g*20袋", "")
  2813. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  2814. temp_search_key = temp_search_key.replace("8g*10袋", "")
  2815. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  2816. temp_search_key = temp_search_key.replace("2.5g*10袋", "")
  2817. elif self.search_key == '999感冒清热颗粒12g*18':
  2818. temp_search_key = temp_search_key.replace("12g*18", "")
  2819. if '999' not in product or temp_search_key not in product:
  2820. self.swipe_back(1)
  2821. self.unrelated_data += 1
  2822. return
  2823. else:
  2824. if self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  2825. temp_search_key = self.search_key.replace('史达功', '')
  2826. temp_search_key = temp_search_key.replace('120', '')
  2827. if '史达功' not in product or temp_search_key not in product:
  2828. self.swipe_back(1)
  2829. self.unrelated_data += 1
  2830. return
  2831. elif self.search_key == '三九胃泰养胃舒颗粒8袋':
  2832. temp_search_key = self.search_key.replace('三九胃泰', '')
  2833. temp_search_key = temp_search_key.replace('8袋', '')
  2834. if '三九胃泰' not in product or temp_search_key not in product:
  2835. self.swipe_back(1)
  2836. self.unrelated_data += 1
  2837. return
  2838. elif self.search_key == '今维多赐多康牌蛋白粉':
  2839. temp_search_key = self.search_key.replace('今维多', '')
  2840. if '今维多' not in product or temp_search_key not in product:
  2841. self.swipe_back(1)
  2842. self.unrelated_data += 1
  2843. return
  2844. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  2845. temp_search_key = self.search_key.replace('佳美舒', '')
  2846. temp_search_key = temp_search_key.replace('4', '')
  2847. if '佳美舒' not in product or temp_search_key not in product:
  2848. self.swipe_back(1)
  2849. self.unrelated_data += 1
  2850. return
  2851. elif self.search_key == '三九胃泰颗粒20g*10':
  2852. temp_search_key = self.search_key.replace('20g*10', '')
  2853. if temp_search_key not in product:
  2854. self.swipe_back(1)
  2855. self.unrelated_data += 1
  2856. return
  2857. elif self.search_key == '三九胃泰颗粒20g*6袋':
  2858. temp_search_key = self.search_key.replace('20g*6袋', '')
  2859. if temp_search_key not in product:
  2860. self.swipe_back(1)
  2861. self.unrelated_data += 1
  2862. return
  2863. elif self.search_key == '顺峰康王酮康他索乳膏':
  2864. temp_search_key = self.search_key.replace('顺峰康王', '')
  2865. if '顺峰康王' not in product or temp_search_key not in product:
  2866. self.swipe_back(1)
  2867. self.unrelated_data += 1
  2868. return
  2869. else:
  2870. if self.search_key not in product.replace(' ', ''):
  2871. self.swipe_back(1)
  2872. self.unrelated_data += 1
  2873. return
  2874. # if self.search_key not in product.replace(' ', ''):
  2875. # self.swipe_back(1)
  2876. # self.unrelated_data += 1
  2877. # return
  2878. else:
  2879. self.swipe_back(1)
  2880. return
  2881. min_price = self.drug_price() # 最低价格
  2882. # 商品链接
  2883. product_link = self.get_product_link()
  2884. #判断是否有自营的文本,有的话不需要获取店铺的信息
  2885. if self.d.xpath('//*[@text="自营"]').exists:
  2886. shop = "美团自营大药房(快递电商)"
  2887. # 爬取日期
  2888. scrape_date = self.get_current_date()
  2889. # scrape_date = "2025-07-18"
  2890. dup_data = {'product': product, 'min_price': min_price, 'shop': shop, 'scrape_date': scrape_date,
  2891. 'platform': '美团'}
  2892. print(f'当前数据:{dup_data}')
  2893. if self.data_is_exists(dup_data):
  2894. print('存在相同数据不入库')
  2895. self.swipe_back(1)
  2896. return
  2897. else:
  2898. for i in range(8):
  2899. if self.d.xpath('//*[@text="进店"]').exists:
  2900. print('开始获取店铺名1')
  2901. break
  2902. self.d.swipe_ext('up', 0.3)
  2903. time.sleep(1)
  2904. # detail_info = self.d.xpath(
  2905. # '//android.widget.ScrollView/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[6]').info
  2906. # bounds = detail_info['bounds']
  2907. # height = bounds['bottom'] - bounds['top']
  2908. # if self.d.xpath('//*[@text="进店"]').exists and height > 100:
  2909. if self.d.xpath('//*[@text="进店"]').exists:
  2910. print('开始获取店铺名2')
  2911. break
  2912. shop = self.get_shop_name()
  2913. # 爬取日期
  2914. scrape_date = self.get_current_date()
  2915. # scrape_date = "2025-07-18"
  2916. dup_data = {'product': product, 'min_price': min_price, 'shop': shop, 'scrape_date': scrape_date,
  2917. 'platform': '美团'}
  2918. print(f'当前数据:{dup_data}')
  2919. #获取店铺信息开始
  2920. #暂时不获取店铺信息 start
  2921. is_has_enter_shop = self.has_shop()
  2922. #需要判断shop是否已经在数据库中存在,如果存在,则不再进入店铺,直接进入下一个商品
  2923. shop_is_exists = self.shop_is_exists_database(shop)
  2924. #存在进店 并且店铺的名称不包含美团官方的字样
  2925. print(f"已采集{self.shop_data_num}家店铺数据")
  2926. if is_has_enter_shop and '美团官方' not in shop and '美团自营' not in shop and not shop_is_exists and self.shop_data_num < 500:
  2927. # license_info = self.get_license_info_ex()
  2928. license_info = self.safe_exec(self.get_license_info_ex)
  2929. contact_address = license_info['contact_address']
  2930. qualification_number = license_info['qualification_number']
  2931. business_license_company = license_info['business_license_company']
  2932. business_license_address = license_info['business_license_address']
  2933. save_shop_data = {
  2934. 'shop': shop,
  2935. 'contact_address': contact_address,
  2936. 'qualification_number': qualification_number,
  2937. 'scrape_date': scrape_date,
  2938. 'business_license_company':business_license_company,
  2939. 'business_license_address':business_license_address,
  2940. 'platform': '美团'
  2941. }
  2942. self.save_shop_info_to_database(save_shop_data)
  2943. self.shop_data_num += 1 # 店铺数据数量+1
  2944. self.swipe_back(2)
  2945. else:
  2946. print('不采集店铺信息')
  2947. #获取店铺信息结束
  2948. #暂时不获取店铺信息 end
  2949. if self.data_is_exists(dup_data):
  2950. print('存在相同数据不入库')
  2951. self.swipe_back(1)
  2952. return
  2953. if not shop:
  2954. print('未获取到店铺名:开始回退')
  2955. self.swipe_back(1)
  2956. return
  2957. if not shop or '自营' in shop:
  2958. self.swipe_back(1)
  2959. return
  2960. time.sleep(self.get_sleep_time())
  2961. # 生产日期为空
  2962. manufacture_date = ''
  2963. # 执政信息
  2964. # if is_has_enter_shop:
  2965. # license_info = self.get_license_info()
  2966. # business_license_company = license_info["单位名称"]
  2967. # credit_code = license_info['社会信用代码']
  2968. # city_str = license_info['地址']
  2969. # # 先把省份啥的替换掉
  2970. # city_sub_str = re.sub(r'[u4e00-\u9fa5]+省', '', city_str)
  2971. # try:
  2972. # city = re.search(r'[\u4e00-\u9fa5]+?(市|区|县)', city_sub_str).group(0)
  2973. # except:
  2974. # city = city_sub_str
  2975. # try:
  2976. # province = self.city2province[city]
  2977. # except:
  2978. # province = ''
  2979. # self.swipe_back(2)
  2980. # else:
  2981. # business_license_company = ''
  2982. # credit_code = ''
  2983. # city = ''
  2984. # province = ''
  2985. business_license_company = ''
  2986. credit_code = ''
  2987. city = ''
  2988. province = ''
  2989. expiry_date = ''
  2990. manufacturer = ''
  2991. approval_number = ''
  2992. #暂时不获取说明书信息 start
  2993. #是否存在说明书
  2994. # is_has_instructions = self.has_instructions()
  2995. #有的药品没有说明书,直接默认
  2996. if self.search_key == '今维多赐多康牌蛋白粉':
  2997. expiry_date = '18个月'
  2998. manufacturer = '华润圣海健康科技有限公司'
  2999. approval_number = '食健备G202437001992'
  3000. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  3001. expiry_date = '24个月'
  3002. manufacturer = '浙江华润三九众益制药有限公司'
  3003. approval_number = '国药准字H20090152'
  3004. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  3005. expiry_date = '3年'
  3006. manufacturer = '江苏萨瑞斯医疗科技有限公司'
  3007. approval_number = '苏械注准20212140025'
  3008. elif self.search_key == '999蒲地蓝消炎片':
  3009. expiry_date = '24个月'
  3010. manufacturer = '特一药业集团股份有限公司'
  3011. approval_number = '国药准字Z20063596'
  3012. elif self.search_key == '999养胃舒颗粒':
  3013. expiry_date = '36个月'
  3014. manufacturer = '合肥华润神鹿药业有限公司'
  3015. approval_number = '国药准字Z34020289'
  3016. elif self.search_key == '999糠酸莫米松凝胶15':
  3017. expiry_date = '36个月'
  3018. manufacturer = '华润三九(南昌)药业有限公司'
  3019. approval_number = '国药准字H20080010'
  3020. elif self.search_key == '999黄芪精':
  3021. expiry_date = '36个月'
  3022. manufacturer = '台州南峰药业有限公司'
  3023. approval_number = '国药准字Z33020783'
  3024. elif self.search_key == '999复方感冒灵颗粒':
  3025. expiry_date = '24个月'
  3026. manufacturer = '华润三九(郴州)制药有限公司'
  3027. approval_number = '国药准字Z43020334'
  3028. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  3029. expiry_date = '36个月'
  3030. manufacturer = '华润三九(南昌)药业有限公司'
  3031. approval_number = '国药准字H20074155'
  3032. elif self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  3033. expiry_date = '暂定24个月,具体有效期以实物说明书为准'
  3034. manufacturer = '史达德药业(北京)有限公司'
  3035. approval_number = '国药准字H11021837'
  3036. elif self.search_key == '999速复康布洛芬缓释胶囊':
  3037. expiry_date = '24个月'
  3038. manufacturer = '北京红林制药有限公司'
  3039. approval_number = '国药准字H20074172'
  3040. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  3041. expiry_date = '24个月'
  3042. manufacturer = '重庆科瑞东和制药有限责任公司'
  3043. approval_number = '国药准字Z50020420'
  3044. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  3045. expiry_date = '24个月'
  3046. manufacturer = '华润三九(南昌)药业有限公司'
  3047. approval_number = '国药准字H20073954'
  3048. elif self.search_key == '999维生素C咀嚼片':
  3049. expiry_date = '24个月'
  3050. manufacturer = '甘肃成纪生物药业有限公司'
  3051. approval_number = '国药准字H62021166'
  3052. elif self.search_key == '999强力枇杷露120ml':
  3053. expiry_date = '36个月'
  3054. manufacturer = '华润三九(南昌)药业有限公司'
  3055. approval_number = '国药准字Z36021533'
  3056. elif self.search_key == '999强力枇杷露150ml':
  3057. expiry_date = '36个月'
  3058. manufacturer = '华润三九(南昌)药业有限公司'
  3059. approval_number = '国药准字Z36021533'
  3060. elif self.search_key == '999抗病毒口服液10ml*10' or self.search_key == '999抗病毒口服液10ml*12':
  3061. expiry_date = '24个月'
  3062. manufacturer = '杭州华润老桐君药业有限公司'
  3063. approval_number = '国药准字Z33020518'
  3064. elif self.search_key == '999精氨酸布洛芬颗粒':
  3065. expiry_date = '暂定36个月'
  3066. manufacturer = '华润三九(唐山)药业有限公司'
  3067. approval_number = '国药准字H20070139'
  3068. elif self.search_key == '999糠酸莫米松乳膏10g支':
  3069. expiry_date = '36个月'
  3070. manufacturer = '华润三九(南昌)药业有限公司'
  3071. approval_number = '国药准字H20074090'
  3072. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  3073. expiry_date = '24个月'
  3074. manufacturer = '华润三九(南昌)药业有限公司'
  3075. approval_number = '国药准字H20074079'
  3076. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  3077. expiry_date = '36个月'
  3078. manufacturer = '合肥华润神鹿药业有限公司'
  3079. approval_number = '国药准字Z20055023'
  3080. elif self.search_key == '999银菊清咽颗粒':
  3081. expiry_date = '30个月'
  3082. manufacturer = '合肥华润神鹿药业有限公司'
  3083. approval_number = '国药准字Z20026680'
  3084. elif self.search_key == '999阿奇霉素片':
  3085. expiry_date = '48个月'
  3086. manufacturer = '浙江华润三九众益制药有限公司'
  3087. approval_number = '国药准字H20084458'
  3088. elif self.search_key == '999补脾益肠丸':
  3089. expiry_date = '24个月'
  3090. manufacturer = '惠州市九惠制药股份有限公司'
  3091. approval_number = '国药准字Z44023376'
  3092. elif self.search_key == '999壮骨关节丸6g*20':
  3093. expiry_date = '24个月'
  3094. manufacturer = '华润三九医药股份有限公司'
  3095. approval_number = '国药准字Z44023377'
  3096. elif self.search_key == '999壮骨关节胶囊':
  3097. expiry_date = '24个月'
  3098. manufacturer = '华润三九医药股份有限公司'
  3099. approval_number = '国药准字Z20080055'
  3100. elif self.search_key == '999正天丸6g*15':
  3101. expiry_date = '30个月'
  3102. manufacturer = '华润三九医药股份有限公司'
  3103. approval_number = '国药准字Z44020711'
  3104. elif self.search_key == '999正天胶囊':
  3105. expiry_date = '24个月'
  3106. manufacturer = '华润三九医药股份有限公司'
  3107. approval_number = '国药准字Z20010142'
  3108. elif self.search_key == '三九胃泰胶囊':
  3109. expiry_date = '24个月'
  3110. manufacturer = '华润三九医药股份有限公司'
  3111. approval_number = '国药准字Z44020704'
  3112. elif self.search_key == '三九胃泰颗粒20g*10':
  3113. expiry_date = '24个月'
  3114. manufacturer = '华润三九医药股份有限公司'
  3115. approval_number = '国药准字Z44020705'
  3116. elif self.search_key == '999感冒灵颗粒':
  3117. expiry_date = '24个月'
  3118. manufacturer = '华润三九(枣庄)药业有限公司'
  3119. approval_number = '国药准字Z44021940'
  3120. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  3121. expiry_date = '36个月'
  3122. manufacturer = '华润三九医药股份有限公司'
  3123. approval_number = '国药准字H44024170'
  3124. elif self.search_key == '三九胃泰颗粒20g*6袋':
  3125. expiry_date = '24个月'
  3126. manufacturer = '华润三九医药股份有限公司'
  3127. approval_number = '国药准字Z44020705'
  3128. elif self.search_key == '顺峰康王酮康他索乳膏':
  3129. expiry_date = '24个月'
  3130. manufacturer = '广东华润顺峰药业有限公司'
  3131. approval_number = '国药准字H10980204'
  3132. elif self.search_key == '999糠酸莫米松凝胶10':
  3133. expiry_date = '36个月'
  3134. manufacturer = '华润三九(南昌)药业有限公司'
  3135. approval_number = '国药准字H20080010'
  3136. elif self.search_key == '999板蓝根颗粒10g*20':
  3137. expiry_date = '36个月'
  3138. manufacturer = '广东恒诚制药股份有限公司'
  3139. approval_number = '国药准字Z44021520'
  3140. elif self.search_key == '999复方氨酚烷胺胶囊' or self.search_key == '999复方氨酚烷胺胶囊12粒' or self.search_key == '999复方氨酚烷胺胶囊10粒' or self.search_key == '999复方氨酚烷胺胶囊6粒' :
  3141. expiry_date = '36个月'
  3142. manufacturer = '华润三九(唐山)药业有限公司'
  3143. approval_number = '国药准字H13021912'
  3144. elif self.search_key == '999咽炎片0.26g*12片*2板':
  3145. expiry_date = '24个月'
  3146. manufacturer = '华润三九(黄石)药业有限公司'
  3147. approval_number = '国药准字Z42021062'
  3148. elif self.search_key == '999小儿止咳糖浆120' or self.search_key == '999小儿止咳糖浆225':
  3149. expiry_date = '24个月'
  3150. manufacturer = '华润三九(雅安)药业有限公司'
  3151. approval_number = '国药准字Z51020675'
  3152. elif self.search_key == '999小儿感冒颗粒6g*10' or self.search_key == '999小儿感冒颗粒6g*24':
  3153. expiry_date = '36个月'
  3154. manufacturer = '华润三九(枣庄)药业有限公司'
  3155. approval_number = '国药准字Z37021392'
  3156. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋' or self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  3157. expiry_date = '36个月'
  3158. manufacturer = '华润三九(黄石)药业有限公司'
  3159. approval_number = '国药准字H42022510'
  3160. elif self.search_key == '999感冒灵胶囊':
  3161. expiry_date = '24个月'
  3162. manufacturer = '华润三九医药股份有限公司'
  3163. approval_number = '国药准字Z44021939'
  3164. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  3165. expiry_date = '24个月'
  3166. manufacturer = '华润三九(黄石)药业有限公司'
  3167. approval_number = '国药准字Z42021105'
  3168. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  3169. expiry_date = '18个月'
  3170. manufacturer = '华润三九医药股份有限公司'
  3171. approval_number = '国药准字Z20100067'
  3172. elif self.search_key == '999感冒清热颗粒12g*18':
  3173. expiry_date = '36个月'
  3174. manufacturer = '山东新大陆制药有限公司'
  3175. approval_number = '国药准字Z37020066'
  3176. elif self.search_key == '999小柴胡颗粒':
  3177. expiry_date = '24个月'
  3178. manufacturer = '华润三九医药股份有限公司'
  3179. approval_number = '国药准字Z44020709'
  3180. else:
  3181. is_has_instructions = self.safe_exec(self.has_instructions)
  3182. # 说明书等信息
  3183. if is_has_instructions:
  3184. print('开始获取说明书信息')
  3185. # instructions_info = self.get_instructions_data()
  3186. instructions_info = self.safe_exec(self.get_instructions_data)
  3187. if instructions_info['有效期'] is not None:
  3188. expiry_date = instructions_info['有效期'].strip('。')
  3189. if instructions_info['生产单位'] is not None:
  3190. manufacturer = instructions_info['生产单位'].strip('。')
  3191. if instructions_info['批准文号'] is not None:
  3192. approval_number = instructions_info['批准文号'].strip('。')
  3193. else:
  3194. # 没有说明书不入库
  3195. print('没有获取到说明书信息')
  3196. self.swipe_back(1)
  3197. return
  3198. #暂时不获取说明书信息 end
  3199. self.unrelated_data = 0
  3200. # 爬取省份
  3201. scrape_province = '广东' # 这里先默认广东
  3202. # 是否有货
  3203. availability = ''
  3204. save_data = {
  3205. 'product': product,
  3206. 'min_price': min_price,
  3207. 'manufacture_date': manufacture_date,
  3208. 'expiry_date': expiry_date,
  3209. 'shop': shop,
  3210. 'business_license_company': business_license_company,
  3211. 'province': province,
  3212. 'city': city,
  3213. 'manufacturer': manufacturer,
  3214. 'specification': specifications,
  3215. 'approval_number': approval_number,
  3216. 'product_link': product_link,
  3217. 'scrape_date': scrape_date,
  3218. 'scrape_province': scrape_province,
  3219. 'availability': availability,
  3220. 'credit_code': credit_code,
  3221. 'platform': '美团',
  3222. 'search_key': self.search_key,
  3223. }
  3224. self.save_to_database(save_data)
  3225. # time.sleep(100000)
  3226. time.sleep(self.get_sleep_time())
  3227. if self.distinct_target():
  3228. print('已到达搜索列表页')
  3229. else:
  3230. for i in range(1):
  3231. print('在详情页')
  3232. self.swipe_back(1)
  3233. time.sleep(self.get_sleep_time())
  3234. # 最外部有个定位按钮
  3235. if self.distinct_target():
  3236. break
  3237. #主函数
  3238. #start_page:开始页,采集用
  3239. #end_page:结束页,采集用
  3240. #task_id:上报数据用
  3241. def main(self, device_id, start_page, end_page, task_id, retry_count=0):
  3242. task_start_time = time.time() #任务开始时间
  3243. task_scape_count = 0 #任务采集数量初始化为0
  3244. MAX_RETRY = 3 # 最大重试次数
  3245. spider_no = 0
  3246. self.connect_devices(device_id)
  3247. time.sleep(self.get_sleep_time())
  3248. self.d.toast.show("测试toast", 20)
  3249. # 启动全局弹窗监控
  3250. self.monitor = SpiderMonitor(self)
  3251. self.monitor.start()
  3252. try:
  3253. # 重新开启美团应用
  3254. self.restart_app()
  3255. # 搜索关键字
  3256. self.enter_target_page()
  3257. # print('开始滑动')
  3258. # self.d.drag(300, 1400, 300, 400, 1)
  3259. # time.sleep(100000)
  3260. for idx in range(300):
  3261. print(f'第{idx + 1}页')
  3262. if spider_no > 30:
  3263. time.sleep(60)
  3264. spider_no = 0
  3265. print('目前无关数据量: ', self.unrelated_data)
  3266. # 检查是否需要暂停(验证码过多)
  3267. if self.monitor.verification_count >= self.monitor.MAX_VERIFICATION_RETRY:
  3268. print("频繁遇到验证码,暂停程序")
  3269. self.d.toast("请处理验证码后点击继续", 30)
  3270. # 等待用户点击屏幕继续
  3271. self.d.click(0, 0) # 无效点击,等待用户操作
  3272. self.monitor.verification_count = 0
  3273. if self.unrelated_data > 20:
  3274. # 连续超过20个不达标的数据则停止采集
  3275. self.loggerMT.info(f"连续20个数据不达标,品规:{self.search_key}")
  3276. break
  3277. # 线程安全获取商品列表
  3278. # drug_lis = self.d.xpath('//android.support.v7.widget.RecyclerView/android.widget.FrameLayout').all()
  3279. # drug_lis = self.safe_list('//android.support.v7.widget.RecyclerView/android.widget.FrameLayout', self.monitor)
  3280. while True:
  3281. if self.d.xpath('//android.support.v7.widget.RecyclerView/android.widget.FrameLayout').exists:
  3282. break
  3283. time.sleep(1)
  3284. drug_lis = self.safe_exec(self.d.xpath('//android.support.v7.widget.RecyclerView/android.widget.FrameLayout').all)
  3285. lis_len = len(drug_lis)
  3286. print(f'当前页面共有{lis_len}个商品')
  3287. for idxx,drug_one in enumerate(drug_lis,start = 1):
  3288. bounds = drug_one.info['bounds']
  3289. top = bounds['top']
  3290. bottom = bounds['bottom']
  3291. # height = bottom - top
  3292. print(f'当前商品bottom:{bottom}')
  3293. print(f'当前商品top:{top}')
  3294. # if 304 <= top and bottom <= 1475: # 默认高度241的才行
  3295. if 304 <= top and bottom <= 1475: # 默认高度241的才行 1559
  3296. # print('目标-->', drug_one.info)
  3297. # drug_one.click()
  3298. #获取当前元素中的属性来判断是否要点击进入采集
  3299. print(f"这页的第几个商品:{idxx}")
  3300. product_title = ''
  3301. price = ''
  3302. shop_name = ''
  3303. #商品名称的xpath
  3304. 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'
  3305. 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'
  3306. if self.d.xpath(product_tittle_xpath).exists:
  3307. product_title = self.d.xpath(product_tittle_xpath).text
  3308. product_title = product_title[1:] if product_title.startswith('0') else product_title
  3309. print(f"product_tittle_xpath列表当前商品名称:{product_title}")
  3310. if '999' in self.search_key:
  3311. if self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  3312. temp_search_key = self.search_key.replace('999皮炎平', '')
  3313. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  3314. temp_search_key = self.search_key.replace('999必无忧', '')
  3315. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  3316. temp_search_key = self.search_key.replace('999必无忧', '')
  3317. elif self.search_key == '999速复康布洛芬缓释胶囊':
  3318. temp_search_key = self.search_key.replace('999速复康', '')
  3319. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  3320. temp_search_key = self.search_key.replace('999选平', '')
  3321. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  3322. temp_search_key = self.search_key.replace('999皮炎平', '')
  3323. else:
  3324. temp_search_key = self.search_key.replace('999', '')
  3325. if self.search_key == '999糠酸莫米松凝胶15':
  3326. temp_search_key = temp_search_key.replace('15', '')
  3327. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  3328. temp_search_key = temp_search_key.replace('30', '')
  3329. elif self.search_key == '999复方金银花颗粒10g':
  3330. temp_search_key = temp_search_key.replace('10g', '')
  3331. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  3332. temp_search_key = temp_search_key.replace('15g*15袋/盒', '')
  3333. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  3334. temp_search_key = temp_search_key.replace('6粒', '')
  3335. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  3336. temp_search_key = temp_search_key.replace('50', '')
  3337. elif self.search_key == '999止泻利颗粒15g*8':
  3338. temp_search_key = temp_search_key.replace('15g*8', '')
  3339. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  3340. temp_search_key = temp_search_key.replace('30', '')
  3341. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  3342. temp_search_key = temp_search_key.replace('15g', '')
  3343. elif self.search_key == '999复方苦参肠炎康片12片':
  3344. temp_search_key = temp_search_key.replace("12片", "")
  3345. elif self.search_key == '999强力枇杷露16袋':
  3346. temp_search_key = temp_search_key.replace("16袋", "")
  3347. elif self.search_key == '999三蛇胆川贝膏138':
  3348. temp_search_key = temp_search_key.replace("138", "")
  3349. elif self.search_key == '999抗病毒口服液10ml*12':
  3350. temp_search_key = temp_search_key.replace("10ml*12", "")
  3351. elif self.search_key == '999抗病毒口服液10ml*10':
  3352. temp_search_key = temp_search_key.replace("10ml*10", "")
  3353. elif self.search_key == '999强力枇杷露120ml':
  3354. temp_search_key = temp_search_key.replace("120ml", "")
  3355. elif self.search_key == '999强力枇杷露150ml':
  3356. temp_search_key = temp_search_key.replace("150ml", "")
  3357. elif self.search_key == '999糠酸莫米松乳膏10g支':
  3358. temp_search_key = temp_search_key.replace("10g支", "")
  3359. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  3360. temp_search_key = temp_search_key.replace("20g", "")
  3361. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  3362. temp_search_key = temp_search_key.replace("(无糖)6g", "")
  3363. elif self.search_key == '999壮骨关节丸6g*20':
  3364. temp_search_key = temp_search_key.replace("6g*20", "")
  3365. elif self.search_key == '999正天丸6g*15':
  3366. temp_search_key = temp_search_key.replace("6g*15", "")
  3367. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  3368. temp_search_key = temp_search_key.replace("20", "")
  3369. elif self.search_key == '999糠酸莫米松凝胶10':
  3370. temp_search_key = temp_search_key.replace("10", "")
  3371. elif self.search_key == '999板蓝根颗粒10g*20':
  3372. temp_search_key = temp_search_key.replace("10g*20", "")
  3373. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  3374. temp_search_key = temp_search_key.replace("10粒", "")
  3375. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  3376. temp_search_key = temp_search_key.replace("12粒", "")
  3377. elif self.search_key == '999咽炎片0.26g*12片*2板':
  3378. temp_search_key = temp_search_key.replace("0.26g*12片*2板", "")
  3379. elif self.search_key == '999小儿止咳糖浆120':
  3380. temp_search_key = temp_search_key.replace("120", "")
  3381. elif self.search_key == '999小儿止咳糖浆225':
  3382. temp_search_key = temp_search_key.replace("225", "")
  3383. elif self.search_key == '999小儿感冒颗粒6g*10':
  3384. temp_search_key = temp_search_key.replace("6g*10", "")
  3385. elif self.search_key == '999小儿感冒颗粒6g*24':
  3386. temp_search_key = temp_search_key.replace("6g*24", "")
  3387. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  3388. temp_search_key = temp_search_key.replace("6g*10袋", "")
  3389. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  3390. temp_search_key = temp_search_key.replace("6g*20袋", "")
  3391. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  3392. temp_search_key = temp_search_key.replace("8g*10袋", "")
  3393. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  3394. temp_search_key = temp_search_key.replace("2.5g*10袋", "")
  3395. elif self.search_key == '999感冒清热颗粒12g*18':
  3396. temp_search_key = temp_search_key.replace("12g*18", "")
  3397. if self.search_key == '999抗病毒口服液':
  3398. if '999' not in product_title or temp_search_key not in product_title :
  3399. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3400. continue
  3401. elif '10ml*12' not in product_title and '10ml*10' not in product_title:
  3402. print(f"当前商品名称:{product_title} 不包含10ml*12和10ml*10品规")
  3403. continue
  3404. elif self.search_key == '999抗病毒口服液10ml*12':
  3405. if '999' not in product_title or temp_search_key not in product_title :
  3406. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3407. continue
  3408. elif '10ml*12' not in product_title:
  3409. print(f"当前商品名称:{product_title} 不包含10ml*12品规")
  3410. continue
  3411. elif self.search_key == '999抗病毒口服液10ml*10':
  3412. if '999' not in product_title or temp_search_key not in product_title :
  3413. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3414. continue
  3415. elif '10ml*10' not in product_title:
  3416. print(f"当前商品名称:{product_title} 不包含10ml*10品规")
  3417. continue
  3418. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  3419. if '999' not in product_title or temp_search_key not in product_title :
  3420. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3421. continue
  3422. elif '30' not in product_title:
  3423. print(f"当前商品名称:{product_title} 不包含30品规")
  3424. continue
  3425. elif self.search_key == '999复方感冒灵颗粒':
  3426. if '999' not in product_title or temp_search_key not in product_title :
  3427. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3428. continue
  3429. elif '14g*15' not in product_title and '14g*9' not in product_title:
  3430. print(f"当前商品名称:{product_title} 不包含14g*15 和 14g*9品规")
  3431. continue
  3432. elif self.search_key == '999养胃舒颗粒':
  3433. if '999' not in product_title or temp_search_key not in product_title :
  3434. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3435. continue
  3436. elif '10g*6' not in product_title:
  3437. print(f"当前商品名称:{product_title} 不包含10g*6品规")
  3438. continue
  3439. elif self.search_key == '999糠酸莫米松凝胶15':
  3440. if '999' not in product_title or temp_search_key not in product_title :
  3441. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3442. continue
  3443. elif '15' not in product_title:
  3444. print(f"当前商品名称:{product_title} 不包含15品规")
  3445. continue
  3446. elif self.search_key == '999复方金银花颗粒10g':
  3447. if '999' not in product_title or temp_search_key not in product_title :
  3448. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3449. continue
  3450. elif '10g' not in product_title:
  3451. print(f"当前商品名称:{product_title} 不包含10g品规")
  3452. continue
  3453. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  3454. if '999' not in product_title or temp_search_key not in product_title :
  3455. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3456. continue
  3457. elif '15g*15' not in product_title:
  3458. print(f"当前商品名称:{product_title} 不包含15g*15品规")
  3459. continue
  3460. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  3461. if '999' not in product_title or temp_search_key not in product_title :
  3462. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3463. continue
  3464. elif '6' not in product_title:
  3465. print(f"当前商品名称:{product_title} 不包含6品规")
  3466. continue
  3467. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  3468. if '999' not in product_title or temp_search_key not in product_title :
  3469. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3470. continue
  3471. elif '50' not in product_title:
  3472. print(f"当前商品名称:{product_title} 不包含50品规")
  3473. continue
  3474. elif self.search_key == '999止泻利颗粒15g*8':
  3475. if '999' not in product_title or temp_search_key not in product_title :
  3476. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3477. continue
  3478. elif '15g*8' not in product_title:
  3479. print(f"当前商品名称:{product_title} 不包含15g*8品规")
  3480. continue
  3481. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  3482. if '999' not in product_title or temp_search_key not in product_title :
  3483. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3484. continue
  3485. elif '30' not in product_title:
  3486. print(f"当前商品名称:{product_title} 不包含30品规")
  3487. continue
  3488. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  3489. if '999' not in product_title or temp_search_key not in product_title :
  3490. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3491. continue
  3492. elif '15' not in product_title:
  3493. print(f"当前商品名称:{product_title} 不包含15品规")
  3494. continue
  3495. elif self.search_key == '999复方苦参肠炎康片12片':
  3496. if '999' not in product_title or temp_search_key not in product_title :
  3497. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3498. continue
  3499. elif '12' not in product_title:
  3500. print(f"当前商品名称:{product_title} 不包含12品规")
  3501. continue
  3502. elif self.search_key == '999强力枇杷露16袋':
  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 '16' not in product_title:
  3507. print(f"当前商品名称:{product_title} 不包含16品规")
  3508. continue
  3509. elif self.search_key == '999三蛇胆川贝膏138':
  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 '138' not in product_title:
  3514. print(f"当前商品名称:{product_title} 不包含16品规")
  3515. continue
  3516. elif self.search_key == '999速复康布洛芬缓释胶囊':
  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 self.search_key == '999维生素C咀嚼片':
  3521. if '999' not in product_title or temp_search_key not in product_title :
  3522. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3523. continue
  3524. elif '80' not in product_title:
  3525. print(f"当前商品名称:{product_title} 不包含80品规")
  3526. elif self.search_key == '999精氨酸布洛芬颗粒':
  3527. if '999' not in product_title or temp_search_key not in product_title :
  3528. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3529. continue
  3530. elif '9' not in product_title:
  3531. print(f"当前商品名称:{product_title} 不包含9品规")
  3532. continue
  3533. elif self.search_key == '999强力枇杷露120ml':
  3534. if '999' not in product_title or temp_search_key not in product_title :
  3535. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3536. continue
  3537. elif '120' not in product_title:
  3538. print(f"当前商品名称:{product_title} 不包含120品规")
  3539. continue
  3540. elif self.search_key == '999强力枇杷露150ml':
  3541. if '999' not in product_title or temp_search_key not in product_title :
  3542. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3543. continue
  3544. elif '150' not in product_title:
  3545. print(f"当前商品名称:{product_title} 不包含120品规")
  3546. continue
  3547. elif self.search_key == '999糠酸莫米松乳膏10g支':
  3548. if '999' not in product_title or temp_search_key not in product_title :
  3549. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3550. continue
  3551. elif '10' not in product_title:
  3552. print(f"当前商品名称:{product_title} 不包含10品规")
  3553. continue
  3554. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  3555. if '999' not in product_title or temp_search_key not in product_title :
  3556. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3557. continue
  3558. elif '20' not in product_title:
  3559. print(f"当前商品名称:{product_title} 不包含20品规")
  3560. continue
  3561. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  3562. if '999' not in product_title or temp_search_key not in product_title :
  3563. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3564. continue
  3565. elif '6' not in product_title:
  3566. print(f"当前商品名称:{product_title} 不包含6品规")
  3567. continue
  3568. elif self.search_key == '999阿奇霉素片':
  3569. if '999' not in product_title or temp_search_key not in product_title :
  3570. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3571. continue
  3572. elif '0.25g*6' not in product_title:
  3573. print(f"当前商品名称:{product_title} 不包含0.25g*6品规")
  3574. continue
  3575. elif self.search_key == '999补脾益肠丸':
  3576. if '999' not in product_title or temp_search_key not in product_title :
  3577. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3578. continue
  3579. elif '6g*15' not in product_title:
  3580. print(f"当前商品名称:{product_title} 不包含6g*15品规")
  3581. continue
  3582. elif self.search_key == '999壮骨关节丸6g*20':
  3583. if '999' not in product_title or temp_search_key not in product_title :
  3584. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3585. continue
  3586. elif '6g*20' not in product_title:
  3587. print(f"当前商品名称:{product_title} 不包含6g*20品规")
  3588. continue
  3589. elif self.search_key == '999正天丸6g*15':
  3590. if '999' not in product_title or temp_search_key not in product_title :
  3591. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3592. continue
  3593. elif '6g*15' not in product_title:
  3594. print(f"当前商品名称:{product_title} 不包含6g*15品规")
  3595. continue
  3596. elif self.search_key == '999感冒灵颗粒':
  3597. if '999' not in product_title or temp_search_key not in product_title :
  3598. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3599. continue
  3600. elif '10g*9' not in product_title:
  3601. print(f"当前商品名称:{product_title} 不包含10g*9品规")
  3602. continue
  3603. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  3604. if '999' not in product_title or temp_search_key not in product_title :
  3605. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3606. continue
  3607. elif '20' not in product_title:
  3608. print(f"当前商品名称:{product_title} 不包含20品规")
  3609. continue
  3610. elif self.search_key == '999糠酸莫米松凝胶10':
  3611. if '999' not in product_title or temp_search_key not in product_title :
  3612. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3613. continue
  3614. elif '10' not in product_title:
  3615. print(f"当前商品名称:{product_title} 不包含10品规")
  3616. continue
  3617. elif self.search_key == '999板蓝根颗粒10g*20':
  3618. if '999' not in product_title or temp_search_key not in product_title :
  3619. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3620. continue
  3621. elif '10g*20' not in product_title:
  3622. print(f"当前商品名称:{product_title} 不包含10g*20品规")
  3623. continue
  3624. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  3625. if '999' not in product_title or temp_search_key not in product_title :
  3626. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3627. continue
  3628. elif '10' not in product_title:
  3629. print(f"当前商品名称:{product_title} 不包含10品规")
  3630. continue
  3631. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  3632. if '999' not in product_title or temp_search_key not in product_title :
  3633. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3634. continue
  3635. elif '12' not in product_title:
  3636. print(f"当前商品名称:{product_title} 不包含12品规")
  3637. continue
  3638. elif self.search_key == '999复方氨酚烷胺胶囊':
  3639. if '999' not in product_title or temp_search_key not in product_title :
  3640. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3641. continue
  3642. elif self.search_key == '999咽炎片0.26g*12片*2板':
  3643. if '999' not in product_title or temp_search_key not in product_title :
  3644. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3645. continue
  3646. elif '0.26g*12' not in product_title:
  3647. print(f"当前商品名称:{product_title} 不包含0.26g*12品规")
  3648. continue
  3649. elif self.search_key == '999小儿止咳糖浆120':
  3650. if '999' not in product_title or temp_search_key not in product_title :
  3651. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3652. continue
  3653. elif '120' not in product_title:
  3654. print(f"当前商品名称:{product_title} 不包含120品规")
  3655. continue
  3656. elif self.search_key == '999小儿止咳糖浆225':
  3657. if '999' not in product_title or temp_search_key not in product_title :
  3658. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3659. continue
  3660. elif '225' not in product_title:
  3661. print(f"当前商品名称:{product_title} 不包含225品规")
  3662. continue
  3663. elif self.search_key == '999小儿感冒颗粒6g*10':
  3664. if '999' not in product_title or temp_search_key not in product_title :
  3665. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3666. continue
  3667. elif '6g*10' not in product_title:
  3668. print(f"当前商品名称:{product_title} 不包含6g*10品规")
  3669. continue
  3670. elif self.search_key == '999小儿感冒颗粒6g*24':
  3671. if '999' not in product_title or temp_search_key not in product_title :
  3672. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3673. continue
  3674. elif '6g*24' not in product_title:
  3675. print(f"当前商品名称:{product_title} 不包含6g*24品规")
  3676. continue
  3677. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  3678. if '999' not in product_title or temp_search_key not in product_title :
  3679. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3680. continue
  3681. elif '6g*10' not in product_title:
  3682. print(f"当前商品名称:{product_title} 不包含6g*10品规")
  3683. continue
  3684. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  3685. if '999' not in product_title or temp_search_key not in product_title :
  3686. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3687. continue
  3688. elif '6g*20' not in product_title:
  3689. print(f"当前商品名称:{product_title} 不包含6g*20品规")
  3690. continue
  3691. elif self.search_key == '999感冒灵胶囊':
  3692. if '999' not in product_title or temp_search_key not in product_title :
  3693. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3694. continue
  3695. elif '0.5g*12' not in product_title:
  3696. print(f"当前商品名称:{product_title} 不包含0.5g*12品规")
  3697. continue
  3698. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  3699. if '999' not in product_title or temp_search_key not in product_title :
  3700. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3701. continue
  3702. elif '8g*10' not in product_title:
  3703. print(f"当前商品名称:{product_title} 不包含8g*10品规")
  3704. continue
  3705. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  3706. if '999' not in product_title or temp_search_key not in product_title :
  3707. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3708. continue
  3709. elif '2.5g*10' not in product_title:
  3710. print(f"当前商品名称:{product_title} 不包含2.5g*10品规")
  3711. continue
  3712. elif self.search_key == '999感冒清热颗粒12g*18':
  3713. if '999' not in product_title or temp_search_key not in product_title :
  3714. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3715. continue
  3716. elif '12g*18' not in product_title:
  3717. print(f"当前商品名称:{product_title} 不包含12g*18品规")
  3718. continue
  3719. elif self.search_key == '999藿香正气合剂':
  3720. if '999' not in product_title or temp_search_key not in product_title :
  3721. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3722. continue
  3723. elif '10ml*6' not in product_title and '10ml*10' not in product_title:
  3724. print(f"当前商品名称:{product_title} 不包含10ml*6 和 10ml*10品规")
  3725. continue
  3726. else:
  3727. if '999' not in product_title or temp_search_key not in product_title:
  3728. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3729. continue
  3730. else:
  3731. if self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  3732. temp_search_key = self.search_key.replace('史达功', '')
  3733. temp_search_key = temp_search_key.replace('120', '')
  3734. if '史达功' not in product_title or temp_search_key not in product_title :
  3735. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3736. continue
  3737. elif '120' not in product_title:
  3738. print(f"当前商品名称:{product_title} 不包含120品规")
  3739. continue
  3740. elif self.search_key == '三九胃泰养胃舒颗粒8袋':
  3741. temp_search_key = self.search_key.replace('三九胃泰', '')
  3742. temp_search_key = temp_search_key.replace('8袋', '')
  3743. if '三九胃泰' not in product_title or temp_search_key not in product_title :
  3744. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3745. continue
  3746. elif '8' not in product_title:
  3747. print(f"当前商品名称:{product_title} 不包含8品规")
  3748. continue
  3749. elif self.search_key == '今维多赐多康牌蛋白粉':
  3750. temp_search_key = self.search_key.replace('今维多', '')
  3751. if '今维多' not in product_title or temp_search_key not in product_title :
  3752. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3753. continue
  3754. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  3755. temp_search_key = self.search_key.replace('佳美舒', '')
  3756. temp_search_key = temp_search_key.replace('4', '')
  3757. if '佳美舒' not in product_title or temp_search_key not in product_title :
  3758. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3759. continue
  3760. elif '4' not in product_title and '8' not in product_title:
  3761. print(f"当前商品名称:{product_title} 不包含4和或8品规")
  3762. continue
  3763. elif self.search_key == '三九胃泰颗粒20g*10':
  3764. temp_search_key = self.search_key.replace('20g*10', '')
  3765. if temp_search_key not in product_title :
  3766. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3767. continue
  3768. elif '20g*10' not in product_title:
  3769. print(f"当前商品名称:{product_title} 不包含20g*10品规")
  3770. continue
  3771. elif self.search_key == '三九胃泰颗粒20g*6袋':
  3772. temp_search_key = self.search_key.replace('20g*6袋', '')
  3773. if temp_search_key not in product_title :
  3774. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3775. continue
  3776. elif '20g*6' not in product_title:
  3777. print(f"当前商品名称:{product_title} 不包含20g*6品规")
  3778. continue
  3779. elif self.search_key == '顺峰康王酮康他索乳膏':
  3780. temp_search_key = self.search_key.replace('顺峰康王', '')
  3781. if '顺峰康王' not in product_title or temp_search_key not in product_title :
  3782. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3783. continue
  3784. else:
  3785. if self.search_key not in product_title.replace(' ', ''):
  3786. continue
  3787. elif self.d.xpath(product_tittle_xpath2).exists:
  3788. product_title = self.d.xpath(product_tittle_xpath2).text
  3789. product_title = product_title[1:] if product_title.startswith('0') else product_title
  3790. print(f"product_tittle_xpath2列表当前商品名称:{product_title}")
  3791. if '999' in self.search_key:
  3792. if self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  3793. temp_search_key = self.search_key.replace('999皮炎平', '')
  3794. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  3795. temp_search_key = self.search_key.replace('999必无忧', '')
  3796. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  3797. temp_search_key = self.search_key.replace('999必无忧', '')
  3798. elif self.search_key == '999速复康布洛芬缓释胶囊':
  3799. temp_search_key = self.search_key.replace('999速复康', '')
  3800. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  3801. temp_search_key = self.search_key.replace('999选平', '')
  3802. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  3803. temp_search_key = self.search_key.replace('999皮炎平', '')
  3804. else:
  3805. temp_search_key = self.search_key.replace('999', '')
  3806. if self.search_key == '999糠酸莫米松凝胶15':
  3807. temp_search_key = temp_search_key.replace('15', '')
  3808. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  3809. temp_search_key = temp_search_key.replace('30', '')
  3810. elif self.search_key == '999复方金银花颗粒10g':
  3811. temp_search_key = temp_search_key.replace('10g', '')
  3812. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  3813. temp_search_key = temp_search_key.replace('15g*15袋/盒', '')
  3814. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  3815. temp_search_key = temp_search_key.replace('6粒', '')
  3816. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  3817. temp_search_key = temp_search_key.replace('50', '')
  3818. elif self.search_key == '999止泻利颗粒15g*8':
  3819. temp_search_key = temp_search_key.replace('15g*8', '')
  3820. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  3821. temp_search_key = temp_search_key.replace('30', '')
  3822. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  3823. temp_search_key = temp_search_key.replace('15g', '')
  3824. elif self.search_key == '999复方苦参肠炎康片12片':
  3825. temp_search_key = temp_search_key.replace("12片", "")
  3826. elif self.search_key == '999强力枇杷露16袋':
  3827. temp_search_key = temp_search_key.replace("16袋", "")
  3828. elif self.search_key == '999三蛇胆川贝膏138':
  3829. temp_search_key = temp_search_key.replace("138", "")
  3830. elif self.search_key == '999抗病毒口服液10ml*12':
  3831. temp_search_key = temp_search_key.replace("10ml*12", "")
  3832. elif self.search_key == '999抗病毒口服液10ml*10':
  3833. temp_search_key = temp_search_key.replace("10ml*10", "")
  3834. elif self.search_key == '999强力枇杷露120ml':
  3835. temp_search_key = temp_search_key.replace("120ml", "")
  3836. elif self.search_key == '999强力枇杷露150ml':
  3837. temp_search_key = temp_search_key.replace("150ml", "")
  3838. elif self.search_key == '999糠酸莫米松乳膏10g支':
  3839. temp_search_key = temp_search_key.replace("10g支", "")
  3840. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  3841. temp_search_key = temp_search_key.replace("20g", "")
  3842. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  3843. temp_search_key = temp_search_key.replace("(无糖)6g", "")
  3844. elif self.search_key == '999壮骨关节丸6g*20':
  3845. temp_search_key = temp_search_key.replace("6g*20", "")
  3846. elif self.search_key == '999正天丸6g*15':
  3847. temp_search_key = temp_search_key.replace("6g*15", "")
  3848. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  3849. temp_search_key = temp_search_key.replace("20", "")
  3850. elif self.search_key == '999糠酸莫米松凝胶10':
  3851. temp_search_key = temp_search_key.replace("10", "")
  3852. elif self.search_key == '999板蓝根颗粒10g*20':
  3853. temp_search_key = temp_search_key.replace("10g*20", "")
  3854. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  3855. temp_search_key = temp_search_key.replace("10粒", "")
  3856. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  3857. temp_search_key = temp_search_key.replace("12粒", "")
  3858. elif self.search_key == '999咽炎片0.26g*12片*2板':
  3859. temp_search_key = temp_search_key.replace("0.26g*12片*2板", "")
  3860. elif self.search_key == '999小儿止咳糖浆120':
  3861. temp_search_key = temp_search_key.replace("120", "")
  3862. elif self.search_key == '999小儿止咳糖浆225':
  3863. temp_search_key = temp_search_key.replace("225", "")
  3864. elif self.search_key == '999小儿感冒颗粒6g*10':
  3865. temp_search_key = temp_search_key.replace("6g*10", "")
  3866. elif self.search_key == '999小儿感冒颗粒6g*24':
  3867. temp_search_key = temp_search_key.replace("6g*24", "")
  3868. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  3869. temp_search_key = temp_search_key.replace("6g*10袋", "")
  3870. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  3871. temp_search_key = temp_search_key.replace("6g*20袋", "")
  3872. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  3873. temp_search_key = temp_search_key.replace("8g*10袋", "")
  3874. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  3875. temp_search_key = temp_search_key.replace("2.5g*10袋", "")
  3876. elif self.search_key == '999感冒清热颗粒12g*18':
  3877. temp_search_key = temp_search_key.replace("12g*18", "")
  3878. if self.search_key == '999抗病毒口服液':
  3879. if '999' not in product_title or temp_search_key not in product_title :
  3880. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3881. continue
  3882. elif '10ml*12' not in product_title and '10ml*10' not in product_title:
  3883. print(f"当前商品名称:{product_title} 不包含10ml*12和10ml*10品规")
  3884. continue
  3885. elif self.search_key == '999抗病毒口服液10ml*12':
  3886. if '999' not in product_title or temp_search_key not in product_title :
  3887. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3888. continue
  3889. elif '10ml*12' not in product_title:
  3890. print(f"当前商品名称:{product_title} 不包含10ml*12品规")
  3891. continue
  3892. elif self.search_key == '999抗病毒口服液10ml*10':
  3893. if '999' not in product_title or temp_search_key not in product_title :
  3894. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  3895. continue
  3896. elif '10ml*10' not in product_title:
  3897. print(f"当前商品名称:{product_title} 不包含10ml*10品规")
  3898. continue
  3899. elif self.search_key == '999皮炎平曲安奈德益康唑乳膏30':
  3900. if '999' not in product_title or temp_search_key not in product_title :
  3901. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3902. continue
  3903. elif '30' not in product_title:
  3904. print(f"当前商品名称:{product_title} 不包含30品规")
  3905. continue
  3906. elif self.search_key == '999复方感冒灵颗粒':
  3907. if '999' not in product_title or temp_search_key not in product_title :
  3908. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3909. continue
  3910. elif '14g*15' not in product_title and '14g*9' not in product_title:
  3911. print(f"当前商品名称:{product_title} 不包含14g*15 和 14g*9品规")
  3912. continue
  3913. elif self.search_key == '999养胃舒颗粒':
  3914. if '999' not in product_title or temp_search_key not in product_title :
  3915. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3916. continue
  3917. elif '10g*6' not in product_title:
  3918. print(f"当前商品名称:{product_title} 不包含10g*6品规")
  3919. continue
  3920. elif self.search_key == '999糠酸莫米松凝胶15':
  3921. if '999' not in product_title or temp_search_key not in product_title :
  3922. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3923. continue
  3924. elif '15' not in product_title:
  3925. print(f"当前商品名称:{product_title} 不包含15品规")
  3926. continue
  3927. elif self.search_key == '999复方金银花颗粒10g':
  3928. if '999' not in product_title or temp_search_key not in product_title :
  3929. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3930. continue
  3931. elif '10g' not in product_title:
  3932. print(f"当前商品名称:{product_title} 不包含10g品规")
  3933. continue
  3934. elif self.search_key == '999复方板蓝根颗粒15g*15袋/盒':
  3935. if '999' not in product_title or temp_search_key not in product_title :
  3936. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3937. continue
  3938. elif '15g*15' not in product_title:
  3939. print(f"当前商品名称:{product_title} 不包含15g*15品规")
  3940. continue
  3941. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  3942. if '999' not in product_title or temp_search_key not in product_title :
  3943. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3944. continue
  3945. elif '6' not in product_title:
  3946. print(f"当前商品名称:{product_title} 不包含6品规")
  3947. continue
  3948. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  3949. if '999' not in product_title or temp_search_key not in product_title :
  3950. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3951. continue
  3952. elif '50' not in product_title:
  3953. print(f"当前商品名称:{product_title} 不包含50品规")
  3954. continue
  3955. elif self.search_key == '999止泻利颗粒15g*8':
  3956. if '999' not in product_title or temp_search_key not in product_title :
  3957. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3958. continue
  3959. elif '15g*8' not in product_title:
  3960. print(f"当前商品名称:{product_title} 不包含15g*8品规")
  3961. continue
  3962. elif self.search_key == '999必无忧盐酸特比萘芬喷雾剂30':
  3963. if '999' not in product_title or temp_search_key not in product_title :
  3964. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3965. continue
  3966. elif '30' not in product_title:
  3967. print(f"当前商品名称:{product_title} 不包含30品规")
  3968. continue
  3969. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15g':
  3970. if '999' not in product_title or temp_search_key not in product_title :
  3971. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3972. continue
  3973. elif '15' not in product_title:
  3974. print(f"当前商品名称:{product_title} 不包含15品规")
  3975. continue
  3976. elif self.search_key == '999复方苦参肠炎康片12片':
  3977. if '999' not in product_title or temp_search_key not in product_title :
  3978. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3979. continue
  3980. elif '12' not in product_title:
  3981. print(f"当前商品名称:{product_title} 不包含12品规")
  3982. continue
  3983. elif self.search_key == '999强力枇杷露16袋':
  3984. if '999' not in product_title or temp_search_key not in product_title :
  3985. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3986. continue
  3987. elif '16' not in product_title:
  3988. print(f"当前商品名称:{product_title} 不包含16品规")
  3989. continue
  3990. elif self.search_key == '999三蛇胆川贝膏138':
  3991. if '999' not in product_title or temp_search_key not in product_title :
  3992. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3993. continue
  3994. elif '138' not in product_title:
  3995. print(f"当前商品名称:{product_title} 不包含138品规")
  3996. elif self.search_key == '999速复康布洛芬缓释胶囊':
  3997. if '999' not in product_title or temp_search_key not in product_title :
  3998. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  3999. continue
  4000. elif self.search_key == '999维生素C咀嚼片':
  4001. if '999' not in product_title or temp_search_key not in product_title :
  4002. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4003. continue
  4004. elif '80' not in product_title:
  4005. print(f"当前商品名称:{product_title} 不包含80品规")
  4006. continue
  4007. elif self.search_key == '999精氨酸布洛芬颗粒':
  4008. if '999' not in product_title or temp_search_key not in product_title :
  4009. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4010. continue
  4011. elif '9' not in product_title:
  4012. print(f"当前商品名称:{product_title} 不包含9品规")
  4013. continue
  4014. elif self.search_key == '999强力枇杷露120ml':
  4015. if '999' not in product_title or temp_search_key not in product_title :
  4016. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4017. continue
  4018. elif '120' not in product_title:
  4019. print(f"当前商品名称:{product_title} 不包含120品规")
  4020. continue
  4021. elif self.search_key == '999强力枇杷露150ml':
  4022. if '999' not in product_title or temp_search_key not in product_title :
  4023. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4024. continue
  4025. elif '150' not in product_title:
  4026. print(f"当前商品名称:{product_title} 不包含150品规")
  4027. continue
  4028. elif self.search_key == '999糠酸莫米松乳膏10g支':
  4029. if '999' not in product_title or temp_search_key not in product_title :
  4030. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4031. continue
  4032. elif '10' not in product_title:
  4033. print(f"当前商品名称:{product_title} 不包含10品规")
  4034. continue
  4035. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  4036. if '999' not in product_title or temp_search_key not in product_title :
  4037. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4038. continue
  4039. elif '20' not in product_title:
  4040. print(f"当前商品名称:{product_title} 不包含20品规")
  4041. continue
  4042. elif self.search_key == '999感冒清热颗粒(无糖)6g':
  4043. if '999' not in product_title or temp_search_key not in product_title :
  4044. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4045. continue
  4046. elif '6' not in product_title:
  4047. print(f"当前商品名称:{product_title} 不包含6品规")
  4048. continue
  4049. elif self.search_key == '999阿奇霉素片':
  4050. if '999' not in product_title or temp_search_key not in product_title :
  4051. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4052. continue
  4053. elif '0.25g*6' not in product_title:
  4054. print(f"当前商品名称:{product_title} 不包含0.25g*6品规")
  4055. continue
  4056. elif self.search_key == '999补脾益肠丸':
  4057. if '999' not in product_title or temp_search_key not in product_title :
  4058. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4059. continue
  4060. elif '6g*15' not in product_title:
  4061. print(f"当前商品名称:{product_title} 不包含6g*15品规")
  4062. continue
  4063. elif self.search_key == '999壮骨关节丸6g*20':
  4064. if '999' not in product_title or temp_search_key not in product_title :
  4065. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4066. continue
  4067. elif '6g*20' not in product_title:
  4068. print(f"当前商品名称:{product_title} 不包含6g*20品规")
  4069. continue
  4070. elif self.search_key == '999正天丸6g*15':
  4071. if '999' not in product_title or temp_search_key not in product_title :
  4072. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4073. continue
  4074. elif '6g*15' not in product_title:
  4075. print(f"当前商品名称:{product_title} 不包含6g*15品规")
  4076. continue
  4077. elif self.search_key == '999感冒灵颗粒':
  4078. if '999' not in product_title or temp_search_key not in product_title :
  4079. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4080. continue
  4081. elif '10g*9' not in product_title:
  4082. print(f"当前商品名称:{product_title} 不包含10g*9品规")
  4083. continue
  4084. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  4085. if '999' not in product_title or temp_search_key not in product_title :
  4086. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4087. continue
  4088. elif '20' not in product_title:
  4089. print(f"当前商品名称:{product_title} 不包含20品规")
  4090. continue
  4091. elif self.search_key == '999糠酸莫米松凝胶10':
  4092. if '999' not in product_title or temp_search_key not in product_title :
  4093. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4094. continue
  4095. elif '10' not in product_title:
  4096. print(f"当前商品名称:{product_title} 不包含10品规")
  4097. continue
  4098. elif self.search_key == '999板蓝根颗粒10g*20':
  4099. if '999' not in product_title or temp_search_key not in product_title :
  4100. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4101. continue
  4102. elif '10g*20' not in product_title:
  4103. print(f"当前商品名称:{product_title} 不包含10g*20品规")
  4104. continue
  4105. elif self.search_key == '999复方氨酚烷胺胶囊10粒':
  4106. if '999' not in product_title or temp_search_key not in product_title :
  4107. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4108. continue
  4109. elif '10' not in product_title:
  4110. print(f"当前商品名称:{product_title} 不包含10品规")
  4111. continue
  4112. elif self.search_key == '999复方氨酚烷胺胶囊12粒':
  4113. if '999' not in product_title or temp_search_key not in product_title :
  4114. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4115. continue
  4116. elif '12' not in product_title:
  4117. print(f"当前商品名称:{product_title} 不包含12品规")
  4118. continue
  4119. elif self.search_key == '999复方氨酚烷胺胶囊':
  4120. if '999' not in product_title or temp_search_key not in product_title :
  4121. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4122. continue
  4123. elif self.search_key == '999咽炎片0.26g*12片*2板':
  4124. if '999' not in product_title or temp_search_key not in product_title :
  4125. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4126. continue
  4127. elif '0.26g*12' not in product_title:
  4128. print(f"当前商品名称:{product_title} 不包含0.26g*12品规")
  4129. continue
  4130. elif self.search_key == '999小儿止咳糖浆120':
  4131. if '999' not in product_title or temp_search_key not in product_title :
  4132. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4133. continue
  4134. elif '120' not in product_title:
  4135. print(f"当前商品名称:{product_title} 不包含120品规")
  4136. continue
  4137. elif self.search_key == '999小儿止咳糖浆225':
  4138. if '999' not in product_title or temp_search_key not in product_title :
  4139. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4140. continue
  4141. elif '225' not in product_title:
  4142. print(f"当前商品名称:{product_title} 不包含225品规")
  4143. continue
  4144. elif self.search_key == '999小儿感冒颗粒6g*10':
  4145. if '999' not in product_title or temp_search_key not in product_title :
  4146. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4147. continue
  4148. elif '6g*10' not in product_title:
  4149. print(f"当前商品名称:{product_title} 不包含6g*10品规")
  4150. continue
  4151. elif self.search_key == '999小儿感冒颗粒6g*24':
  4152. if '999' not in product_title or temp_search_key not in product_title :
  4153. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4154. continue
  4155. elif '6g*24' not in product_title:
  4156. print(f"当前商品名称:{product_title} 不包含6g*24品规")
  4157. continue
  4158. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  4159. if '999' not in product_title or temp_search_key not in product_title :
  4160. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4161. continue
  4162. elif '6g*10' not in product_title:
  4163. print(f"当前商品名称:{product_title} 不包含6g*10品规")
  4164. continue
  4165. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  4166. if '999' not in product_title or temp_search_key not in product_title :
  4167. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4168. continue
  4169. elif '6g*20' not in product_title:
  4170. print(f"当前商品名称:{product_title} 不包含6g*20品规")
  4171. continue
  4172. elif self.search_key == '999感冒灵胶囊':
  4173. if '999' not in product_title or temp_search_key not in product_title :
  4174. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4175. continue
  4176. elif '0.5g*12' not in product_title:
  4177. print(f"当前商品名称:{product_title} 不包含0.5g*12品规")
  4178. continue
  4179. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  4180. if '999' not in product_title or temp_search_key not in product_title :
  4181. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4182. continue
  4183. elif '8g*10' not in product_title:
  4184. print(f"当前商品名称:{product_title} 不包含8g*10品规")
  4185. continue
  4186. elif self.search_key == '999小儿感冒宁颗粒2.5g*10袋':
  4187. if '999' not in product_title or temp_search_key not in product_title :
  4188. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4189. continue
  4190. elif '2.5g*10' not in product_title:
  4191. print(f"当前商品名称:{product_title} 不包含2.5g*10品规")
  4192. continue
  4193. elif self.search_key == '999感冒清热颗粒12g*18':
  4194. if '999' not in product_title or temp_search_key not in product_title :
  4195. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4196. continue
  4197. elif '12g*18' not in product_title:
  4198. print(f"当前商品名称:{product_title} 不包含12g*18品规")
  4199. continue
  4200. elif self.search_key == '999藿香正气合剂':
  4201. if '999' not in product_title or temp_search_key not in product_title :
  4202. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4203. continue
  4204. elif '10ml*6' not in product_title and '10ml*10' not in product_title:
  4205. print(f"当前商品名称:{product_title} 不包含10ml*6 和 10ml*10品规")
  4206. continue
  4207. else:
  4208. if '999' not in product_title or temp_search_key not in product_title:
  4209. print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  4210. continue
  4211. # if '999' not in product_title or temp_search_key not in product_title:
  4212. # print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  4213. # continue
  4214. else:
  4215. if self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  4216. temp_search_key = self.search_key.replace('史达功', '')
  4217. temp_search_key = temp_search_key.replace('120', '')
  4218. if '史达功' not in product_title or temp_search_key not in product_title :
  4219. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4220. continue
  4221. elif '120' not in product_title:
  4222. print(f"当前商品名称:{product_title} 不包含120品规")
  4223. continue
  4224. elif self.search_key == '三九胃泰养胃舒颗粒8袋':
  4225. temp_search_key = self.search_key.replace('三九胃泰', '')
  4226. temp_search_key = temp_search_key.replace('8袋', '')
  4227. if '三九胃泰' not in product_title or temp_search_key not in product_title :
  4228. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4229. continue
  4230. elif '8' not in product_title:
  4231. print(f"当前商品名称:{product_title} 不包含8品规")
  4232. continue
  4233. elif self.search_key == '今维多赐多康牌蛋白粉':
  4234. temp_search_key = self.search_key.replace('今维多', '')
  4235. if '今维多' not in product_title or temp_search_key not in product_title :
  4236. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4237. continue
  4238. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  4239. temp_search_key = self.search_key.replace('佳美舒', '')
  4240. temp_search_key = temp_search_key.replace('4', '')
  4241. if '佳美舒' not in product_title or temp_search_key not in product_title :
  4242. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4243. continue
  4244. elif '4' not in product_title and '8' not in product_title:
  4245. print(f"当前商品名称:{product_title} 不包含4和或8品规")
  4246. continue
  4247. elif self.search_key == '三九胃泰颗粒20g*10':
  4248. temp_search_key = self.search_key.replace('20g*10', '')
  4249. if temp_search_key not in product_title :
  4250. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4251. continue
  4252. elif '20g*10' not in product_title:
  4253. print(f"当前商品名称:{product_title} 不包含20g*10品规")
  4254. continue
  4255. elif self.search_key == '三九胃泰颗粒20g*6袋':
  4256. temp_search_key = self.search_key.replace('20g*6袋', '')
  4257. if temp_search_key not in product_title :
  4258. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4259. continue
  4260. elif '20g*6' not in product_title:
  4261. print(f"当前商品名称:{product_title} 不包含20g*6品规")
  4262. continue
  4263. elif self.search_key == '顺峰康王酮康他索乳膏':
  4264. temp_search_key = self.search_key.replace('顺峰康王', '')
  4265. if '顺峰康王' not in product_title or temp_search_key not in product_title :
  4266. print(f"当前商品名称:{product_title} 不包含关键字:{temp_search_key}")
  4267. continue
  4268. else:
  4269. if self.search_key not in product_title.replace(' ', ''):
  4270. continue
  4271. else:
  4272. print(f"列表当前商品名称不存在")
  4273. #价格
  4274. 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'
  4275. 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'
  4276. if self.d.xpath(price_xpath).exists:
  4277. price_str = self.d.xpath(price_xpath).text
  4278. print(f"price_xpath列表当前商品价格:{price_str}")
  4279. if price_str:
  4280. price = float(re.search('[\d\.]+', price_str).group())
  4281. elif self.d.xpath(price_xpath3).exists:
  4282. price_str = self.d.xpath(price_xpath3).text
  4283. print(f"price_xpath3列表当前商品价格:{price_str}")
  4284. if price_str:
  4285. price = float(re.search('[\d\.]+', price_str).group())
  4286. else:
  4287. 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'
  4288. if self.d.xpath(price_xpath2).exists:
  4289. price_str = self.d.xpath(price_xpath2).text
  4290. print(f"price_xpath2列表当前商品价格:{price_str}")
  4291. if price_str:
  4292. price = float(re.search('[\d\.]+', price_str).group())
  4293. else:
  4294. print(f"列表当前商品价格不存在")
  4295. # price_str = self.d.xpath(f'//android.support.v7.widget.RecyclerView/android.widget.FrameLayout[{idxx}]//*[starts-with(@text,"¥")]').text
  4296. print(f'列表获取到价格:{price}')
  4297. #店铺名称的xpath
  4298. 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]'
  4299. 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]'
  4300. if self.d.xpath(shop_name_xpath).exists:
  4301. shop_name = self.d.xpath(shop_name_xpath).text
  4302. print(f"shop_name_xpath列表当前商品店铺名称:{shop_name}")
  4303. elif self.d.xpath(shop_name_xpath2).exists:
  4304. shop_name = self.d.xpath(shop_name_xpath2).text
  4305. print(f"shop_name_xpath2列表当前商品店铺名称:{shop_name}")
  4306. else:
  4307. print(f"列表当前商品店铺名称不存在")
  4308. #如果商品的名称、价格和生产厂家都不存在则直接下一条数据。 跳过一些不是商品的数据。
  4309. if product_title == '' and price == '' and shop_name == '':
  4310. continue
  4311. scrape_date = self.get_current_date()
  4312. if product_title and price and shop_name:
  4313. #判断数据表中是否存在
  4314. dup_data = {'product': product_title, 'min_price': price, 'shop': shop_name, 'scrape_date': scrape_date,'platform': '美团'}
  4315. if self.data_is_exists(dup_data):
  4316. print('列表存在相同数据不入库')
  4317. continue
  4318. self.safe_exec(drug_one.click)
  4319. print('点击目标药品完毕')
  4320. time.sleep(2)
  4321. # 采集药品信息
  4322. try:
  4323. # self.integrate_data()
  4324. self.safe_exec(self.integrate_data)
  4325. # 检测下是否回退到列表页
  4326. if self.distinct_target():
  4327. print('回退到列表页', True)
  4328. else:
  4329. if self.d.xpath('//*[@text="搜索"]').exists:
  4330. print("检测到搜索按钮,重新开始采集流程")
  4331. if retry_count < MAX_RETRY:
  4332. # 停止当前监控线程
  4333. self.monitor.stop()
  4334. self.monitor.join()
  4335. # 递归重启采集
  4336. return self.main(device_id, retry_count+1)
  4337. else:
  4338. print("超过最大重试次数,终止程序")
  4339. return
  4340. else:
  4341. print("无法恢复页面,终止采集")
  4342. return
  4343. # print('回退到列表页失败,终止采集')
  4344. # return
  4345. time.sleep(self.get_sleep_time())
  4346. spider_no += 1
  4347. except Exception as e:
  4348. print(f'采集药品详情数据出错:{e}')
  4349. #增加阻塞的方法:
  4350. if not self.distinct_target():
  4351. for i in range(1):
  4352. self.swipe_back(1)
  4353. # 最外部有个定位按钮
  4354. if self.distinct_target():
  4355. break
  4356. if i == 0 and not self.distinct_target():
  4357. print('页面出错,退出采集')
  4358. return
  4359. else:
  4360. continue
  4361. if self.d.xpath('//*[@text="已经到底啦"]').exists:
  4362. print('已经到达列表页最底部')
  4363. return
  4364. search_list = self.d.xpath('//android.support.v7.widget.RecyclerView').info
  4365. bounds = search_list['bounds']
  4366. #print('搜索列表高度', 1400 + bounds['top'] - bounds['bottom'])
  4367. # self.d.swipe(200, 1400, 200, 1400 + bounds['top'] - bounds['bottom'])
  4368. # 计算滑动距离
  4369. scroll_distance = bounds['bottom'] - bounds['top'] # 正数
  4370. start_y = 1600
  4371. end_y = start_y - scroll_distance # 向上滑动,y 坐标减小
  4372. # 确保 end_y 不小于 0
  4373. end_y = max(end_y, 304) # 留出一点边距,避免滑出屏幕
  4374. # print('滑动起点 y:', start_y, '终点 y:', end_y)
  4375. # self.d.swipe(200, start_y, 200, end_y, 0.4)
  4376. print('开始滑动')
  4377. self.d.drag(300, 1400, 300, 400, 1)
  4378. # self.safe_exec(self.d.drag, 300, 1400, 300, 400, 1)
  4379. print('滑动结束')
  4380. #print('搜索列表高度', 1400 + bounds['top'] - bounds['bottom'])
  4381. # self.d.swipe(200, 1400, 200, 1400 + bounds['top'] - bounds['bottom'])
  4382. # self.d.swipe(200, 1400, 200, 1400 + bounds['top'] - bounds['bottom'], 0.4)
  4383. time.sleep(self.get_sleep_time())
  4384. finally:
  4385. # 确保监控线程被停止
  4386. self.monitor.stop()
  4387. self.monitor.join()
  4388. def unitest(self):
  4389. """
  4390. 单元测试
  4391. :return:
  4392. """
  4393. save_data = {
  4394. 'product':"[昆中药]舒肝颗粒(低糖型)",
  4395. 'min_price': 14.0,
  4396. 'manufacture_date': '',
  4397. 'expiry_date': '36个月',
  4398. 'shop': '美团自营大药房(快递电商)',
  4399. 'business_license_company': '',
  4400. 'province': '',
  4401. 'city': '',
  4402. 'manufacturer': '昆明中药厂有限公司',
  4403. 'specification': '3g*16袋/盒',
  4404. 'approval_number': '国药准字Z53021161',
  4405. 'product_link': '',
  4406. 'scrape_date': '2025/07/09',
  4407. 'scrape_province': '广东',
  4408. 'availability': '',
  4409. 'credit_code': '',
  4410. 'platform': '美团'
  4411. }
  4412. self.save_to_database(save_data)
  4413. time.sleep(100000)
  4414. pass
  4415. # retrieve database
  4416. def get_retrieve_mysql():
  4417. """
  4418. 建立远端连接并返回一个到数据库的连接对象
  4419. """
  4420. import pymysql
  4421. return pymysql.connect(
  4422. host='39.108.116.125', # 修改后的主机
  4423. port=3306, # 添加端口号
  4424. user='drug_retrieve', # 修改后的用户名
  4425. password='Pem287cwM58jNpe2', # 修改后的密码
  4426. db='drug_retrieve', # 修改后的数据库名
  4427. charset='utf8mb4'
  4428. )
  4429. def main():
  4430. #从配置的系统里面读取采集用到的设备和搜索关键词
  4431. #1、数据库的连接,从数据库中获取采集品规和设备adb码,启动程序进行采集,如果配置了采集时间,还需要支持到了时间终止采集,如果配置了采集的页数,需要滑动到指定的页数后再进行采集
  4432. #2、代码要支持多线程(线程池)的管理,每个线程有自己的生命周期。
  4433. #获取未开始的美团平台的采集任务
  4434. retrieve_conn = get_retrieve_mysql()
  4435. cursor = retrieve_conn.cursor()
  4436. query = "SELECT id,collect_equipment_id,product_name,start_page,end_page FROM retrieve_collect_task_allocate WHERE status = 1 AND platform = 4"
  4437. cursor.execute(query)
  4438. result = cursor.fetchone()
  4439. if result:
  4440. collect_equipment_id = result [1]
  4441. product_name = result[2]
  4442. start_page = result[3]
  4443. end_page = result[4]
  4444. print(f"collect_equipment_id={collect_equipment_id}")
  4445. print(f"product_name={product_name}")
  4446. if collect_equipment_id == 0:
  4447. print("设备id不存在")
  4448. return
  4449. if product_name == '':
  4450. print("采集关键字获取失败")
  4451. return
  4452. #通过 collect_equipment_id 获取设别adb码
  4453. device_query = "SELECT device_id FROM retrieve_collect_equipment WHERE id = %s and status = 0"
  4454. cursor.execute(device_query, (collect_equipment_id))
  4455. device_result = cursor.fetchone()
  4456. if device_result:
  4457. device_id = device_result[0]
  4458. print(f"device_id={device_id}")
  4459. else:
  4460. # self.loggerMT.info("没有可用的设备进行数据采集")
  4461. print("没有可用的设备进行数据采集")
  4462. return
  4463. else:
  4464. # self.loggerMT.info("MT 没有要采集的品规")
  4465. print("MT 没有要采集的品规")
  4466. return
  4467. key = product_name
  4468. try:
  4469. mt = MT(key) # 用当前关键字实例化
  4470. mt.main(device_id,start_page,end_page) # 执行一次完整采集
  4471. logging.info(f'关键字 {key} 本轮采集完成')
  4472. except Exception as e:
  4473. # 发生异常直接跳过该关键字,继续下一轮
  4474. logging.exception(f'关键字 {key} 采集异常:{e}')
  4475. finally:
  4476. # 关闭当前 MT 实例资源(如有需要)
  4477. if hasattr(mt, 'close'):
  4478. mt.close()
  4479. # keys_list = [
  4480. # # '三九胃泰颗粒',
  4481. # # '999小柴胡颗粒',
  4482. # # '999强力枇杷露',
  4483. # # '[999]感冒清热颗粒',
  4484. # # '999抗病毒口服液',
  4485. # # '999皮炎平',
  4486. # # '999盐酸特比萘芬乳膏',
  4487. # # '999盐酸特比萘芬',
  4488. # # '999藿香正气合剂',
  4489. # # '999必无忧盐酸特比萘芬乳膏',
  4490. # # '999复方感冒灵颗粒',
  4491. # # '999糠酸莫米松凝胶',
  4492. # # '999铝碳酸镁咀嚼片',
  4493. # # '999阿奇霉素片',
  4494. # # '999选平硝酸咪康唑乳膏',
  4495. # # 按需继续添加,
  4496. # #暂时不需要
  4497. # # '999必无忧盐酸特比萘芬喷雾剂30'
  4498. # # '999冰连清咽'
  4499. # # '999复方金银花颗粒10g'
  4500. # # '999复方苦参肠炎康片12片'
  4501. # # '999强力枇杷露16袋'
  4502. # # '999三蛇胆川贝膏138'
  4503. # # '999维生素ec颗粒'
  4504. # # '三九胃泰养胃舒颗粒8袋'
  4505. # # '999止泻利颗粒15g*8'
  4506. # # '999阿奇霉素片'
  4507. # # '999可调式生理性海水鼻腔喷雾50'
  4508. # # '999小儿止咳糖浆120' #不低于19.8
  4509. # # '999小儿止咳糖浆225' #禁止挂网
  4510. # # '999小儿感冒颗粒6g*10' #不低于24.9
  4511. # # '999小儿感冒颗粒6g*24' #禁止挂网
  4512. # # '999小儿氨酚黄那敏颗粒6g*10袋' #不低于15.8
  4513. # # '999小儿氨酚黄那敏颗粒6g*20袋' #禁止挂网
  4514. # # '999小儿咽扁颗粒8g*10袋' #仅限999官旗店
  4515. # #2025-08-01最新 其中 藿香正气合剂两种规格 10支和6支 抗病毒口服液 12支和18支 蒲地蓝 24片 36片和44片 枇杷露225ml 小柴胡颗粒9袋和15袋 养胃舒 6袋 复方感冒灵颗粒15袋,
  4516. # #曲安奈德益康唑乳膏 30g 葡萄糖酸锌口服溶液 12支 18支 24支和30支,
  4517. # # 1、999止泻利颗粒15g*8 没有数据 2、999维生素ec颗粒 没有数据 3、999三蛇胆川贝膏138 没有数据 4、999强力枇杷露16袋 没有数据 5、999冰连清咽 没有数据
  4518. # # '999藿香正气合剂',
  4519. # # '999糠酸莫米松凝胶15',
  4520. # # '999抗病毒口服液'
  4521. # # '999抗病毒口服液10ml*10'
  4522. # # '999抗病毒口服液10ml*12',
  4523. # # '999蒲地蓝消炎片',
  4524. # # '999强力枇杷露225ml',
  4525. # # '999小柴胡颗粒',
  4526. # # '999养胃舒颗粒',
  4527. # # '999复方感冒灵颗粒',
  4528. # # '999黄芪精',
  4529. # # '999皮炎平曲安奈德益康唑乳膏30',
  4530. # # '999葡萄糖酸锌口服溶液',
  4531. # # '今维多赐多康牌蛋白粉',
  4532. # # '佳美舒阿奇霉素肠溶胶囊4'
  4533. # # '999必无忧盐酸特比萘芬乳膏15g'
  4534. # # '999复方板蓝根颗粒15g*15袋/盒'
  4535. # # '999速复康布洛芬缓释胶囊'
  4536. # # '999维生素C咀嚼片'
  4537. # # '999精氨酸布洛芬颗粒'
  4538. # # '999强力枇杷露120ml'
  4539. # #OTC
  4540. # # '999强力枇杷露150ml'
  4541. # # '999糠酸莫米松乳膏10g支'
  4542. # # '999选平硝酸咪康唑乳膏20g'
  4543. # '999感冒清热颗粒(无糖)6g'
  4544. # # '999银菊清咽颗粒' #只有一条数据
  4545. # # '999补脾益肠丸'
  4546. # # '999壮骨关节丸6g*20'
  4547. # # '999壮骨关节胶囊'
  4548. # # '999正天丸6g*15'
  4549. # # '999正天胶囊'
  4550. # # '三九胃泰胶囊'
  4551. # # '三九胃泰颗粒20g*10'
  4552. # # '三九胃泰颗粒(无糖)2.5g*6' # 没有数据
  4553. # #10.31 new add
  4554. # # '999感冒灵颗粒' #不低于15.5
  4555. # # '999皮炎平复方醋酸地塞米松乳膏20' #不低于12.5
  4556. # # '三九胃泰颗粒20g*6袋' #不低于13.5
  4557. # # '顺峰康王酮康他索乳膏' #包含10g和20g两个规格 10g 不低于7.5 20g 不低于12.5
  4558. # # '999糠酸莫米松凝胶10' #不低于26.9
  4559. # # '999板蓝根颗粒10g*20' #不低于26.9
  4560. # # '999复方氨酚烷胺胶囊12粒' #不低于17.9
  4561. # # '999复方氨酚烷胺胶囊10粒' #禁止挂网
  4562. # # '999复方氨酚烷胺胶囊6粒' #禁止挂网
  4563. # # '999复方氨酚烷胺胶囊'
  4564. # # '999咽炎片0.26g*12片*2板' #不低于13.5
  4565. # # '999感冒灵胶囊' #仅限999官旗店
  4566. # # '999荆防颗粒' #美团没有数据 #禁止挂网
  4567. # # '999小儿感冒宁颗粒2.5g*10袋' #禁止挂网
  4568. # # '999磷酸奥司他韦胶囊75mg*10' #仅限999官旗店
  4569. # # '史达功右美沙芬愈创甘油醚糖浆120' #仅限999官旗店
  4570. # # '999感冒清热颗粒12g*18'
  4571. # ]
  4572. # #美团手机号:
  4573. # # device_id = '21885f5' # 设备序列号
  4574. # # device_id = '2e58510' # 设备序列号
  4575. # # device_id = 'KNNNEMNVWCJZQOLZ'
  4576. # # device_id = 'B6JVE6AYSWU4LRLZ'
  4577. # # device_id = '656DTOPRZDEALZ5X'
  4578. # # device_id = 'GIOFIBRKZTUGJJAE'
  4579. # # device_id = 'fcb3c749'
  4580. # # device_id = 'UCQGF6CQFMU8WKHI'
  4581. # # device_id = '4TZDUGTOAIFMJVGU'
  4582. # # device_id = '95b2c764'
  4583. # # device_id = 'ZDQWUSSWBEDI896T'
  4584. # # device_id = 'R4SKMJPNBQAENRAM'
  4585. # # device_id = '1462a51f'
  4586. # # device_id = '97ae80e0' # 设备序列号
  4587. # # device_id = 'IZTOWWDQT45D49BU'
  4588. # # device_id = 'N7ZXBITOSOGMYXQS'
  4589. # # device_id = '369dcf96'
  4590. # # device_id = 'GQIRKB7LVOONM7VW'
  4591. # device_id = 'T4UCEQGQEEYP65ZL'
  4592. # # device_id = '49L7GMPRVS85LJHE'
  4593. # # device_id = 'WWRO9LTGG6KFGQCM'
  4594. # # device_id = 'DYF67TM7KJ4POJLF'
  4595. # # device_id = 'ea4e4eb8'
  4596. # # device_id = 'U8ONIJJJS4CELVD6'
  4597. # cycle_no = 0 # 轮次计数
  4598. # while True:
  4599. # cycle_no += 1
  4600. # logging.info(f'========== 第 {cycle_no} 轮采集开始 ==========')
  4601. # for idx, key in enumerate(keys_list, 1):
  4602. # logging.info(f'[{idx}/{len(keys_list)}] 开始采集关键字:{key}')
  4603. # try:
  4604. # mt = MT(key) # 用当前关键字实例化
  4605. # mt.main(device_id) # 执行一次完整采集
  4606. # logging.info(f'关键字 {key} 本轮采集完成')
  4607. # except Exception as e:
  4608. # # 发生异常直接跳过该关键字,继续下一轮
  4609. # logging.exception(f'关键字 {key} 采集异常:{e}')
  4610. # finally:
  4611. # # 关闭当前 MT 实例资源(如有需要)
  4612. # if hasattr(mt, 'close'):
  4613. # mt.close()
  4614. # logging.info('本轮全部关键字采集完成,等待 2 小时后下一轮...')
  4615. # time.sleep(1 * 3600) # 2 小时 = 7200 秒
  4616. # keys = '小柴胡颗粒' # 参苓健脾胃颗粒 舒肝颗粒 清肺化痰丸 香砂平胃颗粒 小柴胡颗粒
  4617. # mt = MT(keys) # 参苓健脾胃颗粒 舒肝颗粒 清肺化痰丸 香砂平胃颗粒
  4618. # # mt.main('95b2c764')
  4619. # mt.main('fcb3c749')
  4620. class TimeoutException(Exception):
  4621. pass
  4622. @contextmanager
  4623. def time_limit(seconds):
  4624. """超时上下文管理器"""
  4625. def signal_handler(signum, frame):
  4626. raise TimeoutException("任务执行超时")
  4627. signal.signal(signal.SIGALRM, signal_handler)
  4628. signal.alarm(seconds)
  4629. try:
  4630. yield
  4631. finally:
  4632. signal.alarm(0)
  4633. # 如果需要并行处理(提高效率),可以使用线程池:
  4634. def process_tasks_in_parallel(max_workers=10):
  4635. """使用线程池并行处理多个任务""" """使用线程池并行处理多个任务,每个任务最多执行30分钟"""
  4636. from concurrent.futures import ThreadPoolExecutor, as_completed
  4637. retrieve_conn = get_retrieve_mysql()
  4638. cursor = retrieve_conn.cursor()
  4639. query = """
  4640. SELECT id, collect_equipment_id, product_name, start_page, end_page, duration
  4641. FROM retrieve_collect_task_allocate
  4642. WHERE status = 1 AND platform = 4
  4643. """
  4644. cursor.execute(query)
  4645. results = cursor.fetchall()
  4646. print(f"获取到的任务结果={results}")
  4647. if not results:
  4648. print("MT 没有要采集的品规")
  4649. return
  4650. # 准备任务列表
  4651. tasks = []
  4652. device_map = {}
  4653. for result in results:
  4654. task_id = result[0]
  4655. collect_equipment_id = result[1]
  4656. product_name = result[2]
  4657. start_page = result[3]
  4658. end_page = result[4]
  4659. duration = result[5]
  4660. if collect_equipment_id != 0 and product_name and product_name.strip():
  4661. # 缓存设备查询
  4662. if collect_equipment_id not in device_map:
  4663. device_query = "SELECT device_id FROM retrieve_collect_equipment WHERE id = %s AND status = 0"
  4664. cursor.execute(device_query, (collect_equipment_id,))
  4665. device_result = cursor.fetchone()
  4666. device_map[collect_equipment_id] = device_result[0] if device_result else None
  4667. if device_map[collect_equipment_id]:
  4668. tasks.append({
  4669. 'task_id': task_id,
  4670. 'device_id': device_map[collect_equipment_id],
  4671. 'key': product_name.strip(),
  4672. 'start_page': start_page,
  4673. 'end_page': end_page,
  4674. 'duration_minutes': duration, # 存储执行时间限制(分钟)
  4675. 'duration_seconds': duration * 60 # 转换为秒
  4676. })
  4677. cursor.close()
  4678. retrieve_conn.close()
  4679. if not tasks:
  4680. print("没有有效的采集任务")
  4681. return
  4682. print(f"准备并行处理 {len(tasks)} 个任务")
  4683. def process_single_task(task):
  4684. """处理单个任务的函数"""
  4685. start_time = time.time()
  4686. try:
  4687. mt = MT(task['key'])
  4688. # 执行采集,获取采集数量 关键数据:实际采集的数量,实际的页数
  4689. mt.main(task['device_id'], task['start_page'], task['end_page'], task['task_id'])
  4690. execution_time = time.time() - start_time
  4691. execution_minutes = execution_time / 60
  4692. logging.info(f"任务 {task['task_id']}: 关键字 {task['key']} 采集完成,耗时 {execution_minutes:.1f} 分钟 (限制: {task['duration_minutes']} 分钟)")
  4693. return task['task_id'], True, None, execution_time, task['duration_minutes']
  4694. except Exception as e:
  4695. execution_time = time.time() - start_time
  4696. execution_minutes = execution_time / 60
  4697. logging.exception(f"任务 {task['task_id']}: 采集异常:{e},已执行 {execution_minutes:.1f} 分钟 (限制: {task['duration_minutes']} 分钟)")
  4698. return task['task_id'], False, str(e), execution_time, task['duration_minutes']
  4699. finally:
  4700. if 'mt' in locals() and hasattr(mt, 'close'):
  4701. try:
  4702. mt.close()
  4703. except:
  4704. pass
  4705. # 使用线程池并行执行
  4706. successful_tasks = 0
  4707. failed_tasks = 0
  4708. timeout_tasks = 0
  4709. total_execution_time = 0
  4710. with ThreadPoolExecutor(max_workers=max_workers) as executor:
  4711. # 提交所有任务,每个任务最多30分钟
  4712. future_to_task = {
  4713. executor.submit(process_single_task, task): task
  4714. for task in tasks
  4715. }
  4716. # 处理完成的任务
  4717. for future in as_completed(future_to_task):
  4718. task = future_to_task[future]
  4719. try:
  4720. # 设置future的超时时间为任务限制时间+1分钟(作为缓冲)
  4721. timeout_buffer = task['duration_seconds'] + 60
  4722. task_id, success, error, execution_time, duration_limit = future.result(timeout=timeout_buffer)
  4723. total_execution_time += execution_time
  4724. execution_minutes = execution_time / 60
  4725. if success:
  4726. successful_tasks += 1
  4727. print(f"任务 {task_id}: 完成,耗时 {execution_minutes:.1f} 分钟 (限制: {duration_limit} 分钟)")
  4728. else:
  4729. failed_tasks += 1
  4730. timeout_tasks += 1
  4731. print(f"任务 {task_id}: 失败,耗时 {execution_minutes:.1f} 分钟 (限制: {duration_limit} 分钟)")
  4732. except FutureTimeoutError as e:
  4733. failed_tasks += 1
  4734. timeout_tasks += 1
  4735. print(f"任务 {task['task_id']}: 执行超时({task['duration_minutes']} 分钟限制)")
  4736. # 尝试取消这个任务
  4737. future.cancel()
  4738. except Exception as e:
  4739. failed_tasks += 1
  4740. print(f"任务 {task['task_id']}: 执行异常 {e}")
  4741. if (successful_tasks + failed_tasks) > 0:
  4742. avg_time = total_execution_time / (successful_tasks + failed_tasks)
  4743. avg_minutes = avg_time / 60
  4744. else:
  4745. avg_minutes = 0
  4746. total_minutes = total_execution_time / 60
  4747. print(f"\n并行采集完成:")
  4748. print(f"成功: {successful_tasks} 个")
  4749. print(f"失败: {failed_tasks} 个 (其中超时: {timeout_tasks} 个)")
  4750. if avg_minutes > 0:
  4751. print(f"平均执行时间: {avg_minutes:.1f} 分钟")
  4752. print(f"总计执行时间: {total_minutes:.1f} 分钟")
  4753. if __name__ == '__main__':
  4754. # main()
  4755. process_tasks_in_parallel(max_workers=10) # 可以同时处理3个任务
  4756. # scheduler = BlockingScheduler()
  4757. # scheduler.add_job(main, 'cron', hour=21, minute=30, misfire_grace_time=120)
  4758. # try:
  4759. # scheduler.start()
  4760. # except (KeyboardInterrupt, SystemExit):
  4761. # pass