get_app_pdd__data_6.py 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849
  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 apscheduler.schedulers.blocking import BlockingScheduler
  12. from aip import AipOcr
  13. import numpy as np
  14. import cv2
  15. import os
  16. from pdd_config import Config
  17. import logging
  18. from logger import setup_logger
  19. import xml.etree.ElementTree as ET
  20. import secrets
  21. setup_logger("pdd_spider") # 初始化日志
  22. def get_access_token():
  23. AppKey = Config.APP_KEY
  24. AppSrcret = Config.APP_SECRET
  25. token_url = Config.token_url
  26. url = f"{token_url}?grant_type=client_credentials&client_id={AppKey}&client_secret={AppSrcret}"
  27. payload = ""
  28. headers = {
  29. 'Content-Type': 'application/json',
  30. 'Accept': 'application/json'
  31. }
  32. response = requests.request("POST", url, headers=headers, data=payload)
  33. try:
  34. return response.json()['access_token']
  35. except:
  36. return None
  37. def get_mysql():
  38. """
  39. 建立并返回一个到数据库的连接对象
  40. """
  41. import pymysql
  42. return pymysql.connect(
  43. host=Config.DB_HOST, # "localhost", # 修改后的主机
  44. port=Config.DB_PORT, # 3306, # 添加端口号
  45. user=Config.DB_USER, # 'root', # 修改后的用户名
  46. password=Config.DB_PASSWORD, # 修改后的密码
  47. db=Config.DB_NAME, # "drug_data", # 修改后的数据库名
  48. charset='utf8mb4'
  49. )
  50. # 获取滑块验证中滑块需要移动的距离
  51. def slide_verify(img_path):
  52. with open(img_path, 'rb') as f:
  53. b = base64.b64encode(f.read()).decode() ## 图片二进制流base64字符串
  54. url = "http://api.jfbym.com/api/YmServer/customApi"
  55. data = {
  56. ## 关于参数,一般来说有3个;不同类型id可能有不同的参数个数和参数名,找客服获取
  57. "token": "1nDVocTE2mJ0yLEYb2sZJ5uUY2VIEoGTkIpW44X7Kgk",
  58. "type": "22222",
  59. "image": b,
  60. }
  61. _headers = {
  62. "Content-Type": "application/json"
  63. }
  64. response = requests.request("POST", url, headers=_headers, json=data).json()
  65. print(response)
  66. if response.get("msg") == "识别成功":
  67. # 获取 data 中的 data 字段
  68. result = response.get("data", {}).get("data")
  69. if result:
  70. print(result) # 输出结果
  71. else:
  72. print("无法获取数据")
  73. else:
  74. print("识别未成功")
  75. return result
  76. class PDD:
  77. def __init__(self, search_key, device_id):
  78. self.package_name = 'com.xunmeng.pinduoduo'
  79. self.APP_ID = '116857964'
  80. self.API_KEY = '1gAzACJOAr7BeILKqkqPOETh'
  81. self.SECRET_KEY = 'ZNArANb9GwJYgLKg4EfYhukKBfPdl1n3'
  82. self.client = AipOcr(self.APP_ID, self.API_KEY, self.SECRET_KEY)
  83. # self.city2province = self.get_city_info()
  84. # host = "localhost"
  85. # user = "root"
  86. # password = "dfwy2025"
  87. # database = "drug_data"
  88. # port = 3306
  89. # self.table_name = "mt_drug"
  90. self.table_name = Config.DB_PDD_TABLE # "pdd_drug"
  91. self.shop_table_name = Config.DB_PDD_SHOP_TABLE # "pdd_shop_info"
  92. self.loggerPdd = logging.getLogger()
  93. self.clipboard = "" # 初始化剪切板的内容为空
  94. self.access_token = get_access_token()
  95. self.search_key = search_key # 参苓健脾胃颗粒 香砂平胃颗粒 舒肝颗粒 清肺化痰丸
  96. self.unrelated_data = 0 # 无关数据数量
  97. self.device_id = device_id
  98. #统计售罄数量
  99. self.sold_out_counts = 0
  100. # 程序启动时间
  101. self.program_start_time = self.app_start_time()
  102. #统计商品数量
  103. #最大量数据阈值
  104. self.max_counts = 0
  105. #统计点击商品的次数
  106. self.click_counts = 0
  107. #商品在列表的位置
  108. self.search_key_loc = 0
  109. # oss配置
  110. self.oss_config = {
  111. "access_key_id": Config.access_key_id,
  112. "access_key_secret": Config.access_key_secret,
  113. "endpoint": Config.endpoint, # 例: oss-cn-beijing.aliyuncs.com
  114. "bucket_name": Config.bucket_name,
  115. "oss_prefix": Config.oss_prefix # OSS中存放截图的前缀(虚拟文件夹)
  116. }
  117. # 代码运行那时候的时间
  118. def app_current_time(self):
  119. return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  120. def app_start_time(self):
  121. """
  122. 获取app启动时间
  123. :return:
  124. """
  125. return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  126. def stop_app(self):
  127. self.d.app_stop(self.package_name)
  128. time.sleep(5)
  129. def start_app(self):
  130. self.d.app_start(self.package_name)
  131. time.sleep(5)
  132. def restart_app(self):
  133. """
  134. 重启app
  135. :return:
  136. """
  137. self.stop_app()
  138. self.start_app()
  139. @staticmethod
  140. def get_sleep_time():
  141. return random.randint(1, 3)
  142. # return random.randint(5, 8)
  143. @staticmethod
  144. def get_current_date():
  145. return datetime.datetime.now().strftime('%Y/%m/%d')
  146. @staticmethod
  147. def get_city_info():
  148. """
  149. 获取所有的省市数据
  150. :return:
  151. """
  152. file_path = '../kailin_city.json'
  153. with open(file_path, 'r', encoding='utf-8') as f:
  154. data = json.load(f)
  155. province = {province_one["id"]: province_one for province_one in data['province']}
  156. city2province = dict()
  157. city = data['city']
  158. for city_one in city:
  159. name = city_one['name']
  160. pid = city_one['pid']
  161. if len(str(pid)) > 2:
  162. pid = int(re.match(r'^\d{2}', str(pid)).group())
  163. city2province[name] = province[pid]['name']
  164. return city2province
  165. def remove_watermark(self, img_path):
  166. """
  167. 图片去水印(将水印部分变成白色背景)并将数据转化为二进制数据
  168. :param img_path: 图片路径
  169. :return: 二进制图片数据
  170. """
  171. img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), -1)
  172. endswith = os.path.splitext(img_path)[1]
  173. new = np.clip(1.4057577998008846 * img - 38.33089999653017, 0, 255).astype(np.uint8)
  174. _, img_binary = cv2.imencode(endswith, new)
  175. return img_binary
  176. def human_slide(self, start_x, start_y, end_x, end_y):
  177. """模拟人类滑动轨迹"""
  178. # 生成带加速度的轨迹
  179. points = []
  180. total_steps = 50
  181. distance_x = end_x - start_x
  182. distance_y = end_y - start_y
  183. previous_x = start_x # 用于记录上一个 x 坐标值
  184. for i in range(total_steps):
  185. # 非线性进度(慢-快-慢)
  186. ratio = (i / total_steps)
  187. if ratio < 0.3:
  188. progress = 0.5 * (ratio / 0.3) ** 2
  189. elif ratio < 0.7:
  190. progress = 0.5 + (ratio - 0.3) * 1.25
  191. else:
  192. progress = 0.9 + 0.5 * ((ratio - 0.7) / 0.3) ** 0.5
  193. # 添加随机抖动
  194. # offset_x = np.random.randint(-2, 3)
  195. # offset_y = np.random.randint(-2, 3)
  196. offset_x = np.random.randint(-1, 1) # 控制抖动范围
  197. offset_y = np.random.randint(-1, 1)
  198. x = start_x + distance_x * min(progress, 0.99) + offset_x
  199. y = start_y + distance_y * min(progress, 0.99) + offset_y
  200. # 确保 x 坐标单调递增
  201. if x < previous_x and x < end_x:
  202. x = previous_x + 1
  203. if x > end_x:
  204. x = end_x
  205. previous_x = x
  206. points.append((x, y))
  207. # 变速延迟(移动越快延迟越短)
  208. delay = 0.002 + 0.01 * (1 - abs(0.5 - ratio))
  209. time.sleep(delay)
  210. print(f"points: {points}")
  211. self.loggerPdd.info(f"points: {points}")
  212. # 执行滑动轨迹
  213. # self.d.touch.down(points[0][0], points[0][1])
  214. for point in points[1:]:
  215. self.d.touch.move(point[0], point[1])
  216. self.d.touch.up(points[-1][0], points[-1][1])
  217. # print(f"points: {points}")
  218. # self.loggerPdd.info(f"points: {points}")
  219. # self.d.swipe_points(points, duration=0.05)
  220. def get_shop_name(self):
  221. """
  222. 获取店铺名
  223. :return:
  224. """
  225. try:
  226. xpath = '//*[@text="进店"]/preceding-sibling::android.view.ViewGroup/android.widget.LinearLayout/android.widget.TextView'
  227. if self.d.xpath(xpath).exists:
  228. shop_name = self.d.xpath(xpath).text
  229. self.loggerPdd.info(f'1-获取到店铺名:{shop_name}')
  230. else:
  231. # 进入店铺新页面
  232. shop_btn_xpath = '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]'
  233. if self.d.xpath(shop_btn_xpath).exists:
  234. self.d.xpath(shop_btn_xpath).click()
  235. time.sleep(1)
  236. # self.d.xpath('//*[@text="店铺"]').click()
  237. xpath_shop_name = '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.RelativeLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.TextView[1]'
  238. if self.d.xpath(xpath_shop_name).exists:
  239. shop_name = self.d.xpath(xpath_shop_name).text
  240. self.loggerPdd.info(f'2-获取到店铺名:{shop_name}')
  241. else:
  242. shop_name = ''
  243. self.loggerPdd.info(f'3-获取到店铺名:{shop_name}')
  244. self.swipe_back(1) #
  245. else:
  246. shop_name = ''
  247. self.loggerPdd.info('4-因为shop_btn_xpath不存在,获取到店铺名为空')
  248. # time.sleep(10000)
  249. # 进入店铺新页面 测试代码
  250. # self.d.xpath('//*[@text="店铺"]').click()
  251. # time.sleep(1)
  252. # content_frame = self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]').exists
  253. # print(content_frame)
  254. # ViewGroup3 = self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]').exists
  255. # print(ViewGroup3)
  256. # LinearLayout = self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]').exists
  257. # print(LinearLayout)
  258. # RelativeLayout = self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]').exists
  259. # print(RelativeLayout)
  260. # LinearLayout2 = self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]').exists
  261. # print(LinearLayout2)
  262. # RecyclerView = self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.support.v7.widget.RecyclerView[1]').exists
  263. # print(RecyclerView)
  264. # xpath2 = '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.RelativeLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.TextView[1]'
  265. # if self.d.xpath(xpath2).exists:
  266. # shop_name = self.d.xpath(xpath2).text
  267. # self.loggerPdd.info(f'2-获取到店铺名:{shop_name}')
  268. # else:
  269. # shop_name = ''
  270. # self.loggerPdd.info(f'3-获取到店铺名:{shop_name}')
  271. # self.swipe_back(1) #返回
  272. return shop_name
  273. except Exception as e:
  274. print(f'获取店铺名出错:{e}')
  275. self.loggerPdd.error(f'获取店铺名出错:{e}')
  276. return None
  277. def get_qualification_number(self):
  278. """
  279. 获取资质编号
  280. :return:
  281. """
  282. try:
  283. qualification_number_str = self.d.xpath(
  284. '//*[@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
  285. qualification_number = qualification_number_str.strip('资质编号:').strip()
  286. return qualification_number
  287. except:
  288. return None
  289. def enter_detail(self):
  290. self.d.xpath('//*[@resource-id="com.sankuai.meituan:id/recycler"]/android.widget.FrameLayout[1]').click()
  291. time.sleep(self.get_sleep_time())
  292. def save_to_database(self, data):
  293. print(f'保存数据到数据库:{data}')
  294. # # 连接数据库
  295. conn = get_mysql()
  296. # 创建游标对象
  297. cur = conn.cursor()
  298. # add_sql = "insert into delete_friend_table(delete_user_name,delete_user_id,delete_content,delete_time) value(%s,%s,%s,%s)"
  299. add_sql = f"""
  300. INSERT INTO {self.table_name}
  301. (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, number)
  302. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  303. """
  304. # 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']))
  305. cur.execute(add_sql,
  306. (data['product'], data['min_price'], data['manufacture_date'], data['expiry_date'], data['shop'],
  307. data['business_license_company'], data['province'], data['city'], data['manufacturer'],
  308. data['specification'], data['approval_number'], data['product_link'], data['scrape_date'],
  309. data['scrape_province'], data['availability'], data['credit_code'], data['platform'],
  310. data['search_key'], data['number']))
  311. conn.commit() # 提交数据
  312. self.max_counts += 1
  313. print(f"存入数据库成功")
  314. def click_target_product_by_search_key(self, fuzzy_match=False, timeout=10):
  315. """
  316. 动态匹配self.search_key对应的商品并点击
  317. :param fuzzy_match: 是否模糊匹配(应对商品名带额外后缀/前缀的情况) 不模糊匹配
  318. :param timeout: 等待元素出现的超时时间(秒)
  319. :return: 点击是否成功(bool)
  320. """
  321. try:
  322. # 1. 定义定位条件(动态使用self.search_key)
  323. if fuzzy_match:
  324. # 模糊匹配:包含search_key即可(推荐,适配搜索结果商品名略有差异)
  325. locator = self.d(textContains=self.search_key)
  326. print(f"🔍 模糊匹配商品:包含「{self.search_key}」的元素")
  327. else:
  328. # 精确匹配:商品名与search_key完全一致
  329. locator = self.d(text=self.search_key)
  330. print(f"🔍 精确匹配商品:「{self.search_key}」")
  331. # 2. 等待元素出现(核心:避免元素未加载就点击)
  332. if locator.wait(timeout=timeout):
  333. print(f"✅ 找到匹配的商品,准备点击")
  334. # 执行点击(优先点击可点击的元素)
  335. locator.click()
  336. print(f"✅ 成功点击「{self.search_key}」对应的商品")
  337. # 点击后等待页面加载
  338. time.sleep(self.get_sleep_time())
  339. return True
  340. else:
  341. # print(f"❌ 超时未找到「{self.search_key}」对应的商品,尝试滑动刷新")
  342. # # 容错:向上滑动(加载更多)后重试
  343. # self.swipe_up()
  344. # 滑动后再等待5秒重试
  345. # if locator.wait(timeout=5):
  346. # locator.click()
  347. # print(f"✅ 滑动后找到并点击「{self.search_key}」对应的商品")
  348. # return True
  349. # else:
  350. print(f"❌ 滑动后仍未找到「{self.search_key}」对应的商品")
  351. return False
  352. except Exception as e:
  353. print(f"❌ 点击「{self.search_key}」对应商品时异常:{e}")
  354. return False
  355. def swipe_down(self):
  356. """
  357. 下滑(模拟真人操作,抗风控+设备适配+容错)
  358. 核心:起点在屏幕上方,终点在屏幕下方(和上滑相反)
  359. :return: None
  360. """
  361. try:
  362. # 1. 获取屏幕尺寸(兼容不同设备,给默认值避免获取失败)
  363. screen_width = self.d.info.get('displayWidth', 1080) # 默认1080px宽度
  364. screen_height = self.d.info.get('displayHeight', 2400) # 默认2400px高度
  365. # 2. 随机滑动时长(0.1~0.3秒,避免固定值被风控,且不设0秒)
  366. duration_rate = random.uniform(0.1, 0.3)
  367. # 3. 计算滑动坐标(用屏幕比例,适配所有设备)
  368. start_x = screen_width // 2 # 水平居中(和上滑一致,符合真人操作习惯)
  369. start_y = screen_height * 0.2 # 起点:屏幕20%高度(上方偏下)
  370. end_y = screen_height * 0.8 # 终点:屏幕80%高度(下方偏上)
  371. # 强制确保起点y < 终点y(必为向下滑,避免逻辑错误)
  372. start_y, end_y = min(start_y, end_y - 10), max(end_y, start_y + 10)
  373. # 4. 核心向下滑动操作
  374. self.d.swipe(start_x, start_y, start_x, end_y, duration=duration_rate)
  375. # 滑动后全局等待(确保页面加载,避免元素定位失败)
  376. time.sleep(self.get_sleep_time())
  377. except Exception as e:
  378. # 异常捕获:避免设备断开/滑动失败导致程序崩溃
  379. print(f"向下滑动失败:{e}")
  380. # 兜底方案:用固定坐标重试(适配主流1080x2400设备)
  381. self.d.swipe(540, 480, 540, 1920, duration=0.2)
  382. time.sleep(self.get_sleep_time())
  383. def swipe_up(self):
  384. """
  385. 上滑
  386. :return:
  387. """
  388. screen_width = self.d.info['displayWidth']
  389. screen_height = self.d.info['displayHeight']
  390. duration_rate = random.uniform(0, 0.3)
  391. self.d.swipe(screen_width // 2, screen_height - 100, screen_width // 2, 100, duration=duration_rate)
  392. no = random.uniform(0, 1)
  393. if no > 0.85:
  394. # 有的时候卡着 再稍微往上滑一点点
  395. self.d.swipe_ext("up", 0.1)
  396. time.sleep(self.get_sleep_time())
  397. def swipe_back(self, no):
  398. """
  399. 返回
  400. :param no: 回退次数
  401. :return:
  402. """
  403. if not self.distinct_target():
  404. for idx in range(no):
  405. self.d.press('back')
  406. time.sleep(self.get_sleep_time())
  407. def drug_price(self):
  408. """
  409. 获取药品价格
  410. :return:
  411. """
  412. try:
  413. xpath = '//*[@text="¥"]/following-sibling::android.widget.TextView[1]'
  414. price_str = self.d.xpath(xpath).text
  415. price = float(re.search(r'[\d\.]+', price_str).group())
  416. print(f'获取到价格:{price}')
  417. return float(price)
  418. except Exception as e:
  419. print(f'提取价格出错-->{e}')
  420. return None
  421. def drug_price_ex(self):
  422. price_str = '' # 价格初始化
  423. ext = '' # 初始化已选择的信息
  424. price = ''
  425. #这是点击进入品规的按钮
  426. button_xpath_1 = '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[2]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.view.ViewGroup[last()]'
  427. button_xpath_2 = '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[2]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.view.ViewGroup[last()]'
  428. # 调试
  429. # test_button = self.d.xpath(button_xpath_1).exists
  430. # print(test_button)
  431. # test_button_2 = self.d.xpath(button_xpath_2).exists
  432. # print(test_button_2)
  433. # time.sleep(1000)
  434. # if self.d.xpath('//*[@text="发起拼单"]').exists:
  435. # self.d.xpath('//*[@text="发起拼单"]').click()
  436. # elif self.d.xpath('//*[@text="去复诊开药"]').exists:
  437. # self.d.xpath('//*[@text="去复诊开药"]').click()
  438. if self.d.xpath(button_xpath_1).exists:
  439. self.d.xpath(button_xpath_1).click()
  440. elif self.d.xpath(button_xpath_2).exists:
  441. self.d.xpath(button_xpath_2).click()
  442. else:
  443. print("button1 and button_2 all not exist")
  444. return price, ext
  445. # 获取是已选择还是请选择
  446. # select_xpath = '//*[@resource-id="android:id/content"]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.TextView[2]'
  447. select_xpath_1 = '//*[@resource-id="android:id/content"]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.TextView[last()]'
  448. select_xpath_2 = '//*[@resource-id="android:id/content"]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.TextView[last()]'
  449. select_xpath_3 = '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.view.ViewGroup[2]/android.widget.LinearLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.view.ViewGroup[1]/android.widget.TextView[last()]'
  450. select_xpath_3_2 = '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.view.ViewGroup[2]/android.widget.LinearLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.view.ViewGroup[1]/android.widget.TextView[last()-1]'
  451. # select_xpath1 = self.d.xpath(select_xpath_1).exists
  452. # print(select_xpath1)
  453. # select_xpath2 = self.d.xpath(select_xpath_2).exists
  454. # print(select_xpath2)
  455. # select_xpath3 = self.d.xpath(select_xpath_3).exists
  456. # print(select_xpath3)
  457. # time.sleep(1000)
  458. price_xpath_1 = '//*[@resource-id="android:id/content"]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.TextView[1]'
  459. price_xpath_2 = '//*[@resource-id="android:id/content"]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.TextView[1]'
  460. price_xpath_3 = '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.view.ViewGroup[2]/android.widget.LinearLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.view.ViewGroup[1]//android.widget.TextView[1]'
  461. if self.d.xpath(select_xpath_1).exists:
  462. text1 = self.d.xpath(select_xpath_1).text
  463. print(f"select_xpath_1--text1={text1}")
  464. if '已选' in text1:
  465. if self.d.xpath(price_xpath_1).exists:
  466. price_str = self.d.xpath(price_xpath_1).text
  467. print(f"select_xpath_1--price_str-1={price_str}")
  468. else:
  469. print("select_xpath_1--price_xpath_1-1 not exist")
  470. ext = text1
  471. elif '请选择' in text1:
  472. # 需要再下面点击选择
  473. scroll_xpath_1 = '//*[@resource-id="android:id/content"]//android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.LinearLayout[last()]/android.view.ViewGroup[1]/android.view.ViewGroup[last()]'
  474. scroll_xpath_2 = ''
  475. if self.d.xpath(scroll_xpath_1).exists:
  476. self.d.xpath(scroll_xpath_1).click()
  477. time.sleep(2) # 延时2秒钟,选择了之后价格会刷新
  478. if self.d.xpath(select_xpath_1).exists:
  479. text2 = self.d.xpath(select_xpath_1).text
  480. if '已选' in text2:
  481. print(f"select_xpath_1--已选择2:text2={text2}")
  482. if self.d.xpath(price_xpath_1).exists:
  483. price_str = self.d.xpath(price_xpath_1).text
  484. print(f"select_xpath_1--price_str-2={price_str}")
  485. else:
  486. print("select_xpath_1--price_xpath_1-2 not exist")
  487. ext = text2
  488. else:
  489. print("select_xpath_1--scroll_xpath_1 not exist")
  490. elif self.d.xpath(select_xpath_2).exists:
  491. text1 = self.d.xpath(select_xpath_2).text
  492. print(f"xpath2--text1={text1}")
  493. if '已选' in text1:
  494. ext = text1
  495. if self.d.xpath(price_xpath_2).exists:
  496. price_str = self.d.xpath(price_xpath_2).text
  497. print(f"select_xpath_2--price_str-2={price_str}")
  498. else:
  499. print("select_xpath_2--price_xpath_2-1 not exist")
  500. elif '请选择' in text1:
  501. print('come in here')
  502. # 需要再下面点击选择
  503. scroll_xpath_1 = '//*[@resource-id="android:id/content"]//android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.LinearLayout[last()]/android.view.ViewGroup[1]/android.view.ViewGroup[1]'
  504. if self.d.xpath(scroll_xpath_1).exists:
  505. print("scroll_xpath_1 exists")
  506. self.d.xpath(scroll_xpath_1).click()
  507. time.sleep(2) # 延时2秒钟,选择了之后价格可能会刷新
  508. if self.d.xpath(select_xpath_2).exists:
  509. text2 = self.d.xpath(select_xpath_2).text
  510. if '已选' in text2:
  511. ext = text2
  512. print(f"select_xpath_2--已选择2:text2={text2}")
  513. if self.d.xpath(price_xpath_2).exists:
  514. price_str = self.d.xpath(price_xpath_2).text
  515. print(f"select_xpath_2--price_str-2={price_str}")
  516. else:
  517. print("select_xpath_2--price_xpath_2-2 not exist")
  518. else:
  519. print("scroll_xpath_1 not exists")
  520. else:
  521. print("not exist 请选择 or 已选")
  522. elif self.d.xpath(select_xpath_3).exists:
  523. text1 = self.d.xpath(select_xpath_3).text
  524. print(f"xpath3--text1-1={text1}")
  525. if ('请选择' not in text1) and ('已选' not in text1):
  526. text1 = self.d.xpath(select_xpath_3_2).text
  527. print(f"xpath3--text1-2={text1}")
  528. if '已选' in text1:
  529. ext = text1
  530. if self.d.xpath(price_xpath_3).exists:
  531. price_str = self.d.xpath(price_xpath_3).text
  532. print(f"select_xpath_3--price_str-3-3-1={price_str}")
  533. else:
  534. print("select_xpath_3--price_xpath_3-3-1 not exist")
  535. elif '请选择' in text1:
  536. print('come in here')
  537. # 需要再下面点击选择
  538. scroll_xpath_1 = '//*[@resource-id="android:id/content"]//android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.LinearLayout[last()]/android.view.ViewGroup[1]/android.view.ViewGroup[1]'
  539. recycler_view_xpath = '//*[@resource-id="android:id/content"]//android.support.v7.widget.RecyclerView[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[last()]/android.view.ViewGroup[1]/android.view.ViewGroup[1]'
  540. if self.d.xpath(scroll_xpath_1).exists:
  541. print("scroll_xpath_1 exists")
  542. self.d.xpath(scroll_xpath_1).click()
  543. time.sleep(2) # 延时2秒钟,选择了之后价格可能会刷新
  544. if self.d.xpath(select_xpath_3).exists:
  545. text2 = self.d.xpath(select_xpath_3).text
  546. if '已选' in text2:
  547. ext = text2
  548. print(f"select_xpath_3--已选择2:text2={text2}")
  549. if self.d.xpath(price_xpath_3).exists:
  550. price_str = self.d.xpath(price_xpath_3).text
  551. print(f"select_xpath_3--price_str-3-2={price_str}")
  552. else:
  553. print("select_xpath_3--price_xpath_3-3-2 not exist")
  554. elif self.d.xpath(recycler_view_xpath).exists:
  555. self.d.xpath(recycler_view_xpath).click()
  556. time.sleep(2) # 延时2秒钟,选择了之后价格可能会刷新
  557. if self.d.xpath(select_xpath_3).exists:
  558. text2 = self.d.xpath(select_xpath_3).text
  559. if '已选' in text2:
  560. ext = text2
  561. print(f"select_xpath_3--已选择2:text2={text2}")
  562. if self.d.xpath(price_xpath_3).exists:
  563. price_str = self.d.xpath(price_xpath_3).text
  564. print(f"select_xpath_3--price_str-3-3={price_str}")
  565. else:
  566. print("select_xpath_3--price_xpath_3-3-3 not exist")
  567. else:
  568. print("scroll_xpath_1 not exists")
  569. else:
  570. print(f"xpath3--text1-不包含请选择和已选择")
  571. else:
  572. print("select_xpath_1 and select_xpath_2 and select_xpath_3 all not exist")
  573. if price_str:
  574. # price = float(re.search('[\d\.]+', price_str).group())
  575. match = re.search(r'¥([\d\.]+)', price_str)
  576. if match:
  577. price = float(match.group(1))
  578. else:
  579. price = ''
  580. # price = float(re.search(r'¥([\d\.]+)', price_str).group(1))
  581. print(f'获取到价格:{price}')
  582. print(f"ext={ext}")
  583. self.swipe_back(1) #
  584. # time.sleep(1000)
  585. return price, ext
  586. def restart_uiautomator_services(self, device_id):
  587. """
  588. 重启atx的uiautomator 服务
  589. :param device_id:
  590. :return:
  591. """
  592. stop_uiautomator_services = f'adb -s {device_id} shell /data/local/tmp/atx-agent server -d --stop'
  593. start_uiautomator_services = f'adb -s {device_id} shell /data/local/tmp/atx-agent server -d'
  594. # result = subprocess.run(stop_uiautomator_services, capture_output=True, text=True, shell=True)
  595. # print(result.stdout)
  596. subprocess.run(stop_uiautomator_services, capture_output=True, text=True, shell=True)
  597. time.sleep(self.get_sleep_time())
  598. subprocess.run(start_uiautomator_services, capture_output=True, text=True, shell=True)
  599. time.sleep(self.get_sleep_time())
  600. def connect_devices(self, device_id):
  601. """
  602. 连接设备
  603. :return:
  604. """
  605. try:
  606. self.d = u2.connect_usb(device_id)
  607. # 设置隐形等待时间
  608. # self.d.implicitly_wait(5)
  609. self.restart_uiautomator_services(device_id)
  610. print(f'[{self.program_start_time}]连接到设备:{device_id}')
  611. except Exception as e:
  612. print(f'{device_id} 连接错误: {e}')
  613. raise Exception(e)
  614. def get_ocr_res(self, img):
  615. try:
  616. image = self.remove_watermark(img)
  617. # image_file = open(img,'wb')
  618. # image_file.write(image)
  619. # res_image = self.client.basicAccurate(image) # 高精度
  620. res_image = self.client.basicGeneral(image)
  621. # print(f'百度api返回结果:{res_image}')
  622. # print(res_image.get('words_result', ''))
  623. # new_dic = dict()
  624. data = res_image.get('words_result', '')
  625. print(f'百度api返回结果:{data}')
  626. # full_text = ';'.join(item['words'] for item in data)
  627. # address = ''
  628. # for item in data:
  629. # if '企业注册号' in item['words']:
  630. # print('come in 111')
  631. # reg_number = item['words'].split(':', 1)[1].strip()
  632. # elif '企业名称' in item['words']:
  633. # print('come in 222')
  634. # company_name = item['words'].split(':', 1)[1].strip()
  635. # elif '所:' in item['words']:
  636. # print('come in 333')
  637. # address = item['words'].split(':', 1)[1].strip()
  638. # # 输出结果
  639. # print("企业注册号:", reg_number)
  640. # print("企业名称:", company_name)
  641. # print("住所:", address)
  642. return data
  643. except:
  644. return None
  645. def screenshot_the_business_license(self):
  646. screenshot_path = 'screenshot1.png'
  647. self.d.screenshot(screenshot_path)
  648. img = cv2.imread(screenshot_path)
  649. # 指定裁剪区域 (left, top, right, bottom)
  650. left = 0
  651. top = 480
  652. right = 720
  653. bottom = 1420
  654. cropped_img = img[top:bottom, left:right]
  655. cropped_screenshot_path = 'cropped_screenshot.png'
  656. cv2.imwrite(cropped_screenshot_path, cropped_img)
  657. def screenshot_the_shop_qualifications(self):
  658. screenshot_path = 'screenshot2.png'
  659. self.d.screenshot(screenshot_path)
  660. img = cv2.imread(screenshot_path)
  661. # 指定裁剪区域 (left, top, right, bottom)
  662. left = 0
  663. top = 480
  664. right = 720
  665. bottom = 1420
  666. def first_screenshot_the_verify(self):
  667. screenshot_verify_path = 'first_screenshot_verify.png'
  668. self.d.screenshot(screenshot_verify_path)
  669. img = cv2.imread(screenshot_verify_path)
  670. ocr_res = self.get_ocr_res('first_screenshot_verify.png')
  671. event = '' # 事件类型:滑块验证、计算结果输入
  672. if ocr_res:
  673. for item in ocr_res:
  674. if '拖动滑块完成' in item['words']:
  675. print("滑块验证")
  676. event = '滑块验证'
  677. break
  678. # 指定裁剪区域 (left, top, right, bottom)
  679. if event == '滑块验证':
  680. left = 118
  681. top = 478
  682. right = 602
  683. bottom = 722
  684. else:
  685. left = 118
  686. top = 478
  687. right = 602
  688. bottom = 722
  689. cropped_verify_img = img[top:bottom, left:right]
  690. cropped_verify_creenshot_path = 'first_cropped_verify_screenshot.png'
  691. cv2.imwrite(cropped_verify_creenshot_path, cropped_verify_img)
  692. return event
  693. def slide_second_screenshot_the_verify(self):
  694. screenshot_verify_path = 'slide_second_screenshot_verify.png'
  695. self.d.screenshot(screenshot_verify_path)
  696. img = cv2.imread(screenshot_verify_path)
  697. left = 118
  698. top = 478
  699. right = 602
  700. bottom = 722
  701. cropped_verify_img = img[top:bottom, left:right]
  702. cropped_verify_creenshot_path = 'second_slide_cropped_verify_screenshot.png'
  703. cv2.imwrite(cropped_verify_creenshot_path, cropped_verify_img)
  704. def second_screenshot_the_verify(self):
  705. screenshot_verify_path = 'second_screenshot_verify.png'
  706. self.d.screenshot(screenshot_verify_path)
  707. img = cv2.imread(screenshot_verify_path)
  708. ocr_res = self.get_ocr_res('second_screenshot_verify.png')
  709. print(f'second_ocr_res:{ocr_res}')
  710. result = ''
  711. if ocr_res:
  712. result = '验证成功'
  713. for item in ocr_res:
  714. if '验证成功' in item['words']:
  715. result = '验证成功'
  716. break
  717. elif '验证不成功' in item['words']:
  718. result = '验证不成功'
  719. break
  720. return result
  721. def screenshot_business_license(self, shop_name):
  722. screenshot_lincense_path = 'license_screenshot.png'
  723. self.d.screenshot(screenshot_lincense_path)
  724. img = cv2.imread(screenshot_lincense_path)
  725. # 裁剪
  726. left = 0
  727. top = 160
  728. right = 720
  729. bottom = 1000
  730. cropped_verify_img = img[top:bottom, left:right]
  731. cropped_screenshot_path = 'D:\\work\\dfwy_spider\\drug_data\\pdd\\screenshot\\' + shop_name + '.png'
  732. cv2.imwrite(cropped_screenshot_path, cropped_verify_img)
  733. return cropped_screenshot_path
  734. def drug_slide(self, distance):
  735. print(f"滑动的distance= {distance}")
  736. # 获取设备上所有窗口的层次结构
  737. dump = self.d.dump_hierarchy()
  738. print(f"drug_slide-111:{dump}")
  739. # 解析 JSON 数据
  740. root = ET.fromstring(dump)
  741. # dump_data = json.loads(dump)
  742. print("drug_slide-222")
  743. # 查找包含 meco.webkit.WebView 的元素
  744. webview_elements = root.findall(".//node[@class='meco.webkit.WebView']")
  745. print("drug_slide-333")
  746. if webview_elements:
  747. print("找到 WebView 元素:", webview_elements[0].attrib)
  748. # 获取WebView的bounds信息
  749. webview_bounds = webview_elements[0].attrib['bounds']
  750. print("WebView bounds:", webview_bounds)
  751. # 假设你需要操作的元素在WebView中的XPath为'//node[@class="meco.webkit.WebView"]'
  752. webview_element_xpath = '//node[@class="meco.webkit.WebView"]'
  753. # 获取WebView内部元素
  754. webview_inner_elements = self.d.xpath(webview_element_xpath)
  755. if webview_inner_elements:
  756. # 拖动元素300像素
  757. start_x = webview_inner_elements[0].info['bounds'][0]
  758. start_y = webview_inner_elements[0].info['bounds'][1]
  759. end_x = start_x + distance
  760. end_y = start_y
  761. self.d.swipe(start_x, start_y, end_x, end_y)
  762. print("拖动成功")
  763. else:
  764. print("未找到需要拖动的元素")
  765. else:
  766. print("未找到 WebView 元素")
  767. def get_title(self):
  768. try:
  769. print('开始提取标题')
  770. time.sleep(self.get_sleep_time())
  771. title_xpath = '//*[@resource-id="com.xunmeng.pinduoduo:id/tv_title"]'
  772. if self.d.xpath(title_xpath).exists:
  773. title = self.d.xpath(title_xpath).info['contentDescription'].strip()
  774. else:
  775. return None
  776. # title = self.d.xpath('//*[@resource-id="com.xunmeng.pinduoduo:id/tv_title"]').info['contentDescription'].strip()
  777. print(f'提取到标题:{title}')
  778. return title
  779. except Exception as e:
  780. print(f'获取标题出错:{e}')
  781. return None
  782. # 从里面匹配出药品名和规格
  783. # drugs_name
  784. # specifications
  785. # match = re.search(r'([^\d]+)([\d\D]+)', title)
  786. # match = re.search(r'(\[[^\]]+\])(.+?)(\d+.*)', title)
  787. # if match:
  788. # drugs_name = match.group(1).strip() + match.group(2).strip()
  789. # specifications = match.group(3).strip()
  790. # print("药品名:", drugs_name)
  791. # print("规格:", specifications)
  792. # print('完整药名:', drugs_name + specifications)
  793. # return drugs_name, specifications
  794. # else:
  795. # print("没有匹配到预期格式")
  796. def enter_shop(self):
  797. """
  798. 进店,方便提取资质环境
  799. :return:
  800. """
  801. # self.d.xpath('//*[@text="进店"]').click()
  802. self.d.xpath('//*[@text="店铺"]').click()
  803. time.sleep(self.get_sleep_time())
  804. def enter_shoper(self):
  805. """
  806. 进入商家
  807. :return:
  808. """
  809. self.d.xpath('//*[@text="商家"]').click()
  810. time.sleep(self.get_sleep_time())
  811. def scan_shoper_license(self):
  812. self.d.xpath('//*[@text="查看商家资质"]').click()
  813. time.sleep(self.get_sleep_time())
  814. def data_is_exists(self, data):
  815. # 1. 验证必要字段
  816. required_keys = ['search_key', 'min_price', 'shop', 'scrape_date', 'platform']
  817. if not all(key in data for key in required_keys):
  818. missing = [key for key in required_keys if key not in data]
  819. print(f"缺少必要字段: {', '.join(missing)}")
  820. return None
  821. try:
  822. # 连接数据库
  823. conn = get_mysql()
  824. # 创建游标对象
  825. cur = conn.cursor()
  826. # 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']}'"
  827. # cur.execute(query_sql)
  828. query_sql = """
  829. SELECT * FROM {}
  830. WHERE search_key = %s
  831. AND min_price = %s
  832. AND shop = %s
  833. AND scrape_date = %s
  834. AND platform = %s
  835. """.format(self.table_name)
  836. cur.execute(query_sql, (
  837. data['search_key'],
  838. data['min_price'],
  839. data['shop'],
  840. data['scrape_date'],
  841. data['platform']
  842. ))
  843. result = cur.fetchone()
  844. return bool(result) # 如果存在返回True,否则False
  845. except Exception as e:
  846. print(f"MySQL 错误: {str(e)}")
  847. # 验证店铺信息是否在数据库中已存在
  848. def shop_is_exists_database(self, shop):
  849. try:
  850. # 连接数据库
  851. conn = get_mysql()
  852. # 创建游标对象
  853. cur = conn.cursor()
  854. query_sql = """
  855. SELECT * FROM {}
  856. WHERE shop = %s
  857. """.format(self.shop_table_name)
  858. cur.execute(query_sql, (
  859. shop
  860. ))
  861. result = cur.fetchone()
  862. return bool(result) # 如果存在返回True,否则False
  863. except Exception as e:
  864. print(f"MySQL 错误: {str(e)}")
  865. def get_instructions_data(self):
  866. """
  867. 确定有详情页之后之后,提取所有的详情页数据
  868. :return:
  869. """
  870. # 下面的for循环已经有滑动的操作了,不要一进来就滑动。
  871. # self.d.swipe_ext("up", scale=0.5)
  872. for i in range(8):
  873. # if self.d(textStartsWith="查看全部").exists:
  874. if self.d.xpath('//*[@text="品牌"]').exists or self.d.xpath('//*[@text="药品通用名"]').exists:
  875. self.d.swipe_ext("up", scale=0.1)
  876. print('开始采集详情数据')
  877. break
  878. self.d.swipe_ext("up", scale=0.5)
  879. time.sleep(self.get_sleep_time())
  880. # 点击查看全部
  881. if self.d.xpath('//*[@text="品牌"]').exists:
  882. self.d.xpath('//*[@text="品牌"]').click()
  883. else:
  884. self.d.xpath('//*[@text="药品通用名"]').click()
  885. time.sleep(self.get_sleep_time())
  886. attr = dict()
  887. # # 获取详情页信息
  888. xpath = '//*[starts-with(@text,"商品参数")]/parent::*/parent::*/following-sibling::*/*/*/android.view.ViewGroup//android.widget.TextView'
  889. ddd = self.d.xpath(xpath).all()
  890. for i in range(0, len(ddd), 2):
  891. group = ddd[i:i + 2]
  892. attr[group[0].text] = group[1].text
  893. if not all(i in ['有效期', '生产企业', '批准文号', '药品规格'] for i in attr.keys()):
  894. self.d.swipe_ext("up", 0.4)
  895. time.sleep(self.get_sleep_time())
  896. xpath = '//*[starts-with(@text,"商品参数")]/parent::*/parent::*/following-sibling::*/*/*/android.view.ViewGroup//android.widget.TextView'
  897. ddd = self.d.xpath(xpath).all()
  898. for i in range(0, len(ddd), 2):
  899. group = ddd[i:i + 2]
  900. attr[group[0].text] = group[1].text
  901. print(f'当前说明书规格参数:{attr}')
  902. res_data = {
  903. "有效期": attr.get('有效期', ''),
  904. "生产单位": attr.get('生产企业', ''),
  905. "批准文号": attr.get('批准文号', ''),
  906. "产品规格": attr.get('药品规格', '')
  907. }
  908. print(f'当前规格参数字典数据:{res_data}')
  909. return res_data
  910. def has_instructions(self):
  911. """
  912. 是否有详情页
  913. :return:如果有详情页返回True,否则返回False
  914. """
  915. # 没有说明书的无法采集具体数据
  916. max_attempts = 12 # 最大尝试次数
  917. attempt = 0 # 当前尝试次数
  918. while attempt < max_attempts:
  919. time.sleep(0.5)
  920. xpath = '//*[@text="商品详情"]'
  921. is_has_instructions = self.d.xpath(xpath).exists
  922. if is_has_instructions:
  923. return True # 如果找到“商品详情”,则返回True
  924. self.d.swipe_ext("up", 0.3)
  925. attempt += 1
  926. return False # 如果尝试次数达到最大次数,则返回False
  927. # time.sleep(self.get_sleep_time())
  928. # xpath = '//*[@text="商品详情"]'
  929. # is_has_instructions = self.d.xpath(xpath).exists
  930. # return
  931. def has_shop_qualifications(self):
  932. max_attempts = 3 # 最大尝试次数
  933. attempt = 0 # 当前尝试次数
  934. while attempt < max_attempts:
  935. time.sleep(0.5)
  936. xpath = '//*[@text="查看全部"]'
  937. is_has_search_all = self.d.xpath(xpath).exists
  938. if is_has_search_all:
  939. elements = self.d.xpath(xpath).all()
  940. count = len(elements)
  941. print(f"页面上共有 {count} 个 '查看全部' 元素")
  942. if count >= 2:
  943. second_element = elements[1]
  944. bounds = second_element.bounds
  945. else:
  946. element = self.d.xpath(xpath).get()
  947. bounds = element.bounds
  948. print(f'bounds:{bounds}')
  949. # time.sleep(1000000)
  950. # 获取元素的坐标和尺寸
  951. x1, y1, x2, y2 = bounds # x1, y1 是左上角坐标,x2, y2 是右下角坐标
  952. element_x = (x1 + x2) / 2 # 元素的中心 x 坐标
  953. element_y = (y1 + y2) / 2 # 元素的中心 y 坐标
  954. if element_y > 500:
  955. self.d.swipe(element_x, element_y, element_x, 500, 1)
  956. # 画完之后再去一次坐标
  957. elements_ex = self.d.xpath(xpath).all()
  958. count_ex = len(elements_ex)
  959. print(f"第二次页面上共有 {count_ex} 个 '查看全部' 元素")
  960. if count_ex >= 2:
  961. second_element_ex = elements[1]
  962. bounds_ex = second_element_ex.bounds
  963. else:
  964. element_ex = self.d.xpath(xpath).get()
  965. bounds_ex = element_ex.bounds
  966. print(f'bounds_ex:{bounds_ex}')
  967. x1, y1, x2, y2 = bounds_ex # x1, y1 是左上角坐标,x2, y2 是右下角坐标
  968. element_x = (x1 + x2) / 2 # 元素的中心 x 坐标
  969. element_y = (y1 + y2) / 2 # 元素的中心 y 坐标
  970. time.sleep(self.get_sleep_time())
  971. target_x = element_x
  972. target_y = element_y + 80
  973. print(f'目标坐标:{target_x}, {target_y}')
  974. # 点击图片
  975. self.d.click(target_x, target_y)
  976. time.sleep(self.get_sleep_time())
  977. # 获取图片内容
  978. self.screenshot_the_shop_qualifications()
  979. ocr_res = self.get_ocr_res('screenshot2.png')
  980. print(f'ocr_res:{ocr_res}')
  981. time.sleep(100000)
  982. return True # 如果找到“商品详情”,则返回True
  983. self.d.swipe_ext("up", 0.1)
  984. attempt += 1
  985. return False # 如果尝试次数达到最大次数,则返回False
  986. def get_license_info_ex(self, shop_name):
  987. # print('开始获取商家资质信息')
  988. self.enter_shop() # 点击店铺
  989. # 点击店铺图片
  990. xpath_shop_image = '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.RelativeLayout[1]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ImageView[1]'
  991. if self.d.xpath(xpath_shop_image).exists:
  992. self.d.xpath(xpath_shop_image).click()
  993. time.sleep(self.get_sleep_time())
  994. # 从弹窗页获取店铺资质的位置
  995. for i in range(10):
  996. if self.d.xpath('//*[@text="店铺资质"]').exists:
  997. print('店铺资质存在1')
  998. break
  999. self.d.swipe_ext('up', 0.3)
  1000. time.sleep(1)
  1001. if self.d.xpath('//*[@text="店铺资质"]').exists:
  1002. print('店铺资质存在2')
  1003. break
  1004. if self.d.xpath('//*[@text="已上传"]').exists:
  1005. self.d.xpath('//*[@text="已上传"]').click()
  1006. else:
  1007. self.d.xpath('//*[@text="店铺资质"]').click()
  1008. time.sleep(self.get_sleep_time())
  1009. xpath_pop_window = '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/meco.webkit.WebView[1]'
  1010. # 等待验证弹窗出现
  1011. for i in range(10):
  1012. if self.d.xpath(xpath_pop_window).exists:
  1013. print(f'第{i}次安全验证弹窗存在')
  1014. break
  1015. else:
  1016. print(f'第{i}次安全验证弹窗不存在')
  1017. time.sleep(self.get_sleep_time())
  1018. time.sleep(10)
  1019. # 第一次截屏
  1020. event = self.first_screenshot_the_verify()
  1021. # 用ocr的方式识别当前是拖动还是点击
  1022. # ocr_res = self.get_ocr_res('screenshot_verify.png')
  1023. # event = ''
  1024. # if ocr_res:
  1025. # for item in ocr_res:
  1026. # if '拖动滑块完成' in item['words']:
  1027. # print("滑块验证")
  1028. # event = '滑块验证'
  1029. # break
  1030. # print("ocr_res end")
  1031. if event == '滑块验证':
  1032. # 需要点击》按钮不懂后截屏,获取
  1033. self.d.touch.down(110, 780)
  1034. time.sleep(0.5)
  1035. # 截屏
  1036. event = self.slide_second_screenshot_the_verify()
  1037. # result = slide_verify('first_cropped_verify_screenshot.png') second_slide_cropped_verify_screenshot
  1038. result = slide_verify('second_slide_cropped_verify_screenshot.png')
  1039. result = int(result)
  1040. print(f'滑动距离:{result}')
  1041. print('开始滑动')
  1042. # internel = 1000
  1043. # self.d.touch.move_to(110 + result, 780, duration=internel)
  1044. # self.d.touch.up(110 + result, 780)
  1045. self.human_slide(110, 780, 110 + result, 780)
  1046. print('滑动结束')
  1047. time.sleep(self.get_sleep_time())
  1048. # 滑完之后怎么判断是否验证成功?
  1049. second_resut = self.second_screenshot_the_verify()
  1050. if second_resut == '验证成功':
  1051. time.sleep(8)
  1052. cropped_screenshot_path = self.screenshot_business_license(shop_name)
  1053. ocr_res = self.get_ocr_res(cropped_screenshot_path)
  1054. print(f'ocr_res:{ocr_res}')
  1055. company_name = ''
  1056. reg_number = ''
  1057. address = ''
  1058. if ocr_res:
  1059. for item in ocr_res:
  1060. if '企业注册号' in item['words']:
  1061. # print('come in 111')
  1062. reg_number = item['words'].split(':', 1)[1].strip()
  1063. elif '企业名称' in item['words']:
  1064. # print('come in 222')
  1065. company_name = item['words'].split(':', 1)[1].strip()
  1066. elif '所:' in item['words']:
  1067. # print('come in 333')
  1068. address = item['words'].split(':', 1)[1].strip()
  1069. # 输出结果
  1070. print("企业注册号:", reg_number)
  1071. print("企业名称:", company_name)
  1072. print("住所:", address)
  1073. print("yanzhenghcenggong")
  1074. # 截取图片保存 第三次截屏,保存到本地
  1075. # 将图片传给第三方verify接口得到需要移动的距离
  1076. # result = slide_verify('cropped_verify_screenshot.png')
  1077. # print(f'滑动距离:{result}')
  1078. # self.drug_slide(340)
  1079. # print('开始滑动')
  1080. # self.d.swipe(120, 760, 460, 760, 0.3)
  1081. # print('滑动结束')
  1082. # time.sleep(1000000)
  1083. # 截图获取需要验证的内容
  1084. def get_license_info(self):
  1085. self.enter_shop()
  1086. self.enter_shoper()
  1087. self.scan_shoper_license()
  1088. # 获取资质编码
  1089. qualification_number = self.get_qualification_number()
  1090. if qualification_number:
  1091. table_license_info = self.get_table_license_info(qualification_number)
  1092. if table_license_info:
  1093. return {
  1094. '单位名称': table_license_info[0],
  1095. '地址': table_license_info[1],
  1096. '社会信用代码': table_license_info[2]
  1097. }
  1098. else:
  1099. # operate_no = random.randint(0, 1)
  1100. self.d.click(0.603, 0.27)
  1101. # if operate_no == 0:
  1102. # self.d.xpath('//*[@text="营业执照"]').click()
  1103. # else:
  1104. # self.d.click(0.603, 0.27)
  1105. time.sleep(self.get_sleep_time())
  1106. self.screenshot_the_business_license()
  1107. ocr_res = self.get_ocr_res('cropped_screenshot.png')
  1108. return ocr_res
  1109. # operate_no = random.randint(0, 1)
  1110. self.d.click(0.603, 0.27)
  1111. # if operate_no == 0:
  1112. # self.d.xpath('//*[@text="营业执照"]').click()
  1113. # else:
  1114. # self.d.click(0.603, 0.27)
  1115. time.sleep(self.get_sleep_time())
  1116. self.screenshot_the_business_license()
  1117. ocr_res = self.get_ocr_res('cropped_screenshot.png')
  1118. return ocr_res
  1119. def distinct_target(self):
  1120. result = False
  1121. is_position = self.d.xpath('//*[@content-desc="拍照搜索"]').exists
  1122. is_position2 = self.d.xpath('//*[@text="年货节大促"]').exists
  1123. is_position3 = self.d.xpath('//*[@text="筛选"]').exists
  1124. is_position4 = self.d.xpath('//*[@text="回头客常拼"]').exists
  1125. list_page_xpath = '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[2]/android.view.ViewGroup[1]/android.widget.LinearLayout[1]//android.support.v7.widget.RecyclerView[1]'
  1126. is_position_new = self.d.xpath(list_page_xpath).exists
  1127. print(f'is_position_new={is_position_new}')
  1128. if is_position or is_position2 or is_position3 or is_position4 or is_position_new:
  1129. result = True
  1130. return result
  1131. def enter_target_page(self):
  1132. self.d.xpath(
  1133. '//*[@resource-id="android:id/content"]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]').click()
  1134. time.sleep(self.get_sleep_time())
  1135. self.d(className='android.widget.EditText').click()
  1136. time.sleep(self.get_sleep_time())
  1137. self.d.send_keys(self.search_key, clear=True)
  1138. time.sleep(self.get_sleep_time())
  1139. self.d.xpath('//*[@text="搜索"]').click()
  1140. time.sleep(self.get_sleep_time())
  1141. """暂不用该功能
  1142. def get_table_license_info(self, qualification_number):
  1143. try:
  1144. sql = f'select business_license_company,city,credit_code from mt_drug where credit_code = "{qualification_number}"'
  1145. self.mysql_client.cur.execute(sql)
  1146. res = self.mysql_client.cur.fetchone()
  1147. return res
  1148. except:
  1149. return None
  1150. """
  1151. def get_clipboard(self):
  1152. self.loggerPdd.info(f"Clipboard content:{self.d.clipboard}") # 打印调试信息
  1153. clipboard_content = self.d.clipboard
  1154. if clipboard_content is None:
  1155. return ''
  1156. return clipboard_content.strip()
  1157. def clear_clipboard(self):
  1158. self.d.set_clipboard("")
  1159. def get_product_link(self):
  1160. product_link = ''
  1161. print('开始获取商品链接')
  1162. content_frame = self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]').exists
  1163. print(content_frame)
  1164. relative_layout = self.d.xpath(
  1165. '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]').exists
  1166. print(relative_layout)
  1167. relative_layout2 = self.d.xpath(
  1168. '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]').exists
  1169. print(relative_layout2)
  1170. Frame_Layout = self.d.xpath(
  1171. '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[2]').exists
  1172. print(Frame_Layout)
  1173. ImageView = self.d.xpath(
  1174. '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[2]/android.view.View[1]').exists
  1175. print(ImageView)
  1176. ImageView2 = self.d.xpath(
  1177. '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[3]/android.view.View[1]').exists
  1178. print(ImageView2)
  1179. # time.sleep(10000)
  1180. '''
  1181. if self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[2]/android.view.View[1]').exists:
  1182. self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[2]/android.view.View[1]').click()
  1183. print('进入分享成功')
  1184. time.sleep(1)
  1185. #先清除剪切板的数据
  1186. # self.clear_clipboard()
  1187. #需要增加从右往左的滑动
  1188. # self.d.swipe_ext("left", 0.5)
  1189. print('开始滑动')
  1190. self.d.swipe(400, 1250, 100, 1250, 0.2)
  1191. time.sleep(0.2)
  1192. if self.d.xpath('//*[@text="复制链接"]').exists:
  1193. self.d.xpath('//*[@text="复制链接"]').click()
  1194. print('点击复制链接')
  1195. # self.d.xpath('//*[@text="复制链接"]').click_exists()
  1196. # self.d.xpath('//*[contains(@text="复")]').click_exists()
  1197. # time.sleep(1)
  1198. product_link = self.get_clipboard()
  1199. time.sleep(0.5)
  1200. print(f'商品链接:{product_link}')
  1201. elif self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[3]/android.view.View[1]').exists:
  1202. self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[3]/android.view.View[1]').click()
  1203. print('进入分享成功111')
  1204. time.sleep(1)
  1205. print('开始滑动')
  1206. self.d.swipe(400, 1250, 100, 1250, 0.2)
  1207. time.sleep(0.2)
  1208. if self.d.xpath('//*[@text="复制链接"]').exists:
  1209. self.d.xpath('//*[@text="复制链接"]').click()
  1210. print('点击复制链接')
  1211. product_link = self.get_clipboard()
  1212. time.sleep(0.5)
  1213. print(f'商品链接:{product_link}')
  1214. else:
  1215. print('进入分享失败')
  1216. time.sleep(10000)
  1217. '''
  1218. # 多种可能的“分享”按钮
  1219. dots_xpaths = [
  1220. # '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[2]/android.view.View[1]',
  1221. '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[last()]/android.view.View[1]',
  1222. # '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[2]/android.view.View[1]',
  1223. # '//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[2]/android.widget.FrameLayout[3]/android.widget.ImageView[1]',
  1224. ]
  1225. max_retry = 5 # 最多尝试次数
  1226. for idx in range(1, max_retry + 1):
  1227. if product_link: # 已经拿到则退出
  1228. break
  1229. for xp in dots_xpaths:
  1230. if self.d.xpath(xp).exists:
  1231. # print(f'{idx}-进入分享点点点')
  1232. self.loggerPdd.info(f'{idx}-进入分享点点点')
  1233. self.d.xpath(xp).click()
  1234. time.sleep(1)
  1235. # self.d.xpath('//*[@text="分享商品"]').click_exists()
  1236. # time.sleep(0.2)
  1237. # print('开始滑动')
  1238. self.loggerPdd.info('开始滑动')
  1239. if (
  1240. self.device_id == '2e58510' or self.device_id == 'fcb3c749' or self.device_id == 'ea4e4eb8' or self.device_id == '95b2c764' or
  1241. self.device_id == '369dcf96' or self.device_id == 'ZDQWUSSWBEDI896T' or self.device_id == 'IRLZAAQCDMHYWKTS' or
  1242. self.device_id == 'U47HZDRG8XJBBURW' or self.device_id == 'WWRO9LTGG6KFGQCM' or self.device_id == 'fcb3c749' or
  1243. self.device_id == 'OVFETO8PCY45E6A6' or self.device_id == 'U8ONIJJJS4CELVD6'):
  1244. self.d.swipe(400, 1350, 100, 1350, 0.3)
  1245. elif (
  1246. self.device_id == 'KNNNEMNVWCJZQOLZ' or self.device_id == 'CMKFUSSG99ROR489' or self.device_id == '656DTOPRZDEALZ5X' or
  1247. self.device_id == 'UCQGF6CQFMU8WKHI' or self.device_id == '4TZDUGTOAIFMJVGU' or self.device_id == 'DEZXWKUC7DJBLVPJ' or
  1248. self.device_id == 'GQIRKB7LVOONM7VW'):
  1249. self.d.swipe(400, 1300, 100, 1300, 0.3)
  1250. elif self.device_id == 'e2899b34':
  1251. self.d.swipe(400, 1150, 100, 1150, 0.3)
  1252. else:
  1253. self.d.swipe(400, 1250, 100, 1250, 0.3)
  1254. # self.d.swipe(400, 1250, 100, 1250, 0.3)
  1255. # self.d.drag(300, 1280, 50, 1280, 0.3)
  1256. # self.d.swipe_ext('left', 0.3)
  1257. time.sleep(0.2)
  1258. self.d.xpath('//*[@text="复制链接"]').click_exists()
  1259. time.sleep(1)
  1260. product_link = self.get_clipboard()
  1261. time.sleep(0.5)
  1262. # print(f'{idx}-商品链接:{product_link}')
  1263. self.loggerPdd.info(f'{idx}-商品链接:{product_link}')
  1264. break # 找到并执行后跳出内层循环
  1265. # if self.d.xpath('//*[@text="复制链接"]').exists:
  1266. # self.d.xpath('//*[@text="复制链接"]').click()
  1267. # # print('点击复制链接')
  1268. # self.loggerPdd.info('点击复制链接')
  1269. # product_link = self.get_clipboard()
  1270. # time.sleep(0.5)
  1271. # # print(f'{idx}-商品链接:{product_link}')
  1272. # self.loggerPdd.info(f'{idx}-商品链接:{product_link}')
  1273. # break # 找到并执行后跳出内层循环
  1274. if not product_link and idx < max_retry:
  1275. time.sleep(0.5) # 最后一次不需要再等待
  1276. # time.sleep(100000)
  1277. return product_link
  1278. def integrate_data(self):
  1279. """
  1280. 整合数据
  1281. :return:
  1282. """
  1283. # 测试通过点击店铺获取店铺的名称:
  1284. # shop = self.get_shop_name()
  1285. # print(f'店铺名称:{shop}')
  1286. # time.sleep(100000)
  1287. # 测试点击店铺进入获取店铺资质
  1288. # license_info = self.get_license_info_ex('1234')#店铺名称
  1289. # time.sleep(100000)
  1290. # 首先判断是否存在:商品已售罄,推荐以下相似商品 的文本
  1291. # if self.d.xpath('//*[contains(@text, "商品已售罄,推荐以下相似商品")]'):
  1292. # self.loggerPdd.info(f'商品已售罄:{self.search_key}')
  1293. # self.swipe_back(1)
  1294. # return
  1295. # 获取价格和盒数备注
  1296. min_price, ext = self.drug_price_ex() # 最低价格和盒数备注
  1297. title_info = self.get_title() # 药品名字
  1298. if title_info:
  1299. if '999' in self.search_key:
  1300. temp_search_key = self.search_key.replace('999', '')
  1301. if self.search_key == '999强力枇杷露225ml':
  1302. temp_search_key = temp_search_key.replace('225ml', '')
  1303. elif self.search_key == '999糠酸莫米松凝胶15':
  1304. temp_search_key = temp_search_key.replace('15', '')
  1305. elif self.search_key == '999养胃舒颗粒10g*6':
  1306. temp_search_key = temp_search_key.replace('10g*6', '')
  1307. elif self.search_key == '999曲安奈德益康唑乳膏30g':
  1308. temp_search_key = temp_search_key.replace('30g', '')
  1309. elif self.search_key == '999抗病毒口服液10ml*6支':
  1310. temp_search_key = temp_search_key.replace('10ml*6支', '')
  1311. elif self.search_key == '999复方板蓝根颗粒15袋':
  1312. temp_search_key = temp_search_key.replace('15袋', '')
  1313. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  1314. temp_search_key = temp_search_key.replace('50', '')
  1315. elif self.search_key == '999维生素E.C颗粒9袋':
  1316. temp_search_key = temp_search_key.replace('9袋', '')
  1317. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  1318. temp_search_key = temp_search_key.replace('6粒', '')
  1319. elif self.search_key == '999复方板蓝根颗粒15g*15袋':
  1320. temp_search_key = temp_search_key.replace('15g*15袋', '')
  1321. elif self.search_key == '999止泻利颗粒15g*8':
  1322. temp_search_key = temp_search_key.replace('15g*8', '')
  1323. elif self.search_key == '999三蛇胆川贝膏138':
  1324. temp_search_key = temp_search_key.replace('138', '')
  1325. elif self.search_key == '999强力枇杷露16袋':
  1326. temp_search_key = temp_search_key.replace('16袋', '')
  1327. elif self.search_key == '999复方苦参肠炎康片12片':
  1328. temp_search_key = temp_search_key.replace('12片', '')
  1329. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15':
  1330. temp_search_key = temp_search_key.replace('必无忧', '')
  1331. temp_search_key = temp_search_key.replace('15', '')
  1332. elif self.search_key == '999速复康布洛芬缓释胶囊':
  1333. temp_search_key = temp_search_key.replace('速复康', '')
  1334. elif self.search_key == '999强力枇杷露120ml':
  1335. temp_search_key = temp_search_key.replace('120ml', '')
  1336. elif self.search_key == '999强力枇杷露150ml':
  1337. temp_search_key = temp_search_key.replace('150ml', '')
  1338. elif self.search_key == '999抗病毒口服液10ml*10':
  1339. temp_search_key = temp_search_key.replace('10ml*10', '')
  1340. elif self.search_key == '999抗病毒口服液10ml*12':
  1341. temp_search_key = temp_search_key.replace('10ml*12', '')
  1342. elif self.search_key == '999感冒清热颗粒6g*10':
  1343. temp_search_key = temp_search_key.replace('6g*10', '')
  1344. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  1345. temp_search_key = temp_search_key.replace('选平', '')
  1346. temp_search_key = temp_search_key.replace('20g', '')
  1347. elif self.search_key == '999糠酸莫米松乳膏10g':
  1348. temp_search_key = temp_search_key.replace('10g', '')
  1349. elif self.search_key == '999壮骨关节丸6g*20':
  1350. temp_search_key = temp_search_key.replace('6g*20', '')
  1351. elif self.search_key == '999正天丸6g*15':
  1352. temp_search_key = temp_search_key.replace('6g*15', '')
  1353. elif self.search_key == '999藿香正气合剂10ml*6':
  1354. temp_search_key = temp_search_key.replace('10ml*6', '')
  1355. elif self.search_key == '999藿香正气合剂10ml*10':
  1356. temp_search_key = temp_search_key.replace('10ml*10', '')
  1357. elif self.search_key == '999小儿止咳糖浆120':
  1358. temp_search_key = temp_search_key.replace('120', '')
  1359. elif self.search_key == '999小儿止咳糖浆225':
  1360. temp_search_key = temp_search_key.replace('225', '')
  1361. elif self.search_key == '999小儿感冒颗粒6g*10':
  1362. temp_search_key = temp_search_key.replace('6g*10', '')
  1363. elif self.search_key == '999小儿感冒颗粒6g*24':
  1364. temp_search_key = temp_search_key.replace('6g*24', '')
  1365. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  1366. temp_search_key = temp_search_key.replace('6g*10袋', '')
  1367. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  1368. temp_search_key = temp_search_key.replace('6g*20袋', '')
  1369. elif self.search_key == '999感冒灵颗粒10g*9袋':
  1370. temp_search_key = temp_search_key.replace('10g*9袋', '')
  1371. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  1372. temp_search_key = temp_search_key.replace('皮炎平', '')
  1373. temp_search_key = temp_search_key.replace('20', '')
  1374. elif self.search_key == '999糠酸莫米松凝胶10':
  1375. temp_search_key = temp_search_key.replace('10', '')
  1376. elif self.search_key == '999板蓝根颗粒10g*20':
  1377. temp_search_key = temp_search_key.replace('10g*20', '')
  1378. elif self.search_key == '999咽炎片0.26g*12片':
  1379. temp_search_key = temp_search_key.replace('0.26g*12片', '')
  1380. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  1381. temp_search_key = temp_search_key.replace('8g*10袋', '')
  1382. elif self.search_key == '999感冒清热颗粒12g*18':
  1383. temp_search_key = temp_search_key.replace('12g*18', '')
  1384. elif self.search_key == '999小柴胡颗粒10g*15':
  1385. temp_search_key = temp_search_key.replace('10g*15', '')
  1386. # print (f"temp_search_key={temp_search_key}")
  1387. if self.search_key == '999抗病毒口服液': # 如果标题不包含 999 或 抗病毒口服液 或 (10ml*12 和 10ml*18) 则退出
  1388. if '999' not in title_info or temp_search_key not in title_info:
  1389. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1390. self.swipe_back(1)
  1391. self.unrelated_data += 1
  1392. return
  1393. elif '10ml*12' not in title_info and '10ml*10' not in title_info:
  1394. print(f"当前商品名称:{title_info} 不包含10*12或10*10品规")
  1395. self.swipe_back(1)
  1396. self.unrelated_data += 1
  1397. return
  1398. elif self.search_key == '999抗病毒口服液10ml*6支': # 如果标题不包含 999 或 抗病毒口服液 或 (10ml*12 和 10ml*18) 则退出
  1399. if '999' not in title_info or temp_search_key not in title_info:
  1400. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1401. self.swipe_back(1)
  1402. self.unrelated_data += 1
  1403. return
  1404. elif '10ml*6' not in title_info:
  1405. print(f"当前商品名称:{title_info} 不包含10ml*6品规")
  1406. self.swipe_back(1)
  1407. self.unrelated_data += 1
  1408. return
  1409. elif self.search_key == '999曲安奈德益康唑乳膏30g':
  1410. if '999' not in title_info or temp_search_key not in title_info:
  1411. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1412. self.swipe_back(1)
  1413. self.unrelated_data += 1
  1414. return
  1415. elif '30' not in title_info:
  1416. print(f"当前商品名称:{title_info} 不包含30品规")
  1417. self.swipe_back(1)
  1418. self.unrelated_data += 1
  1419. return
  1420. elif self.search_key == '999复方感冒灵颗粒':
  1421. if '999' not in title_info or temp_search_key not in title_info:
  1422. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1423. self.swipe_back(1)
  1424. self.unrelated_data += 1
  1425. return
  1426. elif '14g*15' not in title_info:
  1427. print(f"当前商品名称:{title_info} 不包含14g*15 和 14g*9品规")
  1428. self.swipe_back(1)
  1429. self.unrelated_data += 1
  1430. return
  1431. elif self.search_key == '999养胃舒颗粒10g*6':
  1432. if '999' not in title_info or temp_search_key not in title_info:
  1433. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1434. self.swipe_back(1)
  1435. self.unrelated_data += 1
  1436. return
  1437. elif '10g*6' not in title_info:
  1438. print(f"当前商品名称:{title_info} 不包含10g*6品规")
  1439. self.swipe_back(1)
  1440. self.unrelated_data += 1
  1441. return
  1442. elif self.search_key == '999糠酸莫米松凝胶15':
  1443. if '999' not in title_info or temp_search_key not in title_info:
  1444. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1445. self.swipe_back(1)
  1446. self.unrelated_data += 1
  1447. return
  1448. elif '15' not in title_info:
  1449. print(f"当前商品名称:{title_info} 不包含15品规")
  1450. self.swipe_back(1)
  1451. self.unrelated_data += 1
  1452. return
  1453. elif self.search_key == '999强力枇杷露225ml':
  1454. if '999' not in title_info or temp_search_key not in title_info:
  1455. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1456. self.swipe_back(1)
  1457. self.unrelated_data += 1
  1458. return
  1459. elif '225' not in title_info:
  1460. print(f"当前商品名称:{title_info} 不包含225品规")
  1461. self.swipe_back(1)
  1462. self.unrelated_data += 1
  1463. return
  1464. elif self.search_key == '999强力枇杷露120ml':
  1465. if '999' not in title_info or temp_search_key not in title_info:
  1466. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1467. self.swipe_back(1)
  1468. self.unrelated_data += 1
  1469. return
  1470. elif '120' not in title_info:
  1471. print(f"当前商品名称:{title_info} 不包含120品规")
  1472. self.swipe_back(1)
  1473. self.unrelated_data += 1
  1474. return
  1475. elif self.search_key == '999强力枇杷露150ml':
  1476. if '999' not in title_info or temp_search_key not in title_info:
  1477. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1478. self.swipe_back(1)
  1479. self.unrelated_data += 1
  1480. return
  1481. elif '150' not in title_info:
  1482. print(f"当前商品名称:{title_info} 不包含150品规")
  1483. self.swipe_back(1)
  1484. self.unrelated_data += 1
  1485. return
  1486. elif self.search_key == '999抗病毒口服液10ml*10':
  1487. if '999' not in title_info or temp_search_key not in title_info:
  1488. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1489. self.swipe_back(1)
  1490. self.unrelated_data += 1
  1491. return
  1492. elif '10ml*10' not in title_info:
  1493. print(f"当前商品名称:{title_info} 不包含10ml*10品规")
  1494. self.swipe_back(1)
  1495. self.unrelated_data += 1
  1496. return
  1497. elif self.search_key == '999抗病毒口服液10ml*12':
  1498. if '999' not in title_info or temp_search_key not in title_info:
  1499. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1500. self.swipe_back(1)
  1501. self.unrelated_data += 1
  1502. return
  1503. elif '10ml*12' not in title_info:
  1504. print(f"当前商品名称:{title_info} 不包含10ml*12品规")
  1505. self.swipe_back(1)
  1506. self.unrelated_data += 1
  1507. return
  1508. elif self.search_key == '999复方板蓝根颗粒15袋':
  1509. if '999' not in title_info or temp_search_key not in title_info:
  1510. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1511. self.swipe_back(1)
  1512. self.unrelated_data += 1
  1513. return
  1514. elif '15袋' not in title_info:
  1515. print(f"当前商品名称:{title_info} 不包含15袋品规")
  1516. self.swipe_back(1)
  1517. self.unrelated_data += 1
  1518. return
  1519. elif self.search_key == '999可调式生理性海水鼻腔喷雾50':
  1520. if '999' not in title_info or temp_search_key not in title_info:
  1521. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1522. self.swipe_back(1)
  1523. self.unrelated_data += 1
  1524. return
  1525. elif self.search_key == '999维生素E.C颗粒9袋':
  1526. if '999' not in title_info or temp_search_key not in title_info:
  1527. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1528. self.swipe_back(1)
  1529. self.unrelated_data += 1
  1530. return
  1531. elif self.search_key == '999复方氨酚烷胺胶囊6粒':
  1532. if '999' not in title_info or temp_search_key not in title_info:
  1533. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1534. self.swipe_back(1)
  1535. self.unrelated_data += 1
  1536. return
  1537. elif '6粒' not in title_info:
  1538. print(f"当前商品名称:{title_info} 不包含6粒品规")
  1539. self.swipe_back(1)
  1540. self.unrelated_data += 1
  1541. return
  1542. elif self.search_key == '999复方板蓝根颗粒15g*15袋':
  1543. if '999' not in title_info or temp_search_key not in title_info:
  1544. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1545. self.swipe_back(1)
  1546. self.unrelated_data += 1
  1547. return
  1548. elif '15g*15' not in title_info:
  1549. print(f"当前商品名称:{title_info} 不包含15g*15品规")
  1550. self.swipe_back(1)
  1551. self.unrelated_data += 1
  1552. return
  1553. elif self.search_key == '999止泻利颗粒15g*8':
  1554. if '999' not in title_info or temp_search_key not in title_info:
  1555. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1556. self.swipe_back(1)
  1557. self.unrelated_data += 1
  1558. return
  1559. elif '15g*8' not in title_info:
  1560. print(f"当前商品名称:{title_info} 不包含15g*8品规")
  1561. self.swipe_back(1)
  1562. self.unrelated_data += 1
  1563. return
  1564. elif self.search_key == '999三蛇胆川贝膏138':
  1565. if '999' not in title_info or temp_search_key not in title_info:
  1566. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1567. self.swipe_back(1)
  1568. self.unrelated_data += 1
  1569. return
  1570. elif '138' not in title_info:
  1571. print(f"当前商品名称:{title_info} 不包含138品规")
  1572. self.swipe_back(1)
  1573. self.unrelated_data += 1
  1574. return
  1575. elif self.search_key == '999强力枇杷露16袋':
  1576. if '999' not in title_info or temp_search_key not in title_info:
  1577. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1578. self.swipe_back(1)
  1579. self.unrelated_data += 1
  1580. return
  1581. elif '16袋' not in title_info:
  1582. print(f"当前商品名称:{title_info} 不包含16袋品规")
  1583. self.swipe_back(1)
  1584. self.unrelated_data += 1
  1585. return
  1586. elif self.search_key == '999复方苦参肠炎康片12片':
  1587. if '999' not in title_info or temp_search_key not in title_info:
  1588. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1589. self.swipe_back(1)
  1590. self.unrelated_data += 1
  1591. return
  1592. elif '12片' not in title_info:
  1593. print(f"当前商品名称:{title_info} 不包含12片品规")
  1594. self.swipe_back(1)
  1595. self.unrelated_data += 1
  1596. return
  1597. elif self.search_key == '999必无忧盐酸特比萘芬乳膏15':
  1598. if temp_search_key not in title_info:
  1599. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1600. self.swipe_back(1)
  1601. self.unrelated_data += 1
  1602. return
  1603. elif ('999' not in title_info) and ('必无忧' not in title_info):
  1604. print(f"当前商品名称:{title_info} 不包含关键字:999或 必无忧")
  1605. self.swipe_back(1)
  1606. self.unrelated_data += 1
  1607. return
  1608. elif '15' not in title_info:
  1609. print(f"当前商品名称:{title_info} 不包含15品规")
  1610. self.swipe_back(1)
  1611. self.unrelated_data += 1
  1612. return
  1613. elif self.search_key == '999速复康布洛芬缓释胶囊':
  1614. if temp_search_key not in title_info:
  1615. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1616. self.swipe_back(1)
  1617. self.unrelated_data += 1
  1618. return
  1619. elif ('999' not in title_info) and ('速复康' not in title_info):
  1620. print(f"当前商品名称:{title_info} 不包含关键字:999或 速复康")
  1621. self.swipe_back(1)
  1622. self.unrelated_data += 1
  1623. return
  1624. elif self.search_key == '999维生素C咀嚼片':
  1625. if '999' not in title_info or temp_search_key not in title_info:
  1626. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1627. self.swipe_back(1)
  1628. self.unrelated_data += 1
  1629. return
  1630. elif '80' not in title_info:
  1631. print(f"当前商品名称:{title_info} 不包含80品规")
  1632. self.swipe_back(1)
  1633. self.unrelated_data += 1
  1634. return
  1635. elif self.search_key == '999精氨酸布洛芬颗粒':
  1636. if '999' not in title_info or temp_search_key not in title_info:
  1637. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1638. self.swipe_back(1)
  1639. self.unrelated_data += 1
  1640. return
  1641. elif '9' not in title_info:
  1642. print(f"当前商品名称:{title_info} 不包含9品规")
  1643. self.swipe_back(1)
  1644. self.unrelated_data += 1
  1645. return
  1646. elif self.search_key == '999阿奇霉素片':
  1647. if '999' not in title_info or temp_search_key not in title_info:
  1648. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1649. self.swipe_back(1)
  1650. self.unrelated_data += 1
  1651. return
  1652. elif '0.25g*6' not in title_info:
  1653. print(f"当前商品名称:{title_info} 不包含0.25g*6品规")
  1654. self.swipe_back(1)
  1655. self.unrelated_data += 1
  1656. return
  1657. elif self.search_key == '999感冒清热颗粒6g*10':
  1658. if '999' not in title_info or temp_search_key not in title_info:
  1659. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1660. self.swipe_back(1)
  1661. self.unrelated_data += 1
  1662. return
  1663. elif '6g*10' not in title_info:
  1664. print(f"当前商品名称:{title_info} 不包含6g*10品规")
  1665. self.swipe_back(1)
  1666. self.unrelated_data += 1
  1667. return
  1668. elif self.search_key == '999选平硝酸咪康唑乳膏20g':
  1669. if temp_search_key not in title_info:
  1670. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1671. self.swipe_back(1)
  1672. self.unrelated_data += 1
  1673. return
  1674. elif ('999' not in title_info) and ('选平' not in title_info):
  1675. print(f"当前商品名称:{title_info} 不包含关键字:999或 选平")
  1676. self.swipe_back(1)
  1677. self.unrelated_data += 1
  1678. return
  1679. elif '20g' not in title_info:
  1680. print(f"当前商品名称:{title_info} 不包含20g品规")
  1681. self.swipe_back(1)
  1682. self.unrelated_data += 1
  1683. return
  1684. elif self.search_key == '999糠酸莫米松乳膏10g':
  1685. if '999' not in title_info or temp_search_key not in title_info:
  1686. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1687. self.swipe_back(1)
  1688. self.unrelated_data += 1
  1689. return
  1690. elif '10g' not in title_info:
  1691. print(f"当前商品名称:{title_info} 不包含10g品规")
  1692. self.swipe_back(1)
  1693. self.unrelated_data += 1
  1694. return
  1695. elif self.search_key == '999补脾益肠丸':
  1696. if temp_search_key not in title_info:
  1697. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1698. self.swipe_back(1)
  1699. self.unrelated_data += 1
  1700. return
  1701. elif ('999' not in title_info) and ('三九' not in title_info):
  1702. print(f"当前商品名称:{title_info} 不包含关键字:999或 三九")
  1703. self.swipe_back(1)
  1704. self.unrelated_data += 1
  1705. return
  1706. elif self.search_key == '999壮骨关节丸6g*20':
  1707. if '999' not in title_info or temp_search_key not in title_info:
  1708. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1709. self.swipe_back(1)
  1710. self.unrelated_data += 1
  1711. return
  1712. elif '6g*20' not in title_info:
  1713. print(f"当前商品名称:{title_info} 不包含6g*20品规")
  1714. self.swipe_back(1)
  1715. self.unrelated_data += 1
  1716. return
  1717. elif self.search_key == '999正天丸6g*15':
  1718. if '999' not in title_info or temp_search_key not in title_info:
  1719. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1720. self.swipe_back(1)
  1721. self.unrelated_data += 1
  1722. return
  1723. elif '6g*15' not in title_info:
  1724. print(f"当前商品名称:{title_info} 不包含6g*15品规")
  1725. self.swipe_back(1)
  1726. self.unrelated_data += 1
  1727. return
  1728. elif self.search_key == '999藿香正气合剂10ml*6':
  1729. if '999' not in title_info or temp_search_key not in title_info:
  1730. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1731. self.swipe_back(1)
  1732. self.unrelated_data += 1
  1733. return
  1734. elif '10ml*6' not in title_info:
  1735. print(f"当前商品名称:{title_info} 不包含10ml*6品规")
  1736. self.swipe_back(1)
  1737. self.unrelated_data += 1
  1738. return
  1739. elif self.search_key == '999藿香正气合剂10ml*10':
  1740. if '999' not in title_info or temp_search_key not in title_info:
  1741. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1742. self.swipe_back(1)
  1743. self.unrelated_data += 1
  1744. return
  1745. elif '10ml*10' not in title_info:
  1746. print(f"当前商品名称:{title_info} 不包含10ml*10品规")
  1747. self.swipe_back(1)
  1748. self.unrelated_data += 1
  1749. return
  1750. elif self.search_key == '999小儿止咳糖浆120':
  1751. if '999' not in title_info or temp_search_key not in title_info:
  1752. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1753. self.swipe_back(1)
  1754. self.unrelated_data += 1
  1755. return
  1756. elif '120' not in title_info:
  1757. print(f"当前商品名称:{title_info} 不包含120品规")
  1758. self.swipe_back(1)
  1759. self.unrelated_data += 1
  1760. return
  1761. elif self.search_key == '999小儿止咳糖浆225':
  1762. if '999' not in title_info or temp_search_key not in title_info:
  1763. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1764. self.swipe_back(1)
  1765. self.unrelated_data += 1
  1766. return
  1767. elif '225' not in title_info:
  1768. print(f"当前商品名称:{title_info} 不包含225品规")
  1769. self.swipe_back(1)
  1770. self.unrelated_data += 1
  1771. return
  1772. elif self.search_key == '999小儿感冒颗粒6g*10':
  1773. if '999' not in title_info or temp_search_key not in title_info:
  1774. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1775. self.swipe_back(1)
  1776. self.unrelated_data += 1
  1777. return
  1778. elif '6g*10' not in title_info:
  1779. print(f"当前商品名称:{title_info} 不包含6g*10品规")
  1780. self.swipe_back(1)
  1781. self.unrelated_data += 1
  1782. return
  1783. elif self.search_key == '999小儿感冒颗粒6g*24':
  1784. if '999' not in title_info or temp_search_key not in title_info:
  1785. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1786. self.swipe_back(1)
  1787. self.unrelated_data += 1
  1788. return
  1789. elif '6g*24' not in title_info:
  1790. print(f"当前商品名称:{title_info} 不包含6g*24品规")
  1791. self.swipe_back(1)
  1792. self.unrelated_data += 1
  1793. return
  1794. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*10袋':
  1795. if '999' not in title_info or temp_search_key not in title_info:
  1796. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1797. self.swipe_back(1)
  1798. self.unrelated_data += 1
  1799. return
  1800. elif '6g*10' not in title_info:
  1801. print(f"当前商品名称:{title_info} 不包含6g*10品规")
  1802. self.swipe_back(1)
  1803. self.unrelated_data += 1
  1804. return
  1805. elif self.search_key == '999小儿氨酚黄那敏颗粒6g*20袋':
  1806. if '999' not in title_info or temp_search_key not in title_info:
  1807. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1808. self.swipe_back(1)
  1809. self.unrelated_data += 1
  1810. return
  1811. elif '6g*20' not in title_info:
  1812. print(f"当前商品名称:{title_info} 不包含6g*20品规")
  1813. self.swipe_back(1)
  1814. self.unrelated_data += 1
  1815. return
  1816. elif self.search_key == '999感冒灵颗粒':
  1817. if '999' not in title_info or temp_search_key not in title_info:
  1818. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1819. self.swipe_back(1)
  1820. self.unrelated_data += 1
  1821. return
  1822. elif '10g*9' not in title_info:
  1823. print(f"当前商品名称:{title_info} 不包含10g*9品规")
  1824. self.swipe_back(1)
  1825. self.unrelated_data += 1
  1826. return
  1827. elif self.search_key == '999皮炎平复方醋酸地塞米松乳膏20':
  1828. if temp_search_key not in title_info:
  1829. print(f"当前商品名称:{title_info} 不包含关键字:{temp_search_key}")
  1830. self.swipe_back(1)
  1831. self.unrelated_data += 1
  1832. return
  1833. # elif ('999' not in title_info) and ('皮炎平' not in title_info):
  1834. elif '999' not in title_info:
  1835. print(f"当前商品名称:{title_info} 不包含关键字:999或 皮炎平")
  1836. self.swipe_back(1)
  1837. self.unrelated_data += 1
  1838. return
  1839. elif '20g' not in title_info:
  1840. print(f"当前商品名称:{title_info} 不包含20g品规")
  1841. self.swipe_back(1)
  1842. self.unrelated_data += 1
  1843. return
  1844. elif self.search_key == '999糠酸莫米松凝胶10':
  1845. if '999' not in title_info or temp_search_key not in title_info:
  1846. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1847. self.swipe_back(1)
  1848. self.unrelated_data += 1
  1849. return
  1850. elif '10' not in title_info:
  1851. print(f"当前商品名称:{title_info} 不包含10品规")
  1852. self.swipe_back(1)
  1853. self.unrelated_data += 1
  1854. return
  1855. elif self.search_key == '999板蓝根颗粒10g*20':
  1856. if '999' not in title_info or temp_search_key not in title_info:
  1857. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1858. self.swipe_back(1)
  1859. self.unrelated_data += 1
  1860. return
  1861. elif '10g*20' not in title_info:
  1862. print(f"当前商品名称:{title_info} 不包含10g*20品规")
  1863. self.swipe_back(1)
  1864. self.unrelated_data += 1
  1865. return
  1866. elif self.search_key == '999咽炎片0.26g*12片':
  1867. if '999' not in title_info or temp_search_key not in title_info:
  1868. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1869. self.swipe_back(1)
  1870. self.unrelated_data += 1
  1871. return
  1872. elif '0.26g*12' not in title_info and '0.26g*24' not in title_info:
  1873. print(f"当前商品名称:{title_info} 不包含0.26g*12 和 0.26g*24品规")
  1874. self.swipe_back(1)
  1875. self.unrelated_data += 1
  1876. return
  1877. elif self.search_key == '999感冒清热颗粒12g*18':
  1878. if '999' not in title_info or temp_search_key not in title_info:
  1879. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1880. self.swipe_back(1)
  1881. self.unrelated_data += 1
  1882. return
  1883. elif '12g*18' not in title_info:
  1884. print(f"当前商品名称:{title_info} 不包含12g*18品规")
  1885. self.swipe_back(1)
  1886. self.unrelated_data += 1
  1887. return
  1888. elif self.search_key == '999小儿咽扁颗粒8g*10袋':
  1889. if '999' not in title_info or temp_search_key not in title_info:
  1890. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1891. self.swipe_back(1)
  1892. self.unrelated_data += 1
  1893. return
  1894. elif '8g*10' not in title_info:
  1895. print(f"当前商品名称:{title_info} 不包含8g*10品规")
  1896. self.swipe_back(1)
  1897. self.unrelated_data += 1
  1898. return
  1899. elif self.search_key == '999小柴胡颗粒10g*15':
  1900. if '999' not in title_info or temp_search_key not in title_info:
  1901. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1902. self.swipe_back(1)
  1903. self.unrelated_data += 1
  1904. return
  1905. elif '15' not in title_info:
  1906. print(f"当前商品名称:{title_info} 不包含15品规")
  1907. self.swipe_back(1)
  1908. self.unrelated_data += 1
  1909. return
  1910. else:
  1911. if '999' not in title_info or temp_search_key not in title_info:
  1912. print(f"当前商品名称:{title_info} 不包含关键字:{self.search_key}")
  1913. self.swipe_back(1)
  1914. self.unrelated_data += 1
  1915. return
  1916. else:
  1917. if self.search_key == '史达功右美沙芬愈创甘油醚糖浆120':
  1918. temp_search_key = self.search_key.replace('史达功', '')
  1919. temp_search_key = temp_search_key.replace('120', '')
  1920. if '史达功' not in title_info or temp_search_key not in title_info:
  1921. print(f'药品标题未包含药品关键字:-->{temp_search_key}')
  1922. self.swipe_back(1)
  1923. self.unrelated_data += 1
  1924. return
  1925. elif '120' not in title_info:
  1926. print(f"当前商品名称:{title_info} 不包含120品规")
  1927. self.swipe_back(1)
  1928. self.unrelated_data += 1
  1929. return
  1930. elif self.search_key == '三九胃泰养胃舒颗粒8袋':
  1931. temp_search_key = self.search_key.replace('三九胃泰', '')
  1932. temp_search_key = temp_search_key.replace('8袋', '')
  1933. if '三九胃泰' not in title_info or temp_search_key not in title_info:
  1934. print(f'药品标题未包含药品关键字:-->{temp_search_key}')
  1935. self.swipe_back(1)
  1936. self.unrelated_data += 1
  1937. return
  1938. elif '8袋' not in title_info:
  1939. print(f"当前商品名称:{title_info} 不包含8袋品规")
  1940. self.swipe_back(1)
  1941. self.unrelated_data += 1
  1942. return
  1943. elif self.search_key == '三九复方金银花颗粒10g*8袋':
  1944. temp_search_key = self.search_key.replace('三九', '')
  1945. temp_search_key = temp_search_key.replace('10g*8袋', '')
  1946. if '三九' not in title_info or temp_search_key not in title_info:
  1947. print(f'药品标题未包含药品关键字:-->{temp_search_key}')
  1948. self.swipe_back(1)
  1949. self.unrelated_data += 1
  1950. return
  1951. elif '10g*8' not in title_info:
  1952. print(f"当前商品名称:{title_info} 不包含10g*8品规")
  1953. self.swipe_back(1)
  1954. self.unrelated_data += 1
  1955. return
  1956. elif self.search_key == '必无忧盐酸特比萘芬喷雾剂30ml':
  1957. temp_search_key = self.search_key.replace('必无忧', '')
  1958. temp_search_key = temp_search_key.replace('30ml', '')
  1959. if '必无忧' not in title_info or temp_search_key not in title_info:
  1960. print(f'药品标题未包含药品关键字:-->{temp_search_key}')
  1961. self.swipe_back(1)
  1962. self.unrelated_data += 1
  1963. return
  1964. elif '30' not in title_info:
  1965. print(f"当前商品名称:{title_info} 不包含30品规")
  1966. self.swipe_back(1)
  1967. self.unrelated_data += 1
  1968. return
  1969. elif self.search_key == '佳美舒阿奇霉素肠溶胶囊4':
  1970. temp_search_key = self.search_key.replace('佳美舒', '')
  1971. temp_search_key = temp_search_key.replace('4', '')
  1972. if '佳美舒' not in title_info or temp_search_key not in title_info:
  1973. print(f'药品标题未包含药品关键字:-->{temp_search_key}')
  1974. self.swipe_back(1)
  1975. self.unrelated_data += 1
  1976. return
  1977. elif '4' not in title_info and '8' not in title_info:
  1978. print(f"当前商品名称:{title_info} 不包含4品规或8品规")
  1979. self.swipe_back(1)
  1980. self.unrelated_data += 1
  1981. return
  1982. elif self.search_key == '三九胃泰颗粒20g*10':
  1983. temp_search_key = self.search_key.replace('20g*10', '')
  1984. if temp_search_key not in title_info:
  1985. print(f'药品标题未包含药品关键字:-->{temp_search_key}')
  1986. self.swipe_back(1)
  1987. self.unrelated_data += 1
  1988. return
  1989. elif '20g*10' not in title_info:
  1990. print(f"当前商品名称:{title_info} 不包含20g*10品规")
  1991. self.swipe_back(1)
  1992. self.unrelated_data += 1
  1993. return
  1994. elif self.search_key == '三九胃泰颗粒2.5g*6':
  1995. temp_search_key = self.search_key.replace('2.5g*6', '')
  1996. if temp_search_key not in title_info:
  1997. print(f'药品标题未包含药品关键字:-->{temp_search_key}')
  1998. self.swipe_back(1)
  1999. self.unrelated_data += 1
  2000. return
  2001. elif '2.5g*6' not in title_info:
  2002. print(f"当前商品名称:{title_info} 不包含2.5g*6品规")
  2003. self.swipe_back(1)
  2004. self.unrelated_data += 1
  2005. return
  2006. elif self.search_key == '三九胃泰颗粒20g*6袋':
  2007. temp_search_key = self.search_key.replace('20g*6袋', '')
  2008. if temp_search_key not in title_info:
  2009. print(f'药品标题未包含药品关键字:-->{temp_search_key}')
  2010. self.swipe_back(1)
  2011. self.unrelated_data += 1
  2012. return
  2013. elif '20g*6' not in title_info:
  2014. print(f"当前商品名称:{title_info} 不包含20g*6品规")
  2015. self.swipe_back(1)
  2016. self.unrelated_data += 1
  2017. return
  2018. elif self.search_key == '顺峰酮康他索乳膏':
  2019. temp_search_key = self.search_key.replace('顺峰', '')
  2020. if temp_search_key not in title_info or '顺峰' not in title_info:
  2021. print(f'药品标题未包含药品关键字:-->{temp_search_key}')
  2022. self.swipe_back(1)
  2023. self.unrelated_data += 1
  2024. return
  2025. elif self.search_key == '速复康磷酸奥司他韦胶囊75mg*10':
  2026. temp_search_key = self.search_key.replace('速复康', '')
  2027. temp_search_key = temp_search_key.replace('75mg*10', '')
  2028. if '佳美舒' not in title_info or temp_search_key not in title_info:
  2029. print(f'药品标题未包含药品关键字:-->{temp_search_key}')
  2030. self.swipe_back(1)
  2031. self.unrelated_data += 1
  2032. return
  2033. elif '75mg*10' not in title_info:
  2034. print(f"当前商品名称:{title_info} 不包含75mg*10品规")
  2035. self.swipe_back(1)
  2036. self.unrelated_data += 1
  2037. return
  2038. else:
  2039. if self.search_key not in title_info:
  2040. print(f'药品标题未包含药品关键字:-->{self.search_key}')
  2041. self.swipe_back(1)
  2042. self.unrelated_data += 1
  2043. return
  2044. else:
  2045. print('标题获取为空')
  2046. self.swipe_back(1)
  2047. return
  2048. # 第一次没有获取到价格
  2049. if not min_price:
  2050. min_price = self.drug_price() # 最低价格 第二次获取
  2051. if not min_price:
  2052. print('提取价格出错,回退到列表页')
  2053. self.swipe_back(1)
  2054. self.unrelated_data += 1
  2055. return
  2056. # 商品链接 分享链接
  2057. product_link = self.get_product_link()
  2058. time.sleep(2)
  2059. if self.search_key == '999小儿止咳糖浆' or self.search_key == '999小儿氨酚黄那敏颗粒' or self.search_key == '999小儿感冒颗粒':
  2060. shop = self.get_shop_name()
  2061. else:
  2062. for i in range(15):
  2063. if self.d(textStartsWith="进店").exists:
  2064. print('开始获取店铺名')
  2065. break
  2066. screen_width = self.d.info['displayWidth']
  2067. screen_height = self.d.info['displayHeight']
  2068. # self.d.swipe(screen_width // 2, screen_height - 400, screen_width // 2, 400, duration=0.2)
  2069. self.d.swipe_ext("up", scale=0.3)
  2070. time.sleep(self.get_sleep_time())
  2071. if self.d(textStartsWith="进店").exists:
  2072. print('可以开始获取店铺名')
  2073. # shop = self.get_shop_name()
  2074. shop = self.get_shop_name()
  2075. if not shop:
  2076. print('当前店铺名称为空')
  2077. self.swipe_back(1)
  2078. self.unrelated_data += 1
  2079. return
  2080. # 爬取日期
  2081. scrape_date = self.get_current_date()
  2082. if self.search_key == '999小柴胡颗粒10g*15':
  2083. dup_search_key = '999小柴胡颗粒'
  2084. else:
  2085. dup_search_key = self.search_key
  2086. dup_data = {'search_key': dup_search_key, 'min_price': min_price, 'shop': shop, 'scrape_date': scrape_date,
  2087. 'platform': '拼多多'}
  2088. if self.data_is_exists(dup_data):
  2089. print('存在相同数据不入库')
  2090. self.swipe_back(1)
  2091. return
  2092. is_has_instructions = self.has_instructions()
  2093. self.loggerPdd.info(f'是否有说明书:{is_has_instructions}')
  2094. manufacture_date = ''
  2095. business_license_company = ''
  2096. credit_code = ext
  2097. city = ''
  2098. province = ''
  2099. # 说明书等信息
  2100. if is_has_instructions:
  2101. try:
  2102. instructions_info = self.get_instructions_data()
  2103. # print('说明书相关信息:', instructions_info)
  2104. expiry_date = instructions_info['有效期'].strip('。')
  2105. manufacturer = instructions_info['生产单位'].strip('。')
  2106. approval_number = instructions_info['批准文号'].strip('。')
  2107. specifications = instructions_info['产品规格'].strip('。')
  2108. except Exception as e:
  2109. print(f'获取详情页规格参数出错:{e}')
  2110. self.swipe_back(2)
  2111. return
  2112. else:
  2113. expiry_date = ''
  2114. manufacturer = ''
  2115. approval_number = ''
  2116. specifications = ''
  2117. self.unrelated_data = 0
  2118. # 商品链接
  2119. # product_link = ''
  2120. # 爬取省份
  2121. scrape_province = '广东' # 这里先默认广东
  2122. # 是否有货
  2123. availability = ''
  2124. if self.search_key == '999小柴胡颗粒10g*15':
  2125. save_search_key = '999小柴胡颗粒'
  2126. else:
  2127. save_search_key = self.search_key
  2128. save_data = {
  2129. 'product': title_info,
  2130. 'min_price': min_price,
  2131. 'manufacture_date': manufacture_date,
  2132. 'expiry_date': expiry_date,
  2133. 'shop': shop,
  2134. 'business_license_company': business_license_company,
  2135. 'province': province,
  2136. 'city': city,
  2137. 'manufacturer': manufacturer,
  2138. 'specification': specifications,
  2139. 'approval_number': approval_number,
  2140. 'product_link': product_link,
  2141. 'scrape_date': scrape_date,
  2142. 'scrape_province': scrape_province,
  2143. 'availability': availability,
  2144. 'credit_code': credit_code,
  2145. 'platform': '拼多多',
  2146. 'search_key': save_search_key,
  2147. 'number': 1
  2148. }
  2149. # print(f'待插入数据:{save_data}')
  2150. self.save_to_database(save_data)
  2151. self.swipe_back(1)
  2152. # 获取店铺信息start 2025-07-28
  2153. if self.distinct_target():
  2154. print('已到达搜索列表页')
  2155. else:
  2156. for i in range(2):
  2157. self.swipe_back(1)
  2158. # 最外部有个定位按钮
  2159. if self.distinct_target():
  2160. break
  2161. def main(self, device_id, search_key_length, keyword_idx):
  2162. spider_no = 0
  2163. self.connect_devices(device_id)
  2164. time.sleep(self.get_sleep_time())
  2165. # print("搜索前,先重启APP")
  2166. # # 重新开启拼多多应用
  2167. # self.restart_app()
  2168. # # 搜索关键字
  2169. # self.enter_target_page()
  2170. if keyword_idx == 0:
  2171. print("搜索前,先重启APP")
  2172. # 重新开启拼多多应用
  2173. self.restart_app()
  2174. # 搜索关键字
  2175. self.enter_target_page()
  2176. else:
  2177. print("清空前面的文字,再输入关键词")
  2178. self.d.send_keys(self.search_key, clear=True)
  2179. time.sleep(self.get_sleep_time())
  2180. print("点击搜索")
  2181. self.d.xpath('//*[@text="搜索"]').click()
  2182. time.sleep(self.get_sleep_time())
  2183. # 3. 容错处理:点击失败时的兜底逻辑
  2184. # self.swipe_up()
  2185. for idx in range(300):
  2186. print(f'第{idx + 1}页')
  2187. if spider_no > 30:
  2188. time.sleep(300)
  2189. spider_no = 0
  2190. if self.unrelated_data > 10:
  2191. print(f'[{self.program_start_time}]----{self.search_key}----连续超过10个不达标的数据则停止采集')
  2192. print(f"[程序启动时间:{self.program_start_time}-----程序结束时间:{self.app_current_time()}]----搜索关键词:{self.search_key}----点击了{self.click_counts}个商品")
  2193. # self.swipe_down()
  2194. # time.sleep(self.get_sleep_time()) # 下滑后等待页面稳定
  2195. #
  2196. # click_success = self.click_target_product_by_search_key(fuzzy_match=False)
  2197. # if not click_success:
  2198. # print(f"⚠️ 关键词「{self.search_key}」商品点击失败")
  2199. # return
  2200. #
  2201. # print("点击搜索框")
  2202. # self.d(className='android.widget.EditText').click()
  2203. # time.sleep(self.get_sleep_time())
  2204. if keyword_idx == search_key_length - 1:
  2205. print("程序最后一个品规采集完毕,返回主屏幕")
  2206. self.d.press("home")
  2207. # 连续超过10个不达标的数据则停止采集
  2208. break
  2209. if self.max_counts >= 300 and self.search_key == '999糠酸莫米松凝胶10':
  2210. print('enough')
  2211. break
  2212. if self.search_key == '顺峰酮康他索乳膏' and self.max_counts >= 300:
  2213. print('enough')
  2214. break
  2215. # 售罄次数大于4基本就是号废了但是如果下次点击不会出现这种情况就要重置为0
  2216. if self.sold_out_counts > 4:
  2217. print(f"[程序启动时间:{self.program_start_time}-----程序结束时间:{self.app_current_time()}]----搜索关键词:{self.search_key}----点击了{self.click_counts}个商品")
  2218. print("====商品已售罄4次,结束采集(号不能用),返回主屏幕====")
  2219. self.d.press('home')
  2220. break
  2221. if idx == 0:
  2222. drug_lis = self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.FrameLayout[2]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout').all()
  2223. else:
  2224. for i in range(1, 6):
  2225. drug_xpath = f'/hierarchy/android.widget.FrameLayout[{i}]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout'
  2226. drug_lis = self.d.xpath(
  2227. f'/hierarchy/android.widget.FrameLayout[{i}]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout').all()
  2228. if drug_lis:
  2229. break
  2230. print('数量', len(drug_lis))
  2231. for idd, drug_one in enumerate(drug_lis):
  2232. print(idd + 1, drug_one.info)
  2233. time.sleep(self.get_sleep_time())
  2234. # left = drug_one.info['bounds']['left']
  2235. top = drug_one.info['bounds']['top']
  2236. # right = drug_one.info['bounds']['right']
  2237. bottom = drug_one.info['bounds']['bottom']
  2238. # height = bottom - top
  2239. # 高度低于多少的不点击采集
  2240. # if bottom <= 1400 and top >= 258:
  2241. if bottom <= 1524 and top >= 258:
  2242. # print(f"这页的第{idd+1}个商品")
  2243. # #商品名称的xpath
  2244. # if idx == 0:
  2245. # product_tittle_xpath = f'//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.FrameLayout[2]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[{idd+2}]/android.widget.LinearLayout[1]/android.widget.FrameLayout[2]/android.widget.TextView'
  2246. # else:
  2247. # product_tittle_xpath = drug_xpath + f'[{idd+1}]/android.widget.LinearLayout[1]/android.widget.FrameLayout[2]/android.widget.TextView'
  2248. # print(f"商品名称的xpath:{product_tittle_xpath}")
  2249. # if self.d.xpath(product_tittle_xpath).exists:
  2250. # # product_title = self.d.xpath(product_tittle_xpath).info['contentDescription']
  2251. # print(f"列表当前info:{self.d.xpath(product_tittle_xpath).info}")
  2252. # product_title = self.d.xpath(product_tittle_xpath).text
  2253. # print(f"列表当前商品名称:{product_title}")
  2254. # if '999' in self.search_key:
  2255. # temp_search_key = self.search_key.replace('999', '')
  2256. # if '999' not in product_title or temp_search_key not in product_title:
  2257. # print(f"当前商品名称:{product_title} 不包含关键字:{self.search_key}")
  2258. # continue
  2259. # else:
  2260. # if self.search_key not in product_title.replace(' ', ''):
  2261. # continue
  2262. # else:
  2263. # print(f"列表当前商品路径不存在")
  2264. drug_one.click()
  2265. self.click_counts += 1
  2266. time.sleep(self.get_sleep_time())
  2267. # 先判断是否售罄次数是否大于4
  2268. if self.sold_out_counts >= 4:
  2269. print(f"[程序启动时间:{self.program_start_time}-----程序结束时间:{self.app_current_time()}]----搜索关键词:{self.search_key}----点击了{self.click_counts}个商品")
  2270. print("====这是在第一页有两个,商品已售罄4次,结束采集(号不能用)====")
  2271. time.sleep(self.get_sleep_time())
  2272. self.d.press('home')
  2273. return
  2274. # 已售罄 self.sold_out_counts += 1
  2275. if self.d.xpath('//*[contains(@text, "商品已售罄")]').wait(timeout=5):
  2276. print("======商品已售罄======")
  2277. self.sold_out_counts += 1
  2278. self.swipe_back(1)
  2279. continue
  2280. # 采集药品信息
  2281. try:
  2282. # 重置商品售罄次数
  2283. self.sold_out_counts = 0
  2284. self.integrate_data()
  2285. # 检测下是否回退到列表页
  2286. if self.distinct_target():
  2287. print('回退到列表页', True)
  2288. else:
  2289. print(f'[{self.app_current_time()}] 回退到列表页失败,终止采集')
  2290. print(f"[程序启动时间:{self.program_start_time}-----程序结束时间:{self.app_current_time()}]----搜索关键词:{self.search_key}----点击了{self.click_counts}个商品")
  2291. return
  2292. time.sleep(self.get_sleep_time())
  2293. spider_no += 1
  2294. except Exception as e:
  2295. print(f'采集药品详情数据出错:{e}')
  2296. self.loggerPdd.error(f'采集药品详情数据出错:{e}')
  2297. if not self.distinct_target():
  2298. for i in range(1):
  2299. self.swipe_back(1)
  2300. # 最外部有个列表按钮
  2301. if self.distinct_target():
  2302. break
  2303. if i == 0 and not self.distinct_target():
  2304. print('页面出错,退出采集')
  2305. return
  2306. else:
  2307. continue
  2308. if self.d(textStartsWith="抱歉,没有更多商品啦~").exists:
  2309. print('已经到达列表页最底部')
  2310. break
  2311. print('开始滑入下一页')
  2312. # search_list = self.d.xpath('//*[@resource-id="android:id/content"]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.support.v7.widget.RecyclerView[1]').info
  2313. # bounds = search_list['bounds']
  2314. # try:
  2315. # top = drug_lis[0].info['bounds']['top']
  2316. # except Exception as e:
  2317. # print(f'获取滑动参数top出错:{e}')
  2318. # top = 250
  2319. # search_list = self.d.xpath('//android.support.v7.widget.RecyclerView').info
  2320. # bottom = search_list['bounds']['bottom']
  2321. # end_y = 1400 + top - bottom + 162
  2322. # if end_y < 150:
  2323. # end_y = 150
  2324. end_y = 300
  2325. # self.d.swipe(200, 1400, 200, 1400 + bounds['top'] - bounds['bottom'] + 162, 0.4)
  2326. self.d.swipe(200, 1400, 200, end_y, 0.4)
  2327. time.sleep(self.get_sleep_time())
  2328. def unitest(self):
  2329. """
  2330. 单元测试
  2331. :return:
  2332. """
  2333. # device_id = '21885f5'
  2334. # # self.connect_devices(device_id)
  2335. # # self.screenshot_the_business_license()
  2336. # # cropped_screenshot_path = 'cropped_screenshot.png'
  2337. # # self.get_ocr_res(cropped_screenshot_path)
  2338. # shop_name = '我的店铺'
  2339. # base_path = r'D:\work\dfwy_spider\drug_data\pdd\screenshot'
  2340. # cropped_screenshot_path = os.path.join(base_path, shop_name + '.png')
  2341. # os.makedirs(base_path, exist_ok=True)
  2342. # print(cropped_screenshot_path)
  2343. # cropped_screenshot_path = 'D:\\work\\dfwy_spider\\drug_data\\pdd\\screenshot\\' + shop_name + '.png'
  2344. # print(cropped_screenshot_path)
  2345. # time.sleep(10000)
  2346. ocr_res = self.get_ocr_res('ceshi1.jpg')
  2347. print(f'ocr_res:{ocr_res}')
  2348. # 获取当前时间
  2349. current_time = datetime.datetime.now()
  2350. # 格式化为时分秒
  2351. time_str = current_time.strftime("%H-%M-%S")
  2352. # 生成随机的 8 位字符串
  2353. random_str = secrets.token_hex(4) # 生成 4 个字节的随机字符串,转换为 8 位十六进制字符串
  2354. screenshot_path = 'instructionscreenshot1-' + time_str + '-' + random_str + '.png'
  2355. print(screenshot_path)
  2356. # if ocr_res:
  2357. # for item in ocr_res:
  2358. # if '拖动滑块完成' in item['words']:
  2359. # print("滑块验证")
  2360. # break
  2361. # print("ocr_res end")
  2362. # company_name = ''
  2363. # reg_number = ''
  2364. # address = ''
  2365. # if ocr_res:
  2366. # for item in ocr_res:
  2367. # if '企业注册号' in item['words']:
  2368. # # print('come in 111')
  2369. # reg_number = item['words'].split(':', 1)[1].strip()
  2370. # elif '企业名称' in item['words']:
  2371. # # print('come in 222')
  2372. # company_name = item['words'].split(':', 1)[1].strip()
  2373. # elif '所:' in item['words']:
  2374. # # print('come in 333')
  2375. # address = item['words'].split(':', 1)[1].strip()
  2376. # # 输出结果
  2377. # print("企业注册号:", reg_number)
  2378. # print("企业名称:", company_name)
  2379. # print("住所:", address)
  2380. # screenshot_verify_path = 'screenshot_verify.png'
  2381. # img = cv2.imread(screenshot_verify_path)
  2382. # # 指定裁剪区域 (left, top, right, bottom)
  2383. # left = 118
  2384. # top = 478
  2385. # right = 602
  2386. # bottom = 722
  2387. # cropped_verify_img = img[top:bottom, left:right]
  2388. # cropped_verify_creenshot_path = 'cropped_verify_screenshot.png'
  2389. # cv2.imwrite(cropped_verify_creenshot_path, cropped_verify_img)
  2390. # print('裁剪完成')
  2391. time.sleep(100000)
  2392. title_info = '云南白药 参苓健脾胃颗粒10袋 补脾健胃利湿止泻 脾胃虚弱 饮食不消 或泻或吐 形瘦色萎 神疲乏力 5盒装(补脾健胃)'
  2393. min_price = 85
  2394. shop = '堂鹭北大药房旗舰店'
  2395. scrape_date = '2025-03-19'
  2396. dup_data = {'product': title_info, 'min_price': min_price, 'shop': shop, 'scrape_date': scrape_date}
  2397. print(self.data_is_exists(dup_data))
  2398. # pdd
  2399. def main():
  2400. # 999感冒清热颗粒、 三九胃泰颗粒
  2401. # 暂时不需要的:
  2402. # '999小儿止咳糖浆'
  2403. # '999小儿氨酚黄那敏颗粒'
  2404. # '999小儿感冒颗粒'
  2405. # '999抗病毒口服液10ml*6支'
  2406. # '今维多赐多康牌蛋白粉',
  2407. # '必无忧盐酸特比萘芬喷雾剂30ml'
  2408. # '999冰连清咽'
  2409. # '999复方苦参肠炎康片12片'
  2410. # '999强力枇杷露16袋'
  2411. # '999三蛇胆川贝膏138'
  2412. # '999维生素E.C颗粒9袋'
  2413. # '三九胃泰养胃舒颗粒8袋'
  2414. # '999止泻利颗粒15g*8'
  2415. # '史达功右美沙芬愈创甘油醚糖浆120'
  2416. # '999复方氨酚烷胺胶囊6粒'
  2417. # '999可调式生理性海水鼻腔喷雾50'
  2418. # '999小儿止咳糖浆120' #不低于19.8
  2419. # '999小儿止咳糖浆225' #禁止挂网
  2420. # '999小儿感冒颗粒6g*10' #不低于24.9
  2421. # '999小儿感冒颗粒6g*24' #禁止挂网
  2422. # '999小儿氨酚黄那敏颗粒6g*10袋' #不低于15.8
  2423. # '999小儿氨酚黄那敏颗粒6g*20袋' #禁止挂网
  2424. # '999小儿咽扁颗粒8g*10袋' #仅限999官旗店
  2425. # '999阿奇霉素片'
  2426. # OTC
  2427. # '999银菊清咽颗粒' #没有数据
  2428. # '999感冒清热颗粒6g*10'
  2429. # '999选平硝酸咪康唑乳膏20g'
  2430. # '999糠酸莫米松乳膏10g'
  2431. # '999表虚感冒颗粒' #没有数据
  2432. # '999补脾益肠丸'
  2433. # '999壮骨关节胶囊'
  2434. # '999壮骨关节丸6g*20'
  2435. # '999正天丸6g*15'
  2436. # '999正天胶囊'
  2437. # '三九胃泰胶囊'
  2438. # '三九胃泰颗粒20g*10'
  2439. # '三九胃泰颗粒2.5g*6'
  2440. # '999藿香正气合剂10ml*6',
  2441. # '999藿香正气合剂10ml*10',
  2442. # '999黄芪精',
  2443. # '999曲安奈德益康唑乳膏30g',
  2444. # '999葡萄糖酸锌口服溶液',
  2445. # '佳美舒阿奇霉素肠溶胶囊4'
  2446. # '三九复方金银花颗粒10g*8袋'
  2447. # '999精氨酸布洛芬颗粒'
  2448. # '999强力枇杷露150ml'
  2449. # '999强力枇杷露' #同时支持120,150和225ml
  2450. # '顺峰酮康他索乳膏' #包含10g和20g两个规格 10g 不低于7.5 20g 不低于12.5 '顺峰康王酮康他索乳膏'
  2451. # '999板蓝根颗粒10g*20' #不低于26.9
  2452. # '999复方氨酚烷胺胶囊12粒' #不低于17.9 #统一成:999复方氨酚烷胺胶囊
  2453. # '999复方氨酚烷胺胶囊10粒' #禁止挂网 #统一成:999复方氨酚烷胺胶囊
  2454. # '999复方氨酚烷胺胶囊6粒' #禁止挂网 #统一成:999复方氨酚烷胺胶囊
  2455. # '999咽炎片0.26g*12片' #不低于13.5 #999咽炎片0.26g*12片*2板改成 999咽炎片0.26g*12片
  2456. # '999荆防颗粒' #美团没有数据 #禁止挂网 拼多多也没数据
  2457. # '999小儿感冒宁颗粒' #禁止挂网 999小儿感冒宁颗粒2.5g*10袋 改成 999小儿感冒宁颗粒
  2458. # '999维生素C咀嚼片'
  2459. # '999感冒灵颗粒10g*9袋' #不低于15.5
  2460. # '999皮炎平复方醋酸地塞米松乳膏20' #不低于12.5
  2461. # '三九胃泰颗粒20g*6袋' #不低于13.5
  2462. # '999复方氨酚烷胺胶囊'
  2463. # '999感冒灵胶囊' #仅限999官旗店
  2464. # '速复康磷酸奥司他韦胶囊75mg*10' #美团没数据 # 禁止挂网 999磷酸奥司他韦胶囊75mg*10 改成 速复康磷酸奥司他韦胶囊75mg*10
  2465. # '999小儿感冒颗粒6g*10' #不低于24.9
  2466. # '999抗病毒口服液10ml',
  2467. # '999蒲地蓝消炎片',
  2468. # '999小柴胡颗粒10g*15',
  2469. # '999复方感冒灵颗粒',
  2470. # '999必无忧盐酸特比萘芬乳膏15'
  2471. # '999复方板蓝根颗粒15g*15袋'
  2472. # '999速复康布洛芬缓释胶囊'
  2473. # '999精氨酸布洛芬颗粒'
  2474. # '999强力枇杷露225ml'
  2475. # 2025-08-01最新 其中 藿香正气合剂两种规格 10支和6支 抗病毒口服液 12支和18支 蒲地蓝 24片 36片和44片 枇杷露225ml 小柴胡颗粒9袋和15袋 养胃舒 6袋 复方感冒灵颗粒15袋,
  2476. # 曲安奈德益康唑乳膏 30g 葡萄糖酸锌口服溶液 12支 18支 24支和30支,
  2477. # 1、999止泻利颗粒15g*8 没有数据 2、三九胃泰养胃舒颗粒8袋 没有数据 3、999三蛇胆川贝膏138 没有数据 4、999强力枇杷露16袋 没有数据 5、999复方苦参肠炎康片12片 6、999冰连清咽 没有数据
  2478. #############以下是日常采集的品规start##################
  2479. # '999养胃舒颗粒10g*10',
  2480. # '999复方感冒灵颗粒',
  2481. # '999感冒清热颗粒12g*18' #禁止挂网
  2482. # '999小儿感冒宁颗粒' #禁止挂网 999小儿感冒宁颗粒2.5g*10袋 改成 999小儿感冒宁颗粒
  2483. # '999小儿咽扁颗粒8g*10袋' #仅限999官旗店
  2484. # '999布洛芬混悬液'
  2485. #########2026春节采集的品规start################################
  2486. # '999藿香正气合剂'
  2487. # '999糠酸莫米松凝胶15',
  2488. # '999抗病毒口服液10ml*10'
  2489. # '999抗病毒口服液10ml*12'
  2490. # '999强力枇杷露225ml',
  2491. # '999糠酸莫米松凝胶10' #不低于26.9
  2492. #########2026春节采集的品规end################################
  2493. ############以下是日常采集的品规start###########################
  2494. search_key_list = [
  2495. '999糠酸莫米松凝胶10',
  2496. '999糠酸莫米松凝胶15g',
  2497. ]
  2498. #切
  2499. # '999糠酸莫米松凝胶10' #不低于26.9 √#304
  2500. # '999布洛芬混悬液100ml' √#0可不采
  2501. # search_key_list = [
  2502. # '999糠酸莫米松凝胶10',
  2503. # ] # 参苓健脾胃颗粒 香砂平胃颗粒 舒肝颗粒 清肺化痰丸
  2504. # 设备序列号
  2505. device_id = 'e2899b34'
  2506. # device_id = '369dcf96'
  2507. # device_id = '2e58510'
  2508. # device_id = 'ea4e4eb8'
  2509. # device_id = 'IZTOWWDQT45D49BU'
  2510. # device_id = 'INXCDAIR75FMMFGU'
  2511. # device_id = 'CMKFUSSG99ROR489'
  2512. # device_id = '95b2c764'
  2513. # device_id = 'UCQGF6CQFMU8WKHI'
  2514. # device_id = 'U8ONIJJJS4CELVD6'
  2515. # device_id = 'OVFETO8PCY45E6A6'
  2516. # device_id = 'IRLZAAQCDMHYWKTS'
  2517. # device_id = 'DEZXWKUC7DJBLVPJ'
  2518. # device_id = 'U47HZDRG8XJBBURW'
  2519. # device_id = 'WWRO9LTGG6KFGQCM'
  2520. # device_id = 'GQIRKB7LVOONM7VW'
  2521. # device_id = 'ZDQWUSSWBEDI896T'
  2522. # device_id = '656DTOPRZDEALZ5X'
  2523. # device_id = 'N7ZXBITOSOGMYXQS'
  2524. # device_id = '1462a51f'
  2525. # device_id = '4TZDUGTOAIFMJVGU'
  2526. # device_id = 'GIOFIBRKZTUGJJAE'
  2527. # device_id = 'fcb3c749'
  2528. search_key_length = len(search_key_list)
  2529. for keyword_idx, search_key in enumerate(search_key_list):
  2530. print(f"正在处理第 {keyword_idx+1}/{len(search_key_list)} 个关键词:{search_key}")
  2531. pdd = PDD(search_key, device_id)
  2532. # pdd.unitest()
  2533. # pdd.main('369dcf96')
  2534. # pdd.main('2e58510')
  2535. pdd.main(device_id, search_key_length, keyword_idx)
  2536. if __name__ == '__main__':
  2537. main()