UnboundLocalError: local variable 'insert' referenced before assignment on server side
UnboundLocalError: local variable 'insert' referenced before assignment on server side
我已经构建了一个关键词研究应用程序,它在本地服务器上成功 运行 但是当我将它部署到 Cpanel
时它抛出了一个错误:
UnboundLocalError: local variable 'insert' referenced before assignment:
我该如何解决这个问题?
这是我的代码:
def funcurlscrpping(url):
urldata = requests.get(url)
soup = BeautifulSoup(urldata.content, "html")
title = soup.title.string
print ('TITLE IS :', title)
meta = soup.find_all('meta')
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert = tag.attrs['content']
print(insert)
data = insert.split(',')
return data
def funcurlscrppingwithkeyword(ids):
for id in ids:
videourl= 'https://www.youtube.com/watch?v='+ id
urldata = requests.get(videourl)
soup = BeautifulSoup(urldata.content, "html")
title = soup.title.string
print ('TITLE IS :', title)
meta = soup.find_all('meta')
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert1 = tag.attrs['content']
print(insert1)
data1 = insert1.split(',')
return data1
def GetTags(request):
if request.method == 'GET':
url = request.GET['query']
type = request.GET['type']
valid=validators.url(url)
if valid==True:
obj1=[]
obj1 = funcurlscrpping(url)
if type == 'YouTube':
return JsonResponse({"tags": obj1}, status=200)
else:
res = ['#' + x for x in obj1]
return JsonResponse({"tags": res}, status=200)
else:
search_url = 'https://www.googleapis.com/youtube/v3/search'
params = {
'part': 'snippet',
'q':url,
'key' : settings.YOUTUBE_DATA_API_KEY,
'maxResults' : 2,
}
video_ids = []
r = requests.get(search_url,params = params)
results = r.json()["items"]
for result in results:
video_ids.append(result['id']['videoId'])
obj = []
obj = funcurlscrppingwithkeyword(video_ids)
if type == 'YouTube':
return JsonResponse({"tags": obj}, status=200)
else:
res = ['#' + x for x in obj]
return JsonResponse({"tags": res}, status=200)
else:
return HttpResponse("Request method is not a GET")
如有任何建议,我们将不胜感激。
谢谢。
问题可能来自这里:
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert = tag.attrs['content']
print(insert)
data = insert.split(',')
这里:
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert1 = tag.attrs['content']
print(insert1)
data1 = insert1.split(',')
如果条件不成立,可能会发生插入未被初始化的情况。您可以通过在 for 循环之前初始化 insert
来解决此问题。
insert = "" # some default value that works
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert = tag.attrs['content']
print(insert)
data = insert.split(',')
另一个:
insert = "" # some default value that works
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert1 = tag.attrs['content']
print(insert1)
data1 = insert1.split(',')
我已经构建了一个关键词研究应用程序,它在本地服务器上成功 运行 但是当我将它部署到 Cpanel
时它抛出了一个错误:
UnboundLocalError: local variable 'insert' referenced before assignment:
我该如何解决这个问题?
这是我的代码:
def funcurlscrpping(url):
urldata = requests.get(url)
soup = BeautifulSoup(urldata.content, "html")
title = soup.title.string
print ('TITLE IS :', title)
meta = soup.find_all('meta')
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert = tag.attrs['content']
print(insert)
data = insert.split(',')
return data
def funcurlscrppingwithkeyword(ids):
for id in ids:
videourl= 'https://www.youtube.com/watch?v='+ id
urldata = requests.get(videourl)
soup = BeautifulSoup(urldata.content, "html")
title = soup.title.string
print ('TITLE IS :', title)
meta = soup.find_all('meta')
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert1 = tag.attrs['content']
print(insert1)
data1 = insert1.split(',')
return data1
def GetTags(request):
if request.method == 'GET':
url = request.GET['query']
type = request.GET['type']
valid=validators.url(url)
if valid==True:
obj1=[]
obj1 = funcurlscrpping(url)
if type == 'YouTube':
return JsonResponse({"tags": obj1}, status=200)
else:
res = ['#' + x for x in obj1]
return JsonResponse({"tags": res}, status=200)
else:
search_url = 'https://www.googleapis.com/youtube/v3/search'
params = {
'part': 'snippet',
'q':url,
'key' : settings.YOUTUBE_DATA_API_KEY,
'maxResults' : 2,
}
video_ids = []
r = requests.get(search_url,params = params)
results = r.json()["items"]
for result in results:
video_ids.append(result['id']['videoId'])
obj = []
obj = funcurlscrppingwithkeyword(video_ids)
if type == 'YouTube':
return JsonResponse({"tags": obj}, status=200)
else:
res = ['#' + x for x in obj]
return JsonResponse({"tags": res}, status=200)
else:
return HttpResponse("Request method is not a GET")
如有任何建议,我们将不胜感激。
谢谢。
问题可能来自这里:
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert = tag.attrs['content']
print(insert)
data = insert.split(',')
这里:
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert1 = tag.attrs['content']
print(insert1)
data1 = insert1.split(',')
如果条件不成立,可能会发生插入未被初始化的情况。您可以通过在 for 循环之前初始化 insert
来解决此问题。
insert = "" # some default value that works
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert = tag.attrs['content']
print(insert)
data = insert.split(',')
另一个:
insert = "" # some default value that works
for tag in meta:
if 'name' in tag.attrs.keys() and tag.attrs['name'].strip().lower() in ['keywords']:
insert1 = tag.attrs['content']
print(insert1)
data1 = insert1.split(',')