Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import requests
- import re
- from textblob import TextBlob
- import requests
- def analyze_text(text):
- analyzer = TextBlob(text)
- polarity = analyzer.sentiment.polarity
- if polarity > 0:
- return "positive"
- elif polarity < 0:
- return "negative"
- else:
- return "neutral"
- if __name__ == "__main__":
- quotes = []
- for page in range(1, 11):
- req = requests.get(f'https://quotes.toscrape.com/page/{page}/')
- text = req.text
- for match in re.finditer(r'''<span class="text" itemprop="text">([^<]+)''', text, re.DOTALL | re.IGNORECASE):
- quote = match.group(1)
- quote = quote.replace(''', "'")
- quote = quote[1:-1]
- quotes.append(quote)
- positive_quotes = []
- negative_quotes = []
- neutral_quotes = []
- for quote in quotes:
- sent = analyze_text(quote)
- if sent == "positive":
- positive_quotes.append(quote)
- elif sent == "negative":
- negative_quotes.append(quote)
- else:
- neutral_quotes.append(quote)
- with open("sentiment.csv", "w") as f:
- c = csv.writer(f)
- c.writerow(["Positive", "Negative", "Neutral"])
- max_size = max(len(positive_quotes), max(len(neutral_quotes), len(negative_quotes)))
- for i in range(0, max_size):
- pos_text = ''
- if i < len(positive_quotes):
- pos_text = positive_quotes[i]
- neg_text = ''
- if i < len(negative_quotes):
- neg_text = negative_quotes[i]
- neut_text = ''
- if i < len(neutral_quotes):
- neut_text = neutral_quotes[i]
- c.writerow([pos_text, neg_text, neut_text])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement