Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to travel around the world

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to travel around the world with Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to travel around the world with Python".

1 、. Preparatory work

Before you start writing a script, you need to do the following preparations:

1. An Android phone after Root

2. Configure the Android ADB development environment

3. Install pocoui dependent libraries in Python virtual environment

4. Virtual location application FackLocation

5. Choose a scenic spot, download some photos of the corresponding scenic spot, and prepare an excellent text.

2 、. Write code

There are six steps to complete this operation, namely: get longitude and latitude through location, start virtual location service, import pictures into mobile photo albums, open moments, select photo albums, edit content and select geographic location, and publish moments.

First, use the adb command to get the application package name and LaunchActivity of FackLocation and Wechat.

Adb shell dumpsys activity | grep-I run

After getting the application package name and LaunchActivity, you can easily open FackLocation applications and add geographic locations using Airtest and Monitor tools.

After arriving at the search interface, the search results can not be found under Monitor.

Therefore, with the help of the pointer position in the developer's options, we can get the coordinates of the first item of the list, and use adb to perform the click operation, which can complete the operation of selecting longitude and latitude.

Def _ _ mock_location (self): "" Simulation location: return: "" home () stop_app (package_name_location) start_my_app (package_name_location Activity_location) # Click to add location self.poco ('com.lerist.fakelocation:id/fab'). Click () # Click to search self.poco (' com.lerist.fakelocation:id/m_item_search'). Click () # enter destination self.poco ('com.lerist.fakelocation:id/l_search_panel_et_input') .set_text (self.location) in the input box Sleep (2) # wide, High size = self.poco.get_screen_size () # cannot be found in the UI tree due to the selection result Here we use coordinates to perform the click operation adb_click (500,283) # to determine the location while self.poco ('com.lerist.fakelocation:id/a_map_btn_done'). Exists (): self.poco (' com.lerist.fakelocation:id/a_map_btn_done'). Click ()

The second step is to determine whether the service is enabled by the text content of the element. if it is not enabled, complete the opening of the location service with one click.

Def _ _ start_mock (self): "" enable the simulation location: return: "" mock_element = self.poco ('com.lerist.fakelocation:id/f_fakeloc_tv_service_switch') if mock_element.get_text () =' start the simulation': mock_element.click () # wait for the simulation to be started to complete the self .poco ('com.lerist.fakelocation:id/f_fakeloc_tv_service_switch' Text=' stops simulation') .wait_for_appearance () else: pass print ('simulation is on')

Step 3, import the pictures on the PC side into the mobile album directory using the adb push command.

# pictures waiting to be sent locally files = get_all_files ('. / image/') # Total number of pictures to be sent self.image_num = 9 if len (files) > 9 else len (files) # Mobile album directory phone_image_path = 'sdcard/DCIM/Camera/' # one picture is imported into the mobile album for file in files [: self.image_num]: exec_cmd (' adb push% s% s'% (file) Phone_image_path))

It is important to note that after importing the pictures, you need to send another refresh broadcast before they can be found in the album.

# update mobile photo album exec_cmd ('adb shell am broadcast-an android.intent.action.MEDIA_SCANNER_SCAN_FILE-d file:///%s'% phone_image_path)

Step 4, also use Airtest to open the Wechat client, and then automatically jump to the moments interface.

Def _ _ open_friend_circle (self): "" Open moments: return: "" # waiting for Wechat App self.poco (text=' Wechat'). Wait_for_appearance () self.poco (text=' address book'). Wait_for_appearance () self.poco (text=' Discovery'). Wait_for_appearance () self.poco (text=) Wait_for_appearance () print ('Wechat is fully open') # Click [find] Tab self.poco ('com.tencent.mm:id/djv'] Text=' found'). Parent (). Click () # Open moments self.poco ('android:id/title', text=' moments'). Click () # wait for moments to dynamically load full self.poco ('com.tencent.mm:id/eyx'). Wait_for_appearance ()

Step 5, select the picture from the mobile photo album.

The photos of the mobile phone album are sorted by time. According to the number of pictures pushed above, you can select a specific number of photos in front of the album.

# Select the specified number of photos cbs = self.poco ('com.tencent.mm:id/ek8'). Offspring (' com.tencent.mm:id/bwn') index = 0 # Select a fixed number of photos for cb in cbs: if index < self.image_num: cb.click () else: break index + = 1 # confirm to select the picture self.poco ('com.tencent.mm:id/lm'). Click ()

Step 6, enter the text and select a specific location.

Because the virtual positioning is not refreshed in time, in order to ensure the accuracy of the location, positioning is carried out twice in a row.

Def _ _ put_content_and_gps (self): "" enter content and location: return: "" # enter the number of times self.poco ('com.tencent.mm:id/d3k'). Set_text (self.msg) # is located in moments. It usually takes two times to location_count = 0 # Click the location icon while True: self.poco ('com.tencent.mm:id/d0a' Text=' location') .click () # waiting for a result to appear in the search list self.poco ('com.tencent.mm:id/du7') .wait_for_appearance () if location_count = 0: # return keyevent (' BACK') location_count + = 1 else: # exclude the first two items of ListView (do not display, urban area) Click the third item (specific location) self.poco ('com.tencent.mm:id/dul') .children () [2] .click ()

Step 7, get the release button at the top, and you can release the news with one click.

Thank you for your reading, the above is the content of "how to travel around the world with Python". After the study of this article, I believe you have a deeper understanding of how to travel around the world with Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report