get_app_pdd__data_10.py 137 KB

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