Advertisement
Josif_tepe

Untitled

May 3rd, 2025
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. import csv
  2.  
  3. import requests
  4. import re
  5. from textblob import TextBlob
  6. import requests
  7. def analyze_text(text):
  8.     analyzer = TextBlob(text)
  9.     polarity = analyzer.sentiment.polarity
  10.  
  11.     if polarity > 0:
  12.         return "positive"
  13.     elif polarity < 0:
  14.         return "negative"
  15.     else:
  16.         return "neutral"
  17.  
  18.  
  19.  
  20. if __name__ == "__main__":
  21.     quotes = []
  22.     for page in range(1, 11):
  23.         req = requests.get(f'https://quotes.toscrape.com/page/{page}/')
  24.         text = req.text
  25.  
  26.         for match in re.finditer(r'''<span class="text" itemprop="text">([^<]+)''', text, re.DOTALL | re.IGNORECASE):
  27.             quote = match.group(1)
  28.             quote = quote.replace('&#39;', "'")
  29.             quote = quote[1:-1]
  30.             quotes.append(quote)
  31.  
  32.     positive_quotes = []
  33.     negative_quotes = []
  34.     neutral_quotes = []
  35.     for quote in quotes:
  36.         sent = analyze_text(quote)
  37.  
  38.         if sent == "positive":
  39.             positive_quotes.append(quote)
  40.         elif sent == "negative":
  41.             negative_quotes.append(quote)
  42.         else:
  43.             neutral_quotes.append(quote)
  44.  
  45.     with open("sentiment.csv", "w") as f:
  46.         c = csv.writer(f)
  47.         c.writerow(["Positive", "Negative", "Neutral"])
  48.  
  49.         max_size = max(len(positive_quotes), max(len(neutral_quotes), len(negative_quotes)))
  50.  
  51.         for i in range(0, max_size):
  52.             pos_text = ''
  53.             if i < len(positive_quotes):
  54.                 pos_text = positive_quotes[i]
  55.  
  56.             neg_text = ''
  57.             if i < len(negative_quotes):
  58.                 neg_text = negative_quotes[i]
  59.  
  60.             neut_text = ''
  61.             if i < len(neutral_quotes):
  62.                 neut_text = neutral_quotes[i]
  63.  
  64.             c.writerow([pos_text, neg_text, neut_text])
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement