在Django芹菜的上下文中使用函数中的列表

2024-10-01 02:31:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本(scrapt.py),我经常用芹菜节拍来运行它。脚本包含一个列表,其中存储了我刮取的内容。如何在上下文中使用此列表? tasks.py

import time
from celery import shared_task, task
from .scrapt import scrape

@task
def scrape_dev_to():
    
    scrape()
    return

此scrapt.py包含函数

import os
os.chmod('./chromedriver', 0o755)
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

def scrape():

    opt = Options()
    
    opt.add_argument("--remote-debugging-port=9222")
    opt.add_argument("--no-sandbox")
    
    opt.add_argument("--disable-dev-shm-usage")
    driver = webdriver.Chrome(options=opt, 
    executable_path='./chromedriver')
    driver.get('https://www.ig.com/en/forex/markets-forex')


    timeout = 10

    try:
        WebDriverWait(driver, timeout).until(
            EC.visibility_of_element_located(
                (By.XPATH,
                "//div[@class='dynamic-table__cell']")
            )
        )
    except TimeoutException:
        print("Struggling to get the page....Have faith in this buggy script!")
        
    data = []

    while not data:
        for elm in driver.find_elements(By.CSS_SELECTOR, "span[data-field=V2-F-BID]"):
            if elm.text and elm.text != '-': # Maybe check on contains digit
                data.append(elm.text)
        time.sleep(7)

        
    tet =[]
    while not tet:
        for em in driver.find_elements(By.CSS_SELECTOR, "span[data-field=OFR]"):
            if em.text and em.text != '-': # Maybe check on contains digit
                tet.append(em.text)
        time.sleep(7)
    print(data)
    return data
    

这是views函数,我尝试导入scrape函数,出于一个奇怪的原因,我觉得可以在上下文中使用数据列表。我甚至试图在其中添加一个函数,但是我得到了一个位置参数错误。我把一切都搞砸了。我只想在我的模板上连续显示新数据或tet列表

def pricelist(request):
    from .scrapt import scrape
    
    scrape()


           
    context = {
        "price": 123,
        "mydata": data
    }

    return render(request, 'pricelis.html', context)

Tags: 函数textfromimport列表databytime