Fall 2003 Homework 6 Questions
Questions about programming networks and ticker tape movies?
Just looking ahead a little bit - I went and viewed the source at the website, and searched for the first three instances of that '<a href="/wire' thing. The first two were the same story (one was an image). Will we need to add into the program a way to avoid getting two the same, or should we not worry about it?
| I recommending looking for the "Headlines" first, then look for the three headlines as /wire/... links. That way, you don't get any pictures. I've updated the instructions to explain what I mean. Mark Guzdial |
i'm having trouble figuring out how to capture the second headline. is there a pattern in the html... or is there python code that forces jes to search below headline1?
| Just do another string.find. Remember that .find can take THREE parameters: A string to search for, a STARTING POINT, and an ENDING POINT. If you make the starting point +1 beyond where you found the last one, you'll find the next one. Mark Guzdial |
EXTENSION: HW6 is DUE MONDAY DECEMBER 1 at 5 pm
Those who don't want things due during Dead Week, my apologies. You're welcome to turn in HW6 BEFORE Thanksgiving. But for those who want the long weekend, you can have it. Mark Guzdial
THANK YOU!!!
Thank You for extending the deadline, this is a great help to me becuause I have a big Calculus test tomarrow, as well as a Chemistry test on Friday. Will you be dicussing the assignment in class at all? I hope so, I am a little confused on how to start the assignment. Also is there any slide to reference, besides the HTML slides?
| I STRONGLY recommend going to recitation for help on this! You'll need both the HTML slides and the movie slides (or use the book). You'll be gathering the information from the HTML, then creating a movie with it. Mark Guzdial |
i got my program to the point where finds the top 3 headlines and then i tell it to print each out. it ends up looking like this for today...
Probe Blames Failures at FirstEnergy for Massive Aug. Blackout
I Miss You Letters From Snipers Children Read to Jury
Internet Video Aims to Recruit Anti-U.S. Fighters
all that gibberish in the 2nd headline shows up as a quotation on the actual page, so if my ticker shows gibberish is this ok?
ok im feeling really dumb right now lol....that gibberish just turned into a ' on HERE too... so im guessing its just html stuff
so my question is is it ok if our ticker tape prints something out like & # 0 1 5 1 (without spaces) instead of an apostraphe?
| Yeah – that would be hard to work around. Leave it and we'll accept it like that. Mark Guzdial |
im having trouble figuring out how to write the program so that visits the website
Hey, here's my code and it loads, but everytime I try to run it it has a problem with the line I placed an arrow next to. Oh yeah, and how do you space in on this page, i still don't know, sorry.
import urllib
def hw6(directory):
website=urllib.urlopen("http://abcnews.go.com")
news=website.read()
website.close()
loc=FindArea(news)
H1=FindHeadline(news,loc)
H1string=H1[0]
HlLocation= H1[1]
printNow(H1[1])
-->H2=FindHeadline(news,H1Location)
H2string=H2[0]
H2Location=H2[1]
H3=FindHeadline(news,H2Location)
H3string=H3[0]
MakeTickerTape(H1string,H2string,H3string,directory)
def FindArea(news):
ourloc=news.find("http://a.abcnews.com/images/abcnews_hd.gif")
return ourloc
def FindHeadline(news,currentloc):
Hstart=news.find('<a href="/wire/',currentloc)
Hmiddle=news.find(">",Hstart)
Hend=news.find("</a>",Hmiddle)
Headline=news[Hmiddle +1: Hend]
return (Headline, Hend)
def MakeTickerTape(H1,H2,H3,directory):
x=1
for frames in range(1,100):
canvas=makePicture(getMediaPath("640x480.jpg"))
addText(canvas,x,100,H1)
addText(canvas,x,300,H3)
x=x+5
writeFrame(frame,directory,canvas)
def writeFrame(num,directory,framepict):
# Have to deal with single digit vs. double digit frame numbers differently
framenum=str(num)
if num < 10:
writePictureTo(framepict,directory+"//frame0"+framenum+".jpg")
if num >= 10:
writePictureTo(framepict,directory+"//frame"+framenum+".jpg")
I have a feeling it has something to do with H1Location=H1[1], but i'm not sure why. I think if this line has a problem, then the others just like it might. Please help. Thanks.
Oh, nevermind, I figured out the problem. I changed the spot where I wrote H1Location and just put in H1[1] instead. So it looks like this.
def hw6(directory):
website=urllib.urlopen("http://abcnews.go.com")
news=website.read()
website.close()
loc=FindArea(news)
H1=FindHeadline(news,loc)
H1string=H1[0]
HlLocation= H1[1]
printNow(H1[1])
-->H2=FindHeadline(news,(H1[1]))
H2string=H2[0]
H2Location=H2[1]
H3=FindHeadline(news,(H2[1]))
H3string=H3[0]
MakeTickerTape(H1string,H2string,H3string,directory)
It works. Is this ok?
| Please, PLEASE visit the FAQ to learn how to post code on this site. Mark Guzdial |
I am using the tickertapefunction given on the slides ( i modified it so it takes three strings as input and I have 3 strings) and I keep getting this error:
writePictureTo(pict,filename): Path is not valid
An error occurred attempting to pass an argument to a function.
Please check line 34 of C:\JES\JES programs\hw6.py
I also rechecked my hw5, which worked totally fine the other day, and I am getting the same error...it is on the line with the wrtiePictureTo (canvas, directory...)
WHAT IS GOING ON???
Try writing a picture from the Command Area: c = makeEmptyPicture(480,640) writePictureTo(c,"mysamplepic.jpg") If that works, you have the wrong directory. If it doesn't work, something's wrong with your JES. Mark Guzdial |
I keep getting an error on the 4th line of my code (a syntax error). Line 4 looks like this:
website=urllib.urlopen("http://abcnews.go.com")
which seems like it should work. Any thoughts on what I need to change or why it might not be working properly?
| Were you on the Internet? It only works when you're connected – doing that is the same as typing the URL into a browser. Mark Guzdial |
Nevermind the previous posting, I got it figured out (just a spacing issue)
hey can you tell me the spacing issue b/c i'm getting the same problem the the website=blah
I keep getting this error: "You are try to access a part of the object that doesn't exist." I don't understand how this is a spacing issue? Help por favor.
| It's not a spacing issue, but I need to know the line where you're getting the error to help. Mark Guzdial |
how would I make the font bigger and bolder and stuff on the tickertape???
Above you said you recommend going to recitation to get help with this, but we don't have recitation the week of Thanksgiving. Any suggestions?
| That was LAST week. I also explained how to do this at lecture Friday. Mark Guzdial |
You have to import the urllib module.
This is where I get the error: web=urllib.urlopen("http://abcnews.go.com")
And this is the error: "You are try to access a part of the object that doesn't exist." I did import the urllib module.
| And you were hooked up to the Internet at the time? You were able to use a browser? Try turning on Expert mode (Options under the Edit menu) to get more detail on what part of what object doesn't exist. Mark Guzdial |
The tops of the letters of my first headline at y=100 get cut off... i tried changing the value to 105, and it worked fine. When i changed it back again, the tops were once again cut off. What should I do?
| Either be happy with 105 or lower your font size. Your fonts are too large so they won't fit in 100 pixels. Mark Guzdial |
The directions say to have the first headline at y=100, that is why i used that value. Is this not required? I also did not change the font size at all.
i used the code you showed us in class on friday but nothing is printing out from my print statements or to the frames. when i change the values in the print statements and addText lines to page[start1 + 1:end1] it prints the html from the whole page. the beginning of my code looks like this:
import urllib
def hw6(directory):
web = urllib.urlopen("http://www.abcnews.go.com")
page = web.read( )
web.close( )
headline = page.find("/images/abcnew_hd.gif")
wire1 = page.find("/wire/", headline)
start1 = page.find(">", wire1)
end1 = page.find("", start1)
print "first headline:", page[start1:end1]
nevermind the last post...i figured it out. i didnt have the s on the end of the second abcnews...that was what was messing it up
I have checked over and over again and I can't figure out what is wrong with my code. I get the error message:
>>>A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
import urllib
def hw6(directory):
connection = urllib.urlopen("http://www.abcnews.go.com.com/")
news = connection.read()
connection.close()
headloc=news.find('<a href="/wire/>')
if headloc <> -1:
headloc1=news.find('html">',headloc)
endline1=news.find("",headloc1)
print "Headline #1: ",news[headloc1+6:endline1]
headline1=str(news[headloc1+6:endline1])
headloc2=news.find('html">', endline1)
endline2=news.find("", headloc2)
print "Headline #2: ", news[headloc2+6:endline2]
headline2=str(news[headloc2+6:endline2])
headloc3=news.find('html">', endline2)
endline3=news.find("", headloc3)
print "Headline #3: ", news[headloc3+6:endline3]
headline3=str(news[headloc3+6:endline3])
if headloc1 == -1:
print "They must have changed the page format -- can't find the headlines"
tickertape(directory, headline1, headline2, headline3)
def tickertape(directory,headline1, headline2, headline3):
for frame in range(0,100): #99 frames
canvas = makePicture(getMediaPath("640x480.jpg"))
addText(canvas,600-(frame*5),100,headline1)
addText(canvas,615-(frame*5),200,headline2)
addText(canvas,630-(frame*5),300,headline3)
framenum=str(frame)
if frame < 10:
writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
if frame >= 10:
writePictureTo(canvas,directory+"//frame"+framenum+".jpg")
Do you see anything that could be giving me that error?
| What line is the error? Is def tickertape against the left margin? Mark Guzdial |
So it didn't show anything that was red when I followed the instructions for posting code. A complete version:
import urllib
def hw6(directory):
connection = urllib.urlopen("http://www.abcnews.go.com.com/")
news = connection.read()
connection.close()
headloc=news.find('<a href="/wire/>')
if headloc <> -1:
headloc1=news.find('html">',headloc)
endline1=news.find("",headloc1)
print "Headline #1: ",news[headloc1+6:endline1]
headline1=str(news[headloc1+6:endline1])
headloc2=news.find('html">', endline1)
endline2=news.find("", headloc2)
print "Headline #2: ", news[headloc2+6:endline2]
headline2=str(news[headloc2+6:endline2])
headloc3=news.find('html">', endline2)
endline3=news.find("", headloc3)
print "Headline #3: ", news[headloc3+6:endline3]
headline3=str(news[headloc3+6:endline3])
if headloc1 == -1:
print "They must have changed the page format -- can't find the headlines"
tickertape(directory, headline1, headline2, headline3)
def tickertape(directory,headline1, headline2, headline3):
for frame in range(0,100): #99 frames
canvas = makePicture(getMediaPath("640x480.jpg"))
addText(canvas,600-(frame*5),100,headline1)
addText(canvas,615-(frame*5),200,headline2)
addText(canvas,630-(frame5),300,headline3)
framenum=str(frame)
if frame < 10:
writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
if frame >= 10:
writePictureTo(canvas,directory+"//frame"+framenum+".jpg")
| BTW, this may not work at all. Sometimes abcnews pictures at the top of the page link to /wire/ news stories. I suggest looking for the headline GIF first, as suggested in the homework writeup and as described in lecture. Mark Guzdial |
I am getting my 3 tickertapes to show up fine in the slides, but they don't have the right string. They keep starting from the very beginning of the source code from the webpage and not where I am telling it to start, which is after "page.find('abcnews_hd.gif')". What is going on???????
sorry, didn't mean to say "slides" meant to say "frames" in that last post
| Check your tabs. Are you sure you indented correctly? That's the only thing I can think of with the description you gave. Angela Liang |
| Try printing the three headlines. My guess is that you didn't find them right, so the three headlines are what you're seeing in your tickertape. Check your finds and try to make them more specific. Are you sure that you're passing the starting point into your .find methods? Mark Guzdial |
Okay, I seem to be having the same problem as one of the above postings. I get the error :
A local or global name could not be found.You need to define the function or variable before you try to use it in any way.Please check line 46.
Here is my code and the function tickertape:
import urllib
def hw6(directory):
website=urllib.urlopen("http://abcnews.go.com")
news=website.read()
website.close()
loc=FindArea(news)
H1=FindHeadline(news,loc)
H1string=H1[0]
HlLocation= H1[1]
printNow(H1[1])
H2=FindHeadline(news,(H1[1]))
H2string=H2[0]
H2Location=H2[1]
H3=FindHeadline(news,(H2[1]))
H3string=H3[0]
H3Location=H3[1]
tickertape(r"C:\Documents and Settings\Erica Papps\Desktop\JES-Win.5\hw6",H1string,H2sting,H3string)
def tickertape(directory,string1,string2,string3):
for frame in range(0,100): #100 frames
canvas = makePicture(getMediaPath("640x480.jpg"))
#Start at right, and move left
x=1
addText(canvas,x,100,string1)
addText(canvas,x,200,string2)
addText(canvas,x,300,string3)
x=x+5
# Now, write out the frame
# Have to deal with single digit vs. double digit frame numbers differently
framenum=str(frame)
if frame < 10:
writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
if frame >= 10:
writePictureTo(canvas,directory+"//frame"+framenum+".jpg")
What to do with name not found
- Check ALL the variables on the line. Most of the time when people have shown me code with this error, it's not the functions.
- Check your SPELLING. "director" is not the same as "directory."
- Check your input names. "frame" is not the same as "num".
- READ THE ERROR MESSAGE! If you turn on Expert mode (under Options in the Edit menu), it'll actually TELL you which name is not known.
Mark Guzdial
when i run my code everything works fine and the tickertape comes out, but I am confused on what exactly the "top three" headlines are. at the time i ran my code, abcnews.go.com looked like this:
• Medicare Bill Clears Hurdles | Goodies for Bush Pals? | Video
• Engine Failure Eyed in Deadly U.S. Chopper Crash | Iraq Ambush
• School for Scandal? Defense Official Has Questionable Degree
• What a Hassle: Car Sales Top List of Consumer Complaints
• Hall of Fame Baseball Pitcher Warren Spahn Dies at Age 82
• Bush Speaks to Slain Soldiers’ Families, Signs Defense Bill
• Bad Bet: Longshot Victory Foils Ingenious Horse Race Scam
• Dean Spars With Rivals Gephardt and Kerry in Democratic Debate
• Cell Phone ‘Emancipation Day’ | Caution | Tips | Video
• NBA Star Alonzo Mourning Retires, Needs Kidney Transplant
• Is Your Marriage on the Rocks? E-Mail 20/20 for Help
• More News Headlines | More Video | Photo Gallery
what i ended up getting as my headlines were:
Headline1: Medicare Bill Clears Hurdles
Headline2: Engine Failure Eyed in Deadly U.S. Chopper Crash
Headline3: Iraq Ambush
Is this correct or are these not the right headlines?????
thanks
| Those look fine to me. Those would be the parts that you'd actually click on, right? Mark Guzdial |
that's right Guzzy
.....thanks
my code ended up making a frame00, 01, 02....to frame100. Is this ok? because there are actually 101 frames (jpg's)
I am stuck and don't know why. The error says:There is something wrong with the text of the file you had me try to load.
You may have not have as many closing parenthesis as opening parenthesis, or you may have tried to use a jython keyword (if, def, etc...) as a function. This cannot be done. This is my code.
ARGH! NO FORMATTING! READ THE FAQ, PLEASE! It's the line with headline=page.find. You have two opening parentheses and only one closing one. Mark Guzdial |
def hw6(directory):
headlines=newsHeadlines()
for frame in range(0,100):
canvas=makePicture(getMediaPath("640X480"))
addText(canvas,600-(frame*10),100,headline[1])
addText(canvas,600-(frame*10),200,headline[2])
addText(canvas,600-(frame10),300,headline[3])
framenum=str(frame)
if frame<10:
writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
if frame>=10:
writePictureTo(canvas,directory+"//frame"+framenum+".jpg")
import urllib
def newsHeadlines():
web=urllib.urlopen("http://abcnews.go.com")
page=web.read
web.close
headline=page.find(("/images/abcnew_hd.gif")
wire1=page.find('a href="/wire/',headline)
start1=page.find('>',wire1)
end1=page.find('',start1)
print "First headline:",page[start1+1:end1]
headline1=str(page[start1+1:endline1]
wire2=page.find('a href="/wire/', end1)
start2=page.find('>',wire2)
end2=page.find('',start2)
print "Second headline:",page[start2+1:end2]
headline2=str(page[start2+1:end2])
wire3=page.find('a href="/wire/',end2)
start3=page.find('>',wire3)
end3=page.find('',start3)
print "Third headline:",page[start3+1,end3]
headline3=str(page[start3+1,end3])
This is my code. It captures all of the headlines correctly, but for some reason, the frames do not appear correctly because the different lines do not move at different speeds. I do not understand why because the code for the addText look the same. Any suggestions?
def hw6(directory):
# get the list of headlines
headlines = findNewsLive()
for frame in range(0,100): #99 frames
canvas = makePicture(getMediaPath("640x480.jpg"))
#Start at right, and move left
addText(canvas,600-(frame*5),100,headlines[0])
addText(canvas,600-(frame*5),200,headlines[1])
addText(canvas,600-(frame*5),300,headlines[2])
# Now, write out the frame
# Have to deal with single digit vs. double digit frame numbers differently
framenum=str(frame)
if frame < 10:
writePictureTo(canvas,directory+"//frame0"+framenum+".jpg")
if frame >= 10:
writePictureTo(canvas,directory+"//frame"+framenum+".jpg")
def findNewsLive():
import urllib #Could go above, too
connection=urllib.urlopen("http://abcnews.go.com")
news = connection.read()
connection.close()
# zeroloc is where I start looking for headlines
zeroloc = news.find('begin headlines')
firstloc = news.find('<a href="/wire',zeroloc)
if firstloc <> -1:
secondloc = news.find("</a>",firstloc)
stringstart = news.rfind(">",0,secondloc)
print "Current news:",news[stringstart+1:secondloc]
Headlines1 = news[stringstart+1:secondloc]
if firstloc == -1:
print "They must have changed the page format. I can't find a headline."
# Set the location of where I start looking for the next headline as
# the location of where I stopped in the previous headline
zeroloc = secondloc
firstloc = news.find('<a href="/wire',zeroloc)
if firstloc <> -1:
secondloc = news.find("</a>",firstloc)
stringstart = news.rfind(">",0,secondloc)
print "Current news:",news[stringstart+1:secondloc]
Headlines2 = news[stringstart+1:secondloc]
if firstloc == -1:
print "They must have changed the page format. I can't find a headline."
zeroloc = secondloc
firstloc = news.find('<a href="/wire',zeroloc)
if firstloc <> -1:
secondloc = news.find("</a>",firstloc)
stringstart = news.rfind(">",0,secondloc)
print "Current news:",news[stringstart+1:secondloc]
Headlines3 = news[stringstart+1:secondloc]
if firstloc == -1:
print "They must have changed the page format. I can't find a headline."
mylist = [Headlines1, Headlines2, Headlines3]
print mylist
return mylist
| Why do you want the three lines to move at different speeds? Mark Guzdial |
I keep getting this error on this line: web = urllib.urlopen("http://abcnews.go.com")
I am able to access a browser and connected to the internet. This is the error:
You are trying to access a part of the object that doesn't exist.
in file C:\Documents and Settings\Nida\My Documents\CS hw\hw6.py, on line 4, in function hw6
in file C:\JES-SA1a\jython\Lib\urllib.py, on line 71, in function urlopen
in file C:\JES-SA1a\jython\Lib\urllib.py, on line 176, in function open
in file C:\JES-SA1a\jython\Lib\urllib.py, on line 283, in function open_http
in file C:\JES-SA1a\jython\Lib\httplib.py, on line 440, in function putrequest
AttributeError: __getitem__
Please check line 4 of C:\Documents and Settings\Nida\My Documents\CS hw\hw6.py
Never mind...I re-installed JES and its giving better errors now.
The guy asking about the frames moving at different speeds, you've got only 99 frames, you need 100.
This might sound weird, but how do we save the headlines so that they will tickertape across the screen? Right now, I have the code working but all of the frames are blank.
Store your headlines as variables, like you see in some of the xamples above, and then fefine the tickertape function with three strings as inputs, then call the function with those variables as your inputs. There's some examples of that on this page.
to get my code to work, I type: hw6(r"Directory")
but it says there's an error in that. I could have sworn that's what I've used before to get something to work. any clues?
| An error in that line? What's the error? Did you Load? Mark Guzdial |
umm, I'm not sure what happened, but I think abc changed their site and no longer has the wires right before to tell the headlines. what should we do now? try to find another way to post the headlines in the new page setup, or will working with the old be fine?
| I just did View Source on the site, and I see lots of /wire/ headlines. Mark Guzdial |
import urllib
def hw6(directory):
website=urllib.urlopen("http://abcnews.go.com")
news=website.read()
website.close()
loc=FindArea(news)
H1=FindHeadline(news,loc)
H1string=H1[0]
printNow(H1[1])
H2=FindHeadline(news,(H1[1]))
H2string=H2[0]
H3=FindHeadline(news,(H2[1]))
H3string=H3[0]
MakeTickerTape(H1string,H2string,H3string,directory)
def FindArea(news):
ourloc=news.find("http://a.abcnews.com/images/abcnews_hd.gif")
return ourloc
def FindHeadline(news,currentloc):
Hstart=news.find('<a href="/wire/',currentloc)
Hmiddle=news.find(">",Hstart)
Hend=news.find("</a>",Hmiddle)
Headline=news[Hmiddle+1: Hend]
return Headline, Hend
def MakeTickerTape(H1,H2,H3,directory):
x=1
for frame in range(1,100):
canvas=makePicture(getMediaPath("640x480.jpg"))
addText(canvas,x,100,H1)
addText(canvas,x,200,H2)
addText(canvas,x,300,H3)
x=x+5
writeFrame(frame,directory,canvas)
def writeFrame(num,directory,framepict):
# Have to deal with single digit vs. double digit frame numbers differently
framenum=str(num)
if num < 10:
writePictureTo(framepict,directory+"//frame0"+framenum+".jpg")
if num >= 10:
writePictureTo(framepict,directory+"//frame"+framenum+".jpg")
ok. here's my code just above. It works, so is this ok? thanks.
Sara Jones
| Is there something in particular were you concerned with that might make it NOT okay? Mark Guzdial |
Links to this Page