简体   繁体   中英

how to run two activities at the same time in python

i want to make two lists from the same source of html data but i want to collect the data at the same time so as to avoid a possible change in the data.

this is the code i have:


lok = []
num = 6
for _ in range(num):
    inner = driver.find_element_by_xpath(
        "/html/body/div[1]/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[2]/div/div/div[2]/div[5]/span[1]").get_attribute(
        "innerHTML")
    lok.append(inner)
    time.sleep(3600)
    print(lok)


lokk = []
nums = 7
for _ in range(nums):
    inner = driver.find_element_by_xpath(
        "/html/body/div[1]/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[2]/div/div/div[2]/div[5]/span[1]").get_attribute(
        "innerHTML")
    lokk.append(inner)
    time.sleep(3600)
    print(lokk)

this code collects this data:

lok = ['1', '2', '3', '4', '5', '6']

lokk = ['7', '8', '9', '10', '11', '12', '13']

my problem however is that by the time lokk is being collected the time has changed and the values have changed as well. What i want to happen is this:

lok = ['1', '2', '3', '4', '5', '6']

lokk = ['1', '2', '3', '4', '5', '6', '7']

You can append to lokk at the same time as lok and then only collect one more value in the second loop

lok = []
num = 6
for _ in range(num):
    inner = driver.find_element_by_xpath(
        "/html/body/div[1]/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[2]/div/div/div[2]/div[5]/span[1]").get_attribute(
        "innerHTML")
    lok.append(inner)
    lokk.append(inner) # also append to lokk
    time.sleep(3600)
    print(lok)


lokk = []
nums = 1 # only collect 1 more piece of data
for _ in range(nums):
    inner = driver.find_element_by_xpath(
        "/html/body/div[1]/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[2]/div/div/div[2]/div[5]/span[1]").get_attribute(
        "innerHTML")
    lokk.append(inner)
    time.sleep(3600)
    print(lokk)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM