如何在多线程中执行单个函数并在python中循环创建线程实例?

1)我有一个产品链接列表,它包含 3385 个链接

2)我有一个函数 get_pro_info(link) 它获取产品链接并将项目附加到 json 文件。

3)我希望 selenium 打开 5 个浏览器和 5 个并行链接并获取产品信息并附加到文件或列表中。

或 3)硒打开 1 个浏览器和 5 个选项卡(有 5 个链接)并附加文件。

问题我如何在我的代码上应用线程?

我的代码...

new_url=''

def get_pro_info(pro_url):

    driver = webdriver.Chrome(executable_path=r'C:\Users\Beenu\PycharmProjects/chromedriver.exe')

    try:

        new_url = 'https://pk.studiobytcs.com' + pro_url

        print('new product URL: ' + new_url)

        driver.execute_script("window.open('');")

        sleep(1)

        # use to switch control

        driver.switch_to.window(driver.window_handles[0])

        # sleep(1)

        driver.get(new_url)

    except(WebDriverException, selenium.common.exceptions.TimeoutException, Exception) as e:

        print('There is error in getting Product by URL in get_pro_info()! \n' + str(e.stacktrace))

        pass

    description_source_code = ''

    # description_soup = BeautifulSoup()

    description_soup: BeautifulSoup = object

    # global description_soup

    try:

        # description_soup = BeautifulSoup('html.parser')

        description: WebElement = driver.find_element_by_xpath(

            '//*[@id="shopify-section-product-template"]/div[2]/div[1]/div/div[2]')

        description_source_code = description.get_attribute("innerHTML")

        description_soup: BeautifulSoup = BeautifulSoup(description_source_code, 'html.parser')

    except NoSuchElementException as e:

        print('Product description taag not found! \n' + str(e.stacktrace))

        pass

    # 179 here

    # This is for getting heading product name


心有法竹
浏览 187回答 1
1回答

SMILET

如果您想迭代列表并始终获得 20 个链接,那么您可以使用range(start, stop, step)withstep=20all_t = []for i in range(0, len(list_of_product_link), 20):     twenty_links = list_of_product_link[i:i+20]     t = threading.Thread(target=get_product_info, args=(twenty_links,))     t.start()     all_t.append(t)# --- later ---for t in all_t:     t.join()或者for i in range(0, len(list_of_product_link), 20):     twenty_links = list_of_product_link[i:i+20]     all_t = []     for link in twenty_links:              t = threading.Thread(target=get_product_info, args=(link,))          t.start()          all_t.append(t)     # --- inside first `for` loop ---     for t in all_t:         t.join()如果您以后不需要您的清单,其他方法很好all_t = []while list_of_product_link:     twenty_links = list_of_product_link[:20]     list_of_product_link = list_of_product_link[20:]     t = threading.Thread(target=get_product_info, args=(twenty_links,))     t.start()     all_t.append(t)# --- later ---for t in all_t:     t.join()或者while list_of_product_link:     twenty_links = list_of_product_link[:20]     list_of_product_link = list_of_product_link[20:]      all_t = []     for link in twenty_links:              t = threading.Thread(target=get_product_info, args=(link,))          t.start()          all_t.append(t)     # --- inside first `for` loop ---     for t in all_t:         t.join()顺便说一句:args=需要元组——即使你只有一个参数,所以你需要,用( )一个元素创建元组。顺便说一句:如果您希望它每时每刻只运行 20 个线程,那么最好查看多处理和Pool(20) from multiprocessing import Pooldef get_product_info(link):    result = ....    return resultif __name__ == '__main__':    with Pool(20) as p:        all_results = p.map(get_product_info, list_of_product_link)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python