...

View Full Version : Complete newbie - help with simple code



wman2
10-10-2012, 05:16 PM
Well, relatively simple.

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

http://community.giffgaff.com/t5/user/viewprofilepage/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!

Philip M
10-10-2012, 05:21 PM
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

wman2
10-10-2012, 05:22 PM
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? :(

xelawho
10-10-2012, 07:00 PM
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:



<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>

wman2
10-10-2012, 07:09 PM
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:



<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 :)

xelawho
10-10-2012, 07:13 PM
you're welcome. please do not use it for evil :D

wman2
10-10-2012, 07:15 PM
you're welcome. please do not use it for evil :D

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! :thumbsup:

xelawho
10-10-2012, 07:19 PM
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:


document.getElementById("results").innerHTML+=o.query.results.body.div.center.div[0].div.div[3].ul.li[2].span.content.split(" ")[1]+", ";

wman2
10-10-2012, 07:21 PM
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:


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 :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum