Web Scraping Youtube URL on Genius with Request - javascript

I am using request along with cheerio to try to grab the link to a song on genius.com. The URL should be a YouTube link. The problem is that I simply cannot get the 'a' element to return its href attribute. This is my code (cheerio and request are loaded farther up in the script).
request('https://genius.com/Eminem-the-monster-lyrics' , function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
var url = $('a' , 'div.song_media_controls-provider-icon').attr('href');
}
console.log(url);
});
I apologize if its a stupid problem or a stupid post. I'm still learning with all of this. Thank you to anyone for help.

// jQuery cross domain ajax
$.get("https://genius.com/Eminem-the-monster-lyrics").done(function (data) {
console.log(data);
});
// using XMLHttpRequest
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "https://genius.com/Eminem-the-monster-lyrics", true);
xhttp.send();
<!DOCTYPE html>
<html lang="en">
<head>
<title>NK Chaudhary</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="demo">Add here genius.com</div>
</body>
</html>

Related

inject jsp with js result into html>body>div

My question is: Could insert a jsp response (html) in html?
I think using XmlHttpRequest.
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "ajax_info.jsp", true);
xhttp.send();
My question is: But if I have javascript in my jsp that it executes after page loading, is it executed like when I call jsp directly by browser url?
Thanks in advance
For example:
This is index.html
<html>
<head>
<script type="text/javascript" src="app.js"></script>
</head>
<body onload="loadInfo();">
<div id="container"></div>
</body>
This is app.js:
function loadInfo(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("container").innerHTML =this.responseText;
}
};
xhttp.open("GET", "info.html", true);
xhttp.send();
}
This is info.html (i have jsp but i think it is the same..):
<html>
<head>
<script type="text/javascript" src="info.js"></script>
</head>
<body>
<div id="body_info">This is info..</div>
<script type="text/javascript" >
console.log("wait for info..");
info();
</script>
</body>
This is info.js:
function info(){
document.getElementById("body_info").innerHTML ="info.js is executed";
}
If i call info.html, typing url in browser(example http://localhost:8000/info.html), the script is executed and i get
"info.js is executed",instead if i call index.html, maybe the xhr request not return the same but I see "This is info".
how can i resolve and accomplish this problem using xhr?
Thanks
Roberto
When you make ajax called to some page so what ever will there under <body></body> will return as response so in your code this.responseText will be having <script></script> code in it also. You can check if you are using chrome then click on element tab you will see <script></script> also which is return as response .Now,to execute this you can do like below :
function loadInfo() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("container").innerHTML = this.responseText;
//getting the script which is return as response back
var datas = document.getElementById("container").getElementsByTagName("script");
//looping unders <script></script>
for (var i = 0; i < datas.length; i++) {
console.log("inside script executing")
eval(datas[i].innerText); //executing script
}
}
};
xhttp.open("GET", "n.html", true);
xhttp.send();
}
And your script for info.html look like below :
<script>
console.log("wait for info..");
info();
function info() {
document.getElementById("body_info").innerHTML = "info.js is executed";
}
</script>

Serverside send information to vanilla HTML

Is it possible to send information from the serverside to the client side like you would with Pug or EJS but without a view engine?
Right now I am using XHTTP requests to access data but it would be a lot easier to not have to use it so much.
function getAllBears(id){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("allBear").innerHTML = this.responseText;
}
};
xhttp.open("GET", "allBears", true);
xhttp.send();
}
You can insert a script tag in HTML and stringify the variable you have on server side, then read the serialized global variable on window.
response.body = ('
<html>
<head>
...
</head>
<body>
<p>text...</p>
<script>
window.bar = ###
</script>
</body>
</html>
'.replace(###, JSON.stringify(bar))
Be careful that some patterns/chars should be replaced in the result of JSON.stringify, a much safer method is as follows:
function toJSONSafely (obj: any) {
return JSON.stringify(obj)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029')
.replace(/<\/script>/g, '<\\/script>')
}
User your javascript inside the html code i.e inline javascript
as shown below
<html>
<head>
</head>
<body>
<p>use this code </p>
<script>
function getAllBears(id){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("allBear").innerHTML = this.responseText;
}
};
xhttp.open("GET", "allBears", true);
xhttp.send();
}
</script>
</body>
</html>

