-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment_score.ipython
74 lines (53 loc) · 2.22 KB
/
sentiment_score.ipython
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import pandas as pd
#Extract text from articles
article_text = ''. join(map(lambda p: p.text, soup.find_all('p')))
%%%%%%%%%%%%%%%%
import nltk
nltk.download('vader_lexicon')
import requests
from bs4 import BeautifulSoup
from nltk.sentiment.vader import SentimentIntensityAnalyzer
# Define the URL of the article on Yahoo.com
url = "https://www.yahoo.com/news/hezbollah-hesitates-israel-strikes-gaza-171550689.html"
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Parse the page content using BeautifulSoup
soup = BeautifulSoup(response.content, "html.parser")
# Find the article content based on the specific HTML tags
article_content = soup.find('div', class_='caas-body').find_all('p')
# Extract the text from the article content
extracted_text = ""
for paragraph in article_content:
extracted_text += paragraph.get_text() + " "
# Initialize the VADER sentiment analyzer
sia = SentimentIntensityAnalyzer()
# Analyze sentiment of the extracted text
sentiment_scores = sia.polarity_scores(extracted_text)
# Print the sentiment scores
for sentiment, score in sentiment_scores.items():
print(f"{sentiment}: {score}")
else:
print(f"Failed to retrieve the article. Status code: {response.status_code}")
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
import requests
from bs4 import BeautifulSoup
# Define the URL of the article on Yahoo.com
url = "https://www.yahoo.com/news/hezbollah-hesitates-israel-strikes-gaza-171550689.html"
# Send a GET request to the URL
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Parse the page content using BeautifulSoup
soup = BeautifulSoup(response.content, "html.parser")
# Find the article content based on the specific HTML tags
article_content = soup.find('div', class_='caas-body').find_all('p')
# Extract the text from the article content
extracted_text = ""
for paragraph in article_content:
extracted_text += paragraph.get_text() + " "
# Print the extracted text
print(extracted_text)
else:
print(f"Failed to retrieve the article. Status code: {response.status_code}")