get_app_pdd__data_8.py 136 KB

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