Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2012, 05:16 PM   PM User | #1
wman2
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
wman2 is an unknown quantity at this point
Exclamation Complete newbie - help with simple code

Well, relatively simple.

I've got a bunch of links in the format as such:

http://community.giffgaff.com/t5/use.../user-id/24880

On each page, the page title has the username in it, as does the page itself. I need to make some sort of program (no idea what language it would use) to visit each link and grab the username from it, and accumulate all the usernames in the fashion user1,user2,user3 etc.

Any idea how to work that? Thanks!
wman2 is offline   Reply With Quote
Old 10-10-2012, 05:21 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You cannot do that with Javascript. You will need server-side coding. But I have the idea that you would not be able to write such code.

It is your responsibility to die() if necessary….. - PHP Manual
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 10-10-2012, 05:22 PM   PM User | #3
wman2
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
wman2 is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
You cannot do that with Javascript. You will need server-side coding. But I have the idea that you would not be able to write such code.

It is your responsibility to die() if necessary….. - PHP Manual
Ah OK. So you think I'll have to trawl through each of 600 links manually and type out each username?
wman2 is offline   Reply With Quote
Old 10-10-2012, 07:00 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
there are probably much more efficient ways to do this, but as long as you have the user IDs you can put them into the ids array here:

Code:
<body>
<div id="results"></div>
<script type="text/javascript">
function showUser(o){
document.getElementById("results").innerHTML+=o.query.results.body.div.center.div[0].div.div[3].ul.li[2].span.content.split(" ")[1]+"<br>";
}

var ids=[24880,25580,27380]
for (var i = 0; i < ids.length; i++) {
getUser(ids[i])
}

function getUser(num){
if (document.getElementById("giffgaff")){
document.body.removeChild(document.getElementById("giffgaff"));
}
var myurl=encodeURIComponent("http://community.giffgaff.com/t5/user/viewprofilepage/user-id/"+num)
scr=document.createElement("script");
scr.type="text/javascript";
scr.id="giffgaff"
scr.src='http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22'+myurl+'%2F%22&format=json&callback=showUser';
document.body.appendChild(scr);
}
</script>
</body>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
wman2 (10-10-2012)
Old 10-10-2012, 07:09 PM   PM User | #5
wman2
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
wman2 is an unknown quantity at this point
Thumbs up

Quote:
Originally Posted by xelawho View Post
there are probably much more efficient ways to do this, but as long as you have the user IDs you can put them into the ids array here:

Code:
<body>
<div id="results"></div>
<script type="text/javascript">
function showUser(o){
document.getElementById("results").innerHTML+=o.query.results.body.div.center.div[0].div.div[3].ul.li[2].span.content.split(" ")[1]+"<br>";
}

var ids=[24880,25580,27380]
for (var i = 0; i < ids.length; i++) {
getUser(ids[i])
}

function getUser(num){
if (document.getElementById("giffgaff")){
document.body.removeChild(document.getElementById("giffgaff"));
}
var myurl=encodeURIComponent("http://community.giffgaff.com/t5/user/viewprofilepage/user-id/"+num)
scr=document.createElement("script");
scr.type="text/javascript";
scr.id="giffgaff"
scr.src='http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22'+myurl+'%2F%22&format=json&callback=showUser';
document.body.appendChild(scr);
}
</script>
</body>
Xelawho, thank you so so so much! You, sir, are a gentleman and a scholar.

I used the Microsoft Word find and replace to get the user IDs by themselves then put them into your script, it worked wonders! Thank you very much my friend
wman2 is offline   Reply With Quote
Old 10-10-2012, 07:13 PM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
you're welcome. please do not use it for evil
xelawho is offline   Reply With Quote
Old 10-10-2012, 07:15 PM   PM User | #7
wman2
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
wman2 is an unknown quantity at this point
Quote:
Originally Posted by xelawho View Post
you're welcome. please do not use it for evil
I won't don't worry It's just to inform a load of people by PM in a scheme I run about a change but I didn't have their usernames, only their links and it would have taken hours and hours to do it manually.

Thanks again!
wman2 is offline   Reply With Quote
Old 10-10-2012, 07:19 PM   PM User | #8
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
ah, I get it. If you want to format the output differently, just change the innerHTML line - if you do it like this it will put a comma and a space after each username:
Code:
document.getElementById("results").innerHTML+=o.query.results.body.div.center.div[0].div.div[3].ul.li[2].span.content.split(" ")[1]+", ";
xelawho is offline   Reply With Quote
Old 10-10-2012, 07:21 PM   PM User | #9
wman2
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
wman2 is an unknown quantity at this point
Quote:
Originally Posted by xelawho View Post
ah, I get it. If you want to format the output differently, just change the innerHTML line - if you do it like this it will put a comma and a space after each username:
Code:
document.getElementById("results").innerHTML+=o.query.results.body.div.center.div[0].div.div[3].ul.li[2].span.content.split(" ")[1]+", ";
Nice thanks, I've got it how I want it using Word for now but I'll change it for next time
wman2 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:11 AM.


Advertisement
Log in to turn off these ads.