python - 我已经导入了一个函数,但不能使用其中的变量
问题描述
所以我把它作为一个导入的文件:
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 新手。
解决方案
您只将该函数soupIt
导入到您的命名空间中。该变量full
仅对模块是全局的。您还需要full
从 scrhelp 导入。
推荐阅读
- usb - 从 GNU Octave 中与 USB HID 设备对话
- java - 无法从@Controller 类中返回 JSON 数组,抛出 No serializer found for class org.json.JSONObject 错误
- json - 带有 URLSession 的 Swift 中的 SendGrid API 请求
- java - 许多连接用于获取一条记录 VS 一个连接用于使用 java 获取 Mysql 中的所有记录
- php - 提交后数据库表中的值没有更新
- php - 如何在 cakephp 3 中使用 validationDefault 方法
- sql - Oracle - 插入数据时出现“值太大”错误
- visual-studio-code - 如何创建 VS Code 扩展以通过 REST API 显示和更新数据
- android - Android Studio 没有正确加载预览布局
- python - 为 Python 创建 AWS 部署包