get_app_pdd__data.py 133 KB

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