I'd like to call Python the strongest. Python automation can make money easily

ps: This article is only for technical exchange and should not be used for other purposes.

preparation

Before writing code, you need to do the following preparations:

1. Configure Android ADB development environment

2. Installing pocoui dependency libraries in Python virtual environment

3. APK application of operating shear board

Write code

We realize this function in seven steps: open the target application client, search keywords to the commodity list interface, cut the original picture to get the main picture of the commodity, match the commodity, collect and browse the commodity, buy the commodity, get the order number and screenshot.

Step 1: use Airtest automation to open the target application.

# Target application
package_name = 'Application package name'
activity = 'Home Activity'
​
def __pre(self):
 """
 preparation
 :return:
 """
 # Delete cache file
 remove_cache('./part.jpg', './screenshot.png', './uidump.xml')
​
 home()
 stop_app(package_name)
 start_my_app(package_name, activity)

After opening the application, you can proceed to step 2.

Put the pre obtained keywords into the input box, and then click the search button until the search list appears.

It should be noted that some control elements need to be clicked multiple times to calculate a valid click event.

def __search_good_by_key(self):
 """
 Search for products by keyword
 :return:
 """
 self.poco(id_page_main_button_search).wait(5).click()
​
 perform_view_input(self.poco, id_page_search_edittext_search, self.key)
​
 # Click search
 self.poco(id_page_search_button_search).wait_for_appearance()
 while self.poco(id_page_search_button_search).exists():
 print('Click once to search')
 perform_view_id_click(self.poco, id_page_search_button_search)
​
 # Wait for the list to load
 self.poco(id_page_goods_rv).wait_for_appearance()

Step 3: cut the original drawing. You need to delete the redundant white area in the original drawing and only keep the main drawing of goods on the left.

By traversing the x-axis and y-axis, get the color value of each pixel. If it is continuous white, make a mark, and then get the upper, lower, left and right coordinate values of the main map. Finally, cut it with cv2 library to get the main map of goods.

def crop_main_img(img_path):
 """
 Get commodity master map
 :return:
 """
 img = cv2.imread(img_path)
 # The pixels value is made up of three primary colors
 size = img.shape
​
 img_height = size[0]
 img_width = size[1]
 channels = size[2]