Accessing JSON from last.fm API

I'm very new to JavaScript and have been looking for a solution for a while with no success. I'm trying to use the Last.fm API to retrieve the currently playing track on my account. This is what I have so far:
<html>
<body>
<p>this is an experiment!</p>
<script type="text/javascript">
const request = new XMLHttpRequest();
request.open('GET', 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='+[MY_USERNAME]+'&api_key='+[MY_API_KEY]+'&format=json');
request.send();
request.onload = () => {
if (request.status === 200) {
console.log("Success");
var song = JSON.parse(request.response).recenttracks.track[0].name;
console.log(song);
}
};
request.onerror = () => {
console.log("error")
};
</script>
</body>
</html>
and I get an error in the console when I open the file in my browser. Any help is appreciated :)
Update: everything worked when I gave it the direct URL, e.g. I took out the +s and put the API key directly in.
I checked your code with the test-account and it works fine. So probably you get the empty result, let's add some checks:
request.onload = () => {
if (request.status === 200) {
// look at the response
console.log(request.response);
const recenttracks = JSON.parse(request.response).recenttracks;
if (!recenttracks.track || !recenttracks.track.length) {
console.log('track is empty');
return;
}
const song = recenttracks.track[0].name;
console.log(song);
}
};
It looks like you should use onreadystatechange to catch the response instead of onload.
Example:
request.onreadystatechange = function() {
if (this.status == 200) {
console.log(this.responseText);
}
};
You can read more about XMLHttp Requests here:
https://www.w3schools.com/xml/ajax_xmlhttprequest_send.asp

HTTP POST 403 Unauthorized

The function "myFunction" makes a call on a authentication API, which returns a token. Im taking the token into the variable "token" and setting it as header in my next request (in the "deploy" function), but im only getting 403 unauthorized. I tried to make the same call with the same token in Postman, which worked perfectly. Am i doing something wrong with the token here?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>API TEST</title>
</head>
<body>
<button onclick="myFunction()">Authenticate</button>
<button onclick="deploy()">Deploy</button>
<div id="result"></div>
<script>
var token = "";
function myFunction() {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
alert(this.status);
alert(this.readyState);
if (this.readyState == 4 && this.status == 200) {
document.getElementById("result").innerHTML = this.responseText;
var json = JSON.parse(this.responseText)
token = json["token"];
}
};
xhr.open("POST", "url");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify({"username": "mariero\\nabilo", "password": "test"}));
}
function deploy() {
alert(token);
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
alert(this.status);
alert(this.readyState);
if (this.readyState == 4 && this.status == 200) {
document.getElementById("result").innerHTML = this.responseText;
}
};
xhr.open("POST", "url");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.setRequestHeader("X-Authorization", token);
xhr.send(JSON.stringify({
"taskRelativePath":"My Tasks\\Siebel_RBT-1_Prod_IE.atmx",
"botRunners":
[{
"client":"D00460.lyse.no",
"user":"mariero\\RBT-LYD-1"
}]
}));
}
</script>
</body>
</html>

POST XMLHTTPRequest in Javascript

I'm not very familiar with APIs in general, but don't let that discourage you from answering this question ;)
I'm trying to pull data using the XMLHTTPRequest object. I was originally going to use a WSDL file, but the API I'm using does not have one. For some reason, when I'm trying to get the response displayed in an HTML file, nothing comes across, not even the error that it didn't connect. The api also requires the use of POST.
Here's the javascript:
window.onload = function startCall() {
var http = new XMLHttpRequest();
var url = "https://api.domain.com";
var params = "Version=2.00&ApiKey=111111111111111111&CallID=001";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", "226");
http.setRequestHeader("Host", "api.domain.com");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
function sendToHtml(url, 'target') {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById('target').innerHTML = req.responseText;
}
}
else {
document.getElementById('target').innerHTML=" Error:\n"+ req.status + "\n" +req.statusText;
}
}
}
And here's the HTML
<html>
<head>
<script type="text/javascript" src="call.js"></script>
</head>
<body>
<span onload="startCall" id="target">
</span>
</body>
</html>
Thankyou for any help
POST requests between domains are restricted so browser will not allow it.
There is option to enable it (CORS) by sending correct Access-Control-Allow-Origin, but it have to be done on API server.

Categories