I just wrote a script that is meant to go through the alphabet and find all unclaimed four-letter twitter names (really just for practice as I’m new to Python). I have written a couple previous scripts that use ‘urllib2’ to get website html from a url, but this time it doesn’t appear to be working. Here is my script:
import urllib2
src=''
url=''
print "finding four-letter @usernames on twitter..."
d_one=''
d_two=''
d_three=''
d_four=''
n_one=0
n_two=0
n_three=0
n_four=0
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
while (n_one > 26):
while(n_two > 26):
while (n_three > 26):
while (n_four > 26):
d_one=letters[n_one]
d_two=letters[n_two]
d_three=letters[n_three]
d_four=letters[n_four]
url = "twitter.com/" + d_one + d_two + d_three + d_four
src=urllib2.urlopen(url)
src=src.read()
if (src.find('Sorry, that page doesn’t exist!') >= 0):
print "nope"
n_four+=1
else:
print url
n_four+=1
n_three+=1
n_four=0
n_two+=1
n_three=0
n_four=0
n_one+=1
n_two=0
n_three=0
n_four=0
Running this code returned the following error:
SyntaxError: Non-ASCII character ‘\xe2’ in file name.py on line 29,
but no encoding declared; see http://www.python.org/peps/pep-0263.html
for details
and after visiting that link and doing some additional searching, I added the following line to the top of the document:
# coding: utf-8
Now, while it no longer returns an error, nothing appears to be happening. I added the line
print src
which should have printed the html of each url, but nothing happened when I ran it. Any advice would be greatly appreciated.
Well, you initialise
n_one=0, and then do a loopwhile (n_one > 26). When Python first encounters it, it seeswhile (0 > 26)which is obviously false and it therefore skips the entire loop.And as gnibbler’s answer tells you, there are cleaner ways of doing the loop anyway.