猿问

我正在尝试解析网站并生成积极、中立或消极的情绪分析

我正在尝试从 CNBC 网站获取非常基本的情绪分析。我把这段代码放在一起,效果很好。


from bs4 import BeautifulSoup

import urllib.request

from  pandas import DataFrame


resp = urllib.request.urlopen("https://www.cnbc.com/finance/")

soup = BeautifulSoup(resp, from_encoding=resp.info().get_param('charset'))

    

substring = 'https://www.cnbc.com/'


df = ['review']

for link in soup.find_all('a', href=True):

    print(link['href'])

    if (link['href'].find(substring) == 0): 

        # append

        df.append(link['href'])


#print(link['href'])



#list(df)

# convert list to data frame

df = DataFrame(df)

#type(df)

#list(df)


# add column name

df.columns = ['review']


# clean up

df['review'] = df['review'].str.replace('\d+', '')


# Get rid of special characters

df['review'] = df['review'].str.replace(r'[^\w\s]+', '')



from nltk.sentiment.vader import SentimentIntensityAnalyzer

sid = SentimentIntensityAnalyzer()

df['sentiment'] = df['review'].apply(lambda x: sid.polarity_scores(x))

def convert(x):

    if x < 0:

        return "negative"

    elif x > .2:

        return "positive"

    else:

        return "neutral"

df['result'] = df['sentiment'].apply(lambda x:convert(x['compound']))

df['result']

当我运行上面的代码时,我得到了正面和负面的信息,但这些并没有映射到原始的“评论”。如何在数据框中、每个链接的语言旁边显示每种情绪?谢谢!


Smart猫小萌
浏览 92回答 0
0回答
随时随地看视频慕课网APP

相关分类

Python
我要回答