Как записать повторную выходную переменную в список для анализа
Я пытаюсь проанализировать HTML-текст с нескольких веб-страниц для анализа настроений. С помощью сообщества я смог перебрать множество URL-адресов и получить оценку настроения на основе анализа настроений библиотеки textblob, и успешно использовал функцию печати для вывода оценки для каждого URL-адреса. Однако я не смог добиться этого, поместив множество выходных данных, полученных с помощью моей возвращаемой переменной, в список, чтобы я мог использовать его для дальнейшего анализа, используя сохраненные числа для вычисления средних значений и позже отображая результаты в виде графика.
Код с функцией печати:
import requests
import json
import urllib
from bs4 import BeautifulSoup
from textblob import TextBlob
#you can add to this
urls = ["http://www.thestar.com/business/economy/2015/05/19/canadian-consumer-confidence-dips-but-continues-to-climb-in-us-report.html",
"http://globalnews.ca/news/2012054/canada-ripe-for-an-invasion-of-u-s-dollar-stores-experts-say/",
"http://www.cp24.com/news/tsx-flat-in-advance-of-fed-minutes-loonie-oil-prices-stabilize-1.2381931",
"http://www.marketpulse.com/20150522/us-and-canadian-gdp-to-close-out-week-in-fx/",
"http://www.theglobeandmail.com/report-on-business/canada-pension-plan-fund-sees-best-ever-annual-return/article24546796/",
"http://www.marketpulse.com/20150522/canadas-april-inflation-slowest-in-two-years/"]
def parse_websites(list_of_urls):
for url in list_of_urls:
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
# kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
# get text
text = soup.get_text()
# break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
# drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)
#print(text)
wiki = TextBlob(text)
r = wiki.sentiment.polarity
print r
parse_websites(urls)
выход:
>>>
0.10863027172
0.156074203574
0.0766585497835
0.0315555555556
0.0752548359411
0.0902824858757
>>>
но когда я использую возвращаемую переменную для формирования списка, чтобы использовать значения для работы, я не получаю результата, код:
import requests
import json
import urllib
from bs4 import BeautifulSoup
from textblob import TextBlob
#you can add to this
urls = ["http://www.thestar.com/business/economy/2015/05/19/canadian-consumer-confidence-dips-but-continues-to-climb-in-us-report.html",
"http://globalnews.ca/news/2012054/canada-ripe-for-an-invasion-of-u-s-dollar-stores-experts-say/",
"http://www.cp24.com/news/tsx-flat-in-advance-of-fed-minutes-loonie-oil-prices-stabilize-1.2381931",
"http://www.marketpulse.com/20150522/us-and-canadian-gdp-to-close-out-week-in-fx/",
"http://www.theglobeandmail.com/report-on-business/canada-pension-plan-fund-sees-best-ever-annual-return/article24546796/",
"http://www.marketpulse.com/20150522/canadas-april-inflation-slowest-in-two-years/"]
def parse_websites(list_of_urls):
for url in list_of_urls:
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
# kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
# get text
text = soup.get_text()
# break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
# break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
# drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)
#print(text)
wiki = TextBlob(text)
r = wiki.sentiment.polarity
r = []
return [r]
parse_websites(urls)
выход:
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
>>>
Как я могу сделать так, чтобы я мог работать с числами и иметь возможность добавлять, вычитать их из списка, например, [r1, r2, r3...]
Заранее спасибо.
1 ответ
Из приведенного ниже кода вы просите Python вернуть пустой список:
r = wiki.sentiment.polarity
r = [] #creat empty list r
return [r] #return empty list
Если я правильно понял вашу проблему, все, что вам нужно сделать, это:
my_list = [] #create empty list
for url in list_of_urls:
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
for script in soup(["script", "style"]):
script.extract() # rip it out
text = soup.get_text()
lines = (line.strip() for line in text.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
text = '\n'.join(chunk for chunk in chunks if chunk)
wiki = TextBlob(text)
r = wiki.sentiment.polarity
my_list.append(r) #add r to list my_list
print my_list
[r1, r2, r3,...]
Кроме того, вы можете создать словарь с URL-адресом в качестве ключа
my_dictionary = {}
r = wiki.sentiment.polarity
my_dictionary[url] = r
print my_dictionary
{'url1': r1, 'url2: r2 и т. д.)
print my_dictionary['url1']
r1
Словарь может иметь больше смысла для вас, так как было бы легче получать, редактировать и удалять "r", используя URL-адрес, используемый в качестве ключа.
Я немного новичок в Python, так что, надеюсь, другие поправят меня, если это не имеет смысла...