我是Python的新手。我刚刚编写了一个新脚本,用于从多个Google分析配置文件中导出一些数据。它的工作非常好,但我确信它的写得非常糟糕。
我真的不知道从哪里开始改进它所以这是我的第一个问题。
我循环浏览个人资料ID列表。对于每个个人资料ID,我执行了几项操作,我使用append
方法。所以我一步一步建立一些列表,但最后我需要重置这些列表。所以我在代码的开头和结尾都有这样的东西:
fullurllist = []
urllist = []
share = []
sharelist = []
sharelist1 = []
end_list = []
我想我应该避免这种情况。我是否需要更改代码的所有逻辑。我还能做些什么来改善这方面。
以下是代码:
# Loop through the profiles_list and get the best pages for each profile
for profile in profiles_list:
response = service.data().ga().get(
ids='ga:' + profile,
start_date='1daysAgo',
end_date='today',
metrics='ga:sessions',
dimensions='ga:pagePath',
sort='-ga:sessions',
filters='ga:sessions>400').execute()
# Catch response.
rawdata = response.get('rows', [])
# Flatten response (which is a list of lists)
for row in rawdata:
urllist.append(row[0])
# Building a list of full url (Hostname + Page path)
fullurllist = [urljoin(base, h) for h in urllist]
# Scraping some data from the url list
for url in fullurllist:
try:
page = urllib2.urlopen(url)
except urllib2.HTTPError as e:
if e.getcode() == 404: # eheck the return code
continue
soup = BeautifulSoup(page, 'html.parser')
# Take out the <div> of name and get its value
name_box = soup.find(attrs={'class': 'nb-shares'})
if name_box is None:
continue
share = name_box.text.strip() # strip() is used to remove starting and trailing
# save the data in tuple
sharelist.append(url)
sharelist1.append(share)
# Format the data scraped
end_list = [int(1000*float(x.replace('k', ''))) if 'k' in x else int(x) for x in sharelist1]
#export in csv
csv_out = open(response.get('profileInfo').get('profileName') + '.csv', 'wb')
mywriter = csv.writer(csv_out)
for row in zip(sharelist, end_list):
mywriter.writerow([row])
csv_out.close()
#reset list
fullurllist = []
urllist = []
share = []
sharelist = []
sharelist1 = []
end_list = []
非常感谢!
答案 0 :(得分:0)
更合适的做法是在for循环的顶部而不是在外面进行声明(fullurlllist = []
)。
他们应该只在循环内生活