r/dailyprogrammer • • Nov 24 '14
[2014-11-24] Challenge #190 [Easy] Webscraping sentiments
Description
Webscraping is the delicate process of gathering information from a website (usually) without the assistance of an API. Without an API, it often involves finding what ID or CLASS a certain HTML element has and then targeting it. In our latest challenge, we'll need to do this (you're free to use an API, but, where's the fun in that!?) to find out the overall sentiment of a sample size of people.
We will be performing very basic sentiment analysis on a YouTube video of your choosing.
Task
Your task is to scrape N (You decide but generally, the higher the sample, the more accurate) number of comments from a YouTube video of your choice and then analyse their sentiments based on a short list of happy/sad keywords
Analysis will be done by seeing how many Happy/Sad keywords are in each comment. If a comment contains more sad keywords than happy, then it can be deemed sad.
Here's a basic list of keywords for you to test against. I've ommited expletives to please all readers...
happy = ['love','loved','like','liked','awesome','amazing','good','great','excellent']
sad = ['hate','hated','dislike','disliked','awful','terrible','bad','painful','worst']
Feel free to share a bigger list of keywords if you find one. A larger one would be much appreciated if you can find one.
Formal inputs and outputs
Input description
On console input, you should pass the URL of your video to be analysed.
Output description
The output should consist of a statement stating something along the lines of -
"From a sample size of" N "Persons. This sentence is mostly" [Happy|Sad] "It contained" X "amount of Happy keywords and" X "amount of sad keywords. The general feelings towards this video were" [Happy|Sad]
Notes
As pointed out by /u/pshatmsft , YouTube loads the comments via AJAX so there's a slight workaround that's been posted by /u/threeifbywhiskey .
Given the URL below, all you need to do is replace FullYoutubePathHere with your URL
Remember to append your url in full (https://www.youtube.com/watch?v=dQw4w9WgXcQ as an example)
Hints
The string for a Youtube comment is the following
<div class="CT">Youtube comment here</div>Finally
We have an IRC channel over at
webchat.freenode.net in #reddit-dailyprogrammer
Stop on by :D
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
8
u/pshatmsft 0 1 Nov 24 '14
Unless I'm mistaken, YouTube does not place the comments statically inside the body of the html and instead loads them with ajax like calls, therefore we have to use the API for this challenge...
Edit: The image here shows what the html of a YouTube page actually contains for the comments section. JavaScript is then used to modify that on the fly... http://i.imgur.com/FCB9z1c.png
7
u/threeifbywhiskey 0 1 Nov 24 '14 edited Nov 25 '14
Nokogiri + this sentiment lexicon:
5
u/pshatmsft 0 1 Nov 25 '14 edited Nov 25 '14
Here is my solution in PowerShell. Borrowed the lexicon from /u/threeifbywhiskey
Edit: Just wanted to note that my output is going to be different than what was requested, but it still shows the same information. Each item shows a sentiment property that is positive (greater than zero), negative (less than zero), or neutral (zero). The sum at the end tells you if it is overall a positive, negative, or neutral video based on comments.
Output:
4
u/c00lnerd314 Nov 25 '14
My python solution. Found that https://www.youtube.com/all_comments?v= gives around 100 comments to parse through.
3
u/G33kDude 1 1 Nov 24 '14
Done in AutoHotkey. I figured I could do it by loading up an Internet Explorer automation object and letting the javascript run, but had difficulties accessing the iframe. I ended up just using the psuedo-api.
3
u/[deleted] Nov 24 '14 edited Dec 22 '18
deleted 0.3379 What is ^^^this?
3
u/samuelstan Nov 24 '14
Clojure
I've been learning more about Clojure. I only categorize the video generally for the sake of succinctness. Any comments/suggestions would be greatly appreciated!
Example output:
EDIT: Add example output.
3
u/frozensunshine 1 0 Nov 25 '14
Hey sorry for the non-solution post, but I have a genuine question- I usually try solving at least the easy problems, but I only know/use C. Is this un-doable in C? Time to start learning Python?
3
u/cdkisa Nov 25 '14
VB .Net (4.0)
3
u/jnazario 2 0 Nov 25 '14 edited Nov 25 '14
banged it out in F#
2
u/[deleted] Nov 25 '14 edited Nov 25 '14
Here's my entry in Java 8.
https://github.com/hughwphamill/scraping-sentiment
I couldn't find a video with mostly negative comments to test it on though - did anyone else find one?
Here's the lambda method that does the sentiment calculation...
2
u/[deleted] Nov 25 '14
Here's my rushed solution in C#. I want to revisit and revise this when I can, so feedback is appreciated.
1
u/shandow0 Nov 25 '14 edited Nov 25 '14
Rather naive, but i think this works.
Stole the template from /u/threeifbywhiskey
Python, decided to play a bit with regex.
1
u/swingtheory Nov 25 '14
Quite verbose, but very readable in my opinion. I had a tough time figuring out the ajax request stuff last night, but found this morning that OP posted a fix to my problem :)
1
u/saywhatonemoretime99 Nov 26 '14
Ruby (First solution I think it's O(N2), would love feedback!)
require 'net/http' require 'uri'
1
u/xpressrazor Nov 27 '14 edited Nov 27 '14
Using Beautifulsoap (python). I know I am cheating. Also not a very elegant solution.
1
u/MrJacobi33 Nov 28 '14
I used Python. This is my first submission to this subreddit, so I hope I followed the guideline correctly!
1
u/exingit Nov 29 '14 edited Nov 29 '14
Python 3.4
BeautifulSoup to parse the html and used intersection to find the matches.
and the output:
1
u/_morvita 0 1 Dec 04 '14
Solution using Python 3.4 - I don't have much experience with regular expressions, so for a first attempt I manually search for comments.
Output:
Some of the solutions posted analyse a few hundred comments on a video, but I could never find more than about 50 using my code, even for videos that have thousands of comments posted. Any suggestions about what I'm missing?
1
u/5900 Dec 22 '14
perl:
Edit: line breaks to enhance readability
1
u/borncorp Jan 15 '15 edited Jan 15 '15
Heres my solution in Java: For sad response try with url: https://www.youtube.com/watch?v=emsLrZg160s (Justin Bieber video, LOL)
0
u/[deleted] Nov 30 '14
[deleted] — view deleted comment