首页 > 解决方案 > 我已经导入了一个函数,但不能使用其中的变量

问题描述

所以我把它作为一个导入的文件:

from bs4 import BeautifulSoup as bs
import requests
import time

def soupIt(URL, osztaly, pls):
    global full
    global noedit
    global ft
    global ftfull
    global eur
    global eurfull
    global dollar
    global dollarfull
    content = requests.get(URL)
    soup = bs(content.text, 'html.parser')
    do = soup.find_all(pls, class_ = osztaly)[0].get_text()
    noedit = do.strip()
    do = noedit.replace('.', '')
    do = do.replace(',', '')
    do = do.replace(' ', '')
    do = do.replace('  ', '')
    do = do.replace('Ft', '')
    do = do.replace('$', '')
    full = do.replace('€', '')
    ft = noedit + " Ft"
    ftfull = full + " Ft"
    eur = noedit + " €"
    eurfull = full + " €"
    dollar = noedit + " $"
    dollarfull = full + " $"
    

我尝试在我尝试过的文件中使用它soupIt()

from scrhelp import soupIt
soupIt("https://www.emag.hu/aoc-gaming-monitor-ips-23-8-full-hd-1-ms-144hz-freesync-dp-hdmi-fekete-24g2u-bk/pd/D0HVSGBBM/",'product-new-price', "")
print(full)
 

它给了我错误:

回溯(最近一次调用):文件“D:\Donát\Programozás\Phyton\webscraper\apitest.py”,第 3 行,在 print(full) NameError: name 'full' is not defined

我试图full在我尝试使用它的文件中将它设置为全局全局,但它不起作用,而且我是 python 新手。

标签: python

解决方案


您只将该函数soupIt导入到您的命名空间中。该变量full仅对模块是全局的。您还需要full从 scrhelp 导入。


推荐阅读