每次运行代码时,我都会不断收到以下错误消息:
Attempting to parse Socialblade: https://socialblade.com/youtube/user/aantonop
Got no_of_videos: 325
Internal Server Error: /api/connect_youtube/
Traceback (most recent call last):
File "/Users/test/projects/my-api/api/views.py", line 160, in post
response.update(scraper.collect_creator_data(request.data['youtubeChannelUsername']))
File "/Users/test/projects/my-api/api/scraper.py", line 23, in collect_creator_data
process_socialblade(username)
File "/Users/test/projects/my-api/api/scraper.py", line 68, in process_socialblade
if no_of_subscribers:
UnboundLocalError: local variable 'no_of_subscribers' referenced before assignment
我怀疑是由于在脚本中使用了全局变量,但不确定如何纠正它。我已经设置了可以通过多种方法处理的全局变量。
我该如何解决?下面的代码:
from bs4 import BeautifulSoup
from test.settings.base import YOUTUBE_URL, YOUTUBE_API_KEY
from urllib.request import Request, urlopen
import ssl
no_of_videos = 0
no_of_subscribers = 0
no_of_views = 0
avg_views = 0
like_count = 0
dislike_count = 0
channel_id = None
photo = None
description = None
start_date = None
title = None
keywords = None
sb_grade = None
def collect_creator_data(username):
process_socialblade(username)
process_youtube(username)
return {"id": channel_id,
"photo": photo,
"title": title,
"like_count": like_count,
"dislike_count": dislike_count,
"keywords": keywords,
"description": description,
"start_date": start_date,
"no_of_videos": no_of_videos,
"no_of_subscribers": no_of_subscribers,
"no_of_views": no_of_views,
"avg_views": avg_views,
"sb_grade": sb_grade
}
def process_socialblade(username):
socialblade_user_url = 'https://socialblade.com/youtube/user/' + username
context = ssl._create_unverified_context()
print('Attempting to parse Socialblade:', socialblade_user_url)
q = Request(socialblade_user_url)
q.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) '
'Chrome/23.0.1271.64 Safari/537.11')
q.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
q.add_header('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')
q.add_header('Accept-Encoding', 'none')
q.add_header('Accept-Language', 'en-US,en;q=0.8')
q.add_header('Connection', 'keep-alive')
socialblade_user_html = urlopen(q, context=context).read()
if socialblade_user_html:
soup = BeautifulSoup(socialblade_user_html, "html.parser")
youtube_user_top_info_list = soup.findAll('div', attrs={'class': 'YouTubeUserTopInfo'})
for i in youtube_user_top_info_list:
try:
no_of_videos = i.find('span', id='youtube-stats-header-uploads').get_text()
except AttributeError:
pass
if no_of_videos:
print('Got no_of_videos:', no_of_videos)
try:
no_of_subscribers = i.find('span', id='youtube-stats-header-subs').get_text()
except AttributeError:
pass
if no_of_subscribers:
print('Got no_of_subscribers:', no_of_subscribers)
try:
no_of_views = i.find('span', id='youtube-stats-header-views').get_text()
except AttributeError:
pass
if no_of_views:
print('Got no_of_views:', no_of_views)
else:
print('Could not parse Socialblade:', socialblade_user_html)
我应该在process_socialblade
中创建全局变量吗?