Programming with Python
Lab VI
Subjects: Modules
Submission: FirstNameLastNameLab6.py file. This file should contain Parts A, B, C, or D (any two). Please uncomment all parts of your code before submitting it.
Install Modules
We want to install the needed modules. To do this, we will use Thonny’s Manage Packages tool.
Download additional NLTK Componenets
You need to download additional “Collections” from NLTK by first importing NLTK, then using the download functionality:
import nltk
nltk.download()
Result:
[This screen appears]
For now, navigate to “Models” and download “VADER Sentiment Lexicon”. We will be using this in our example.
SSL Errors
Occasionally when navigating internet data using Python, you’ll encounter SSL errors. If this happens to you, add the following to your script:
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
We will discuss what this block of code does when we learn about error handling.
Part A – Pandas
#Michael Deamer Lab 6
fileName = ‘C:/Users/Administrator/Desktop/Python IO/ tickerInfo.csv’
Note:
Be sure to use the variable fileName with a lowercase f and an uppercase N.
import pandas as pd
df = pd.read_csv(fileName)
maxClose = df[‘Close’].max()
print(maxClose)
closingPrices = list(df[‘Close’])
Note:
We will use this list in Part B.
Part B – MatPlotLib
We want to graph the List created in Part A.
import matplotlib.pyplot as plt
x = range(len(closingPrices))
plt.plot(x, closingPrices)
plt.show()
Result:
Part C – NLTK
We want the computer to determine if a sentence indicates positive, negative, or neutral sentiment using NLTK.
comment = ‘This is the best product I\’ve ever owned!’
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sid = SentimentIntensityAnalyzer()
sentimentScores = sid.polarity_scores(comment)
print(sentimentScores[‘compound’])
Result:
0.6696
Note:
Positive scores are between 0 and 1. Negative scores are between 0 and -1.
Note:
Do not change the variable name, just the sentence.
Part D – Beautiful Soup
We want to scrape data from musicpriceguide.com. More specifically, we would like to get the list of vinyl records from that website. Not all websites are conducive to scrapping. Some reasons we might not be able to scrape a site include:
We want to if our website permits scrapping before moving forward.
Note: At the time this lab was created, there were two matches for Paul McCartney’s record.
Result:
<a style=”font-size:1.1em” href=”28951/Paul-McCartney-I-Don-t-Know-Come-On-To-Me-Rare-2018-white-label-7-035-200.html” title=”Paul McCartney I Don t Know Come On To Me Rare 2018 white label 7 035 200″>Paul McCartney I Don t Know Come On To Me Rare 2018 white label 7 035 200</a>
Note:
The title appears as both as an attribute of an a tag and as text between tags. Neither appear between <script> tags. This indicates that we can get this data using BeautifulSoup.
from bs4 import BeautifulSoup
from urllib.request import urlopen
url = ‘http://www.musicpriceguide.com’
page = urlopen(url)
Note:
Although nothing is displayed to the screen, this code causes Python to navigate to the site and retrieve its html code.
soup = BeautifulSoup(page, ‘html.parser’)
print(soup)
Note:
If we are not able to print the HTML code in this step, the site might block robots and will not be easy to scrape. We may want to find another site.
titleTags = soup.find_all(title = True)
for a in titleTags:
print(a)
Result:
<a href=”29098/Blossom-Toes-We-Are-Ever-So-Clean-Marmalade-UK-Original-Pressing-LP.html” style=”font-size:1.1em” title=”Blossom Toes We Are Ever So Clean Marmalade UK Original Pressing LP”>Blossom Toes We Are Ever So Clean Marmalade UK Original Pressing LP</a>
<a href=”27241/KALEIDOSCOPE-TANGERINE-DREAM-LP-ORIG-UK-1967-FONTANA-1ST-PRESS-RARE-PSYCH.html” style=”font-size:1.1em” title=”KALEIDOSCOPE TANGERINE DREAM LP ORIG UK 1967 FONTANA 1ST PRESS RARE PSYCH”>KALEIDOSCOPE TANGERINE DREAM LP ORIG UK 1967 FONTANA 1ST PRESS RARE PSYCH</a>
[…etc]
for a in titleTags:
print(a.get_text())
Result:
QUEEN crazy little thing 45 RPM 12 RARE COLOMBIA PROMO ONLY PURPLE COLOR
The Rolling Stones Let It Bleed UK Vinyl LP 1969 1st Press Mono EX EX
XTC The Complete and Utter Dukes of Stratosphear Stratosphere 2 LP CD BOX SET
[…etc]
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more
Recent Comments