​
 # 1080*458
 print(f'image width:{img_width},height:{img_height}'
​
 # Identifies the array for the x and y axes
 arr_x = []
 arr_y = []
​
 # Traverse the width to get the x-axis coordinates of the main graph
 for x in range(img_width):
 is_black = True
​
 # Ergodic height
 for y in range(img_height):
 # Get color value
 color_position = img[y, x]
 if (color_position == color_white).all():
 pass
 else:
 is_black = False
​
 arr_x.append(is_black)
​
 # Traverse the height to get the y-axis coordinates of the main graph
 for y in range(img_height):
 is_black = True
​
 # Ergodic height
 for x in range(img_width):
 # Get color value
 color_position = img[y, x]
 if (color_position == color_white).all():
 pass
 else:
 is_black = False
​
 arr_y.append(is_black)
​
 position_x = get_space_index(arr_x)
 position_y = get_space_index(arr_y)
​
 main_img_path = "./head_img.jpeg"
​
 # shear
 # The clipping coordinates are [y0:y1, x0:x1]
 cropped = img[position_y[0]:position_y[1], position_x[0]: position_x[1]]
 cv2.imwrite(main_img_path, cropped)
​
 return main_img_path

Step 4, product matching

After getting the main map of goods, use Airtest to search the elements on the current page. If it is not found, it will slide to the next element; Otherwise, get the coordinates of the matching goods.

def __search_good_from_list(self):
 """
 Match items from list
 :return:
 """
 # Circular picture search
 while True:
 try:
 pos = loop_find(Template(self.main_img_path), timeout=10, threshold=0.95)
 except TargetNotFoundError:
 print('Slide one page')
 self.__swipe(True)
 else:
 print('eureka')
​
 # Screen width and height
 screen_size = self.poco.get_screen_size()
 print(screen_size)
​
 # Click the coordinate point (width and height)
 # (0.22407407407407406, 0.8550925925925926)
 position_click = (pos[0] / screen_size[0], pos[1] / screen_size[1])
 print(position_click)
 self.poco.click(position_click)
 break

Step 5: collect and browse products

After jumping to the product information interface, collect the products first, and then jump to the product details page and comment page.

Perform sleep and slide operations within the preset browsing time.

def __browser_good_detail(self):
 """
 Browse products
 :return:
 """
 # Switch to details Tab
 self.poco('com.**:id/taodetail_nav_bar_tab_text', text='details').click()
​
 # Sliding duration: self browser_ detail_ time
 browser_start = datetime.datetime.now()
 browser_end = browser_start
​
 while (browser_end - browser_start).seconds < self.browser_detail_time:
 # Sleep for a while
 time.sleep(random.randint(2, 5))
​
 # Slide once
 self.__swipe(True)
​
 # End time
 browser_end = datetime.datetime.now()
​
 print('The details page has been viewed')

Step 6: purchase goods

The purchase of goods is very simple, just click a purchase button to complete; For security reasons, select the receiving address here and enter the payment password manually.

def __buy_good(self):
 """
 Purchase goods
 :return:
 """
 # Buy now
 self.poco('**/detail_main_sys_button', text='Buy now').click()
​
 # Select item properties
 sleep(10)
​
 # Confirm purchase
 self.poco('**/confirm_text', text='determine').parent().click()
​
 # place order
 self.poco(text='place order').click()
​
 # Manually enter password or fingerprint
 sleep(10)

Step 7: get the order ID and product screenshot

Through the observation of Monitor, it is found that the order number text element is difficult to get through attributes or child and parent relationships.

You can click the copy button to paste the order number into the clipboard of the system, and then use the App adb + clipper to get the content in the clipboard.

def __get_order_no(self):
 """
 Get order number
 :return:
 """
 global copy_element
 while True:
 # Due to the limitation of the mobile phone screen, the first page of the [copy] button may not be displayed
 try:
 copy_element = self.poco(text='copy')
 except Exception as e:
 print('No element found, slide down one page')
 self.__swipe(True)
​
 break
​
 # Copy to cut version
 copy_element.click()
​
 # Get the data from the shear board
 result = exec_cmd('adb shell am broadcast -a clipper.get')[1]
​
 # Match order number
 result = re.findall(r'data="(.*)"', result)
​
 order_no = ''
​
 if result and len(result) > 0:
 order_no = result[0]
​
 print(order_no)
​
 return order_no

Then use the adb command to intercept the current screen, and then save it to the PC, that is, all the operations are completed.

def get_order_pic(self):
 """
 Get order screenshot interface
 :return:
 """
 screenshot_pic_result = './order_screenshot.png'
​
 # Capture the current screen of the mobile phone
 exec_cmd('adb shell /system/bin/screencap -p /sdcard/screenshot.png')
​
 # Save to PC
 exec_cmd('adb pull /sdcard/screenshot.png %s' % screenshot_pic_result)
​
 return screenshot_pic_result

Result conclusion

Through the above steps, you can complete a series of operations such as automatic commodity selection, browsing and purchase.

It should be added that due to the inconsistent resolution of mobile phones, there will be some errors in the main map matching of goods; However, because the width and height ratio of the main image are consistent, the purpose of adaptation can be achieved by scaling the image.

About Python technology reserve

It's good to learn Python well, whether in employment or sideline, but to learn python, you still need to have a learning plan. Finally, let's share a full set of Python learning materials to help those who want to learn Python!

1, Python learning routes in all directions

All directions of Python is to sort out the commonly used technical points of Python and form a summary of knowledge points in various fields. Its purpose is that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.

2, Learning software

If a worker wants to do well, he must sharpen his tools first. The commonly used development software for learning Python is here, which saves you a lot of time.

3, Getting started video

When we watch videos, we can't just move our eyes and brain without hands. The more scientific learning method is to use them after understanding. At this time, the hand training project is very suitable.

4, Actual combat cases

Optical theory is useless. We should learn to knock together and practice, so as to apply what we have learned to practice. At this time, we can make some practical cases to learn.

5, Interview materials

We must learn Python in order to find a high paying job. The following interview questions are the latest interview materials from front-line Internet manufacturers such as Alibaba, Tencent and byte, and Alibaba boss has given authoritative answers. After brushing this set of interview materials, I believe everyone can find a satisfactory job.


This complete set of Python learning materials has been uploaded to CSDN. Friends can scan the official authentication QR code of CSDN below on wechat and get it for free [guaranteed to be 100% free]

Keywords: Python Programmer AI

Added by hostcord on Sun, 20 Feb 2022 01:51:21 +0200