I have built a keyword research application and it is running successfully on a local server but when I deployed it to Cpanel
it is throwing me an error:
UnboundLocalError: local variable 'insert' referenced before assignment:
How can I solve this issue?
Here is my code:
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")
Any suggestions would be highly appreciated.
Thanks.
The problem can come from here:
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(',')
and here:
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(',')
It can happen that insert is not being initialized if the condition is not true. You can fix this by initializing the insert
before the for loop.
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(',')
and the other one:
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(',')