Home > Net >  how to run two activities at the same time in python
how to run two activities at the same time in python

Time:11-27

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']

CodePudding user response:

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)

  • Related