【发布时间】:2019-10-04 07:51:08
【问题描述】:
这些是脚本的简化版本。我仍在努力学习,所以他们是 很粗糙。
** movieInfo.py **
def castInfo():
castMbrName = inputbox("Provide name")
castPageURL = "https://www.cast_site.com/" + castMbrName
# Most pages match this format. Some don't. For example:
# "http://www.cast_site.com/" + castMbrFirstName + "-" + castMbrSecondName
# or
# "http://www.cast_site.com/" + castMbrAliasName
# To make sure I have the right page, I search for the cast member's name on it.
sourceCastPage = requests.get(castPageURL, headers=hdr).text
soupCastPage = BeautifulSoup(sourceCastPage, "lxml")
try:
castMbrNameOnPage = soupCastPage.find("div", attrs={"class":"xyz"}).text
# If this succeeds, I use castPageURL in the rest of the script.
cast_mbr_url = castPageURL
except (AttributeError,TypeError):
# If not, I want send castMbrName to castPageURL.py
return castMbrName #<<see note below<<
# Getting the value from castURL in castPageURL.url into movieInfo.py
cast_mbr_url = checkCastNameURL()
castInfo()
# Do other things with cast_mbr_url
.
.
.
** castPageURL.py **
# This script will take the cast member's name, search the db and either add the name and url if not
# found or return the url if found.
def checkCastNameURL():
# This is a TinyDB setup containing the names and urls that don't match.
db = TinyDB(castMbr_db.json)
# I want to import the castMbrName variable from movieInfo.py into here.
cast_name_req = castInfo()
# When I manually provided the cast name, it worked. Now, I want to use castMbrName from movieInfo.py
# Search the db for the cast member's name. If not found, get copy of correct url from site and add to db.
if db.search(where("cast_name") == cast_name_req) == []:
cast_name_url = simpledialog.askstring(
title="",
prompt="Paste cast member's url from site:",
initialvalue="",
).strip()
# Add name and url to db
db.insert(
{
"cast_name": cast_name_req,
"cast_url": cast_name_url,
}
)
result = db.get(Query()["cast_name"] == castNameReq)
castURL = result.get("cast_url")
return castURL
我想将变量 castMbrName 从 movieInfo.py 发送到 castPageURL.py 并将 castURL 从 castPageURL.py 发送到 movieInfo.py。这些脚本单独工作,但我想将 castPageURL 合并到 movieInfo 中。
我的问题是我不知道在哪里导入什么。任何见解将不胜感激。
我试过了:
** movieInfo.py **
import castPageURL
From castPageURL import checkCastNameURL # with and without this
** castPageURL **
import movieInfo
from movieInfo import castInfo # with and without this as well
我也试过这个:
** movieInfo.py **
import castPageURL
From castPageURL import checkCastNameURL # with and without this
** castPageURL **
from __main__ import *
我什至尝试在函数内部而不是脚本顶部导入。
** movieInfo.py **
import castPageURL
From castPageURL import checkCastNameURL # with and without this
** castPageURL.py **
def checkCastNameURL():
import movieInfo
from movieInfo import castInfo # with and without this
我无法让这两个脚本识别对方的功能。
另外,在“try/except”块的“except”部分,一旦完成,
return castMbrName
如何让它运行?
cast_mbr_url = checkCastNameURL()
如果需要进一步说明,请告诉我。
【问题讨论】:
-
对不起,这不是很容易理解。为什么你的函数会互相调用?他们之间不传递任何信息,那么为什么要这样做呢?究竟是什么问题;当您尝试这些导入时会发生什么?
-
听起来您想将参数从一个函数传递到另一个函数。长期以来的习惯是“传递参数”,在您的情况下可能如下所示:
cast_mbr_url = checkCastNameURL(castMbrName) -
@DanielRoseman,我想将 cast_mbr 从第一个 py 脚本发送到第二个 py 脚本,看看它是否在数据库中。如果它不在数据库中,我想将 cast_mbr 和正确的 url 添加到数据库中,并将正确的 url 返回到第一个 py 脚本。如果 cast_mbr 在数据库中,我想从第二个 py 脚本返回数据库中的匹配 url,并将其发送到第一个 py 脚本以供稍后处理。我希望能澄清一些事情。
标签: python parameter-passing try-except