Random page from a website?
Page 1 of 112 Replies - 811 Views - Last Post: 12 August 2012 - 11:54 PM
#1
Random page from a website?
Posted 11 August 2012 - 05:29 PM
Is this possible? And if so, what tools do I need for it?
Thanks.
Replies To: Random page from a website?
#2
Re: Random page from a website?
Posted 11 August 2012 - 09:15 PM
#3
Re: Random page from a website?
Posted 11 August 2012 - 09:21 PM
modi123_1, on 11 August 2012 - 09:15 PM, said:
I've never done databases before but this would be a great place to get started. Do I have to extract the links manually or is there an automatic way of doing it? There's thousands of links on that site. I know there's a random Wikipedia article generator somewhere...
#4
Re: Random page from a website?
Posted 11 August 2012 - 09:24 PM
#5
Re: Random page from a website?
Posted 11 August 2012 - 10:15 PM
Guitar player eh? I frequent the site.
#6
Re: Random page from a website?
Posted 11 August 2012 - 10:19 PM
Lemur, on 11 August 2012 - 10:15 PM, said:
Guitar player eh? I frequent the site.
I already did it in Python, I managed to get the list of text files easily. Now I need to convert it into Javascript.
import random
list = open("list.txt", "r").read().split("\n")
print("http://www.classtab.org/" + list[random.randint(0, len(list))])
This doesn't seem to work:
function load(){
var fs = require("fs");
var array = fs.readFileSync("list.txt").toString().split("\n");
window.alert(array[Math.floor(Math.random()*array.length())])
}
The problem is with the require("fs") thing. I found that piece of code on google.
This post has been edited by carnivroar: 11 August 2012 - 10:19 PM
#7
Re: Random page from a website?
Posted 11 August 2012 - 10:45 PM
Bingo!
But I'm not sure if it's a good idea to have a big ass array in the code.
This post has been edited by carnivroar: 12 August 2012 - 12:07 AM
#8
Re: Random page from a website?
Posted 12 August 2012 - 11:31 AM
This post has been edited by atraub: 12 August 2012 - 11:37 AM
#9
Re: Random page from a website?
Posted 12 August 2012 - 03:08 PM
atraub, on 12 August 2012 - 11:31 AM, said:
Can you embed python code on a website? I looked it up but it seems so complicated. I think there has to be a better way than storing all the text file names in an array.
#10
Re: Random page from a website?
Posted 12 August 2012 - 06:15 PM
carnivroar, on 11 August 2012 - 07:29 PM, said:
Is this possible? And if so, what tools do I need for it?
Thanks.
Yep it's possible. You need to import urllib2, random, and an html parser (I like BeautifulSoup).
#11
Re: Random page from a website?
Posted 12 August 2012 - 06:43 PM
import urllib2, random
from bs4 import BeautifulSoup
#get html and turn it into soup
html = urllib2.urlopen('http://www.classtab.org/')
soup = BeautfiulSoup(html, "lxml")
links = soup.find_all('a')
count = len(links)
#now create a random number between 1 and count
#do that lol I forgot how random works!
html2 = urllib2.urlopen('http://www.classtab.org/'+links[randomnumber].get('href'))
soup2 = BeautifulSoup(html2, "lxml")
print soup2.get_text()
###haven't tested this. Hope it works and helps! Good luck.
#12
Re: Random page from a website?
Posted 12 August 2012 - 07:13 PM
#13
Re: Random page from a website?
Posted 12 August 2012 - 11:54 PM
And I need to implement a better script because it's very slow on Firefox (FF cannot handle Javascript very well).
This post has been edited by carnivroar: 12 August 2012 - 11:54 PM
|
|

New Topic/Question
Reply


MultiQuote








|