Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Urllib - Web Development

Get Embed Code
4 Languages

Showing Revision 6 created 05/25/2016 by Udacity Robot.

  1. Okay, so what I'd like to do now
  2. is I'm going to go into Python, and we're going to play with
  3. a Python library for actually making an HTTP request
  4. so you can see how that works,
  5. and then prepare for some quizzes.
  6. So in Python we have a library called urllib2.
  7. There's also a urllib1,
  8. and this is kind of the evolution of Python in front of your here.
  9. We're going to use 2, for the most part.
  10. urllib1 has a few handy functions of its own,
  11. and when we use those, I'll include those in documents.
  12. But anyway, urllib2 has a function in it called url open,
  13. and we can give a url here to download.
  14. So let's say I'm downloading
  15. Actually, I need to make sure I save this.
  16. I usually save it in a variable called P for page.
  17. Probably not even the right concept, but that's my habit.
  18. I always use P when I use url open.
  19. So if you run this, we're going to get this P object,
  20. which is actually, basically, a file object.
  21. In Python, file objects--
  22. basically, what a file object is, is an object that has a read method.
  23. And you can call a read on there to get the contents.
  24. So I'm going to show the contents in C and call read on P.
  25. Okay, we called url open on this url,
  26. storing it in this variable P,
  27. and then we called read on the response
  28. and stored it in a variable called C.
  29. Now, if we were to evaluate C, a wall of text,
  30. which is what we expect.
  31. So this is actually Google's front page.
  32. If you remember early in this class,
  33. we basically accomplished the same thing
  34. using telnet or curl.
  35. You can also do the same thing in Python.
  36. So now we have this variable C that has this whole response in it,
  37. and we can manipulate it in our programs,
  38. which is what we're going to be doing a lot of.
  39. Let's take a peek at what we have on that P object.
  40. We can use the dir built-in function in Python
  41. to examine an object.
  42. So now we can see the methods and attributes on our P object,
  43. and we can see a couple of them that are probably interesting to us--
  44. headers, for one, and get url is another.
  45. Get code is probably the status code.
  46. This is generally how I work.
  47. When you don't know a library super well,
  48. you can use dir to kind of examine the object.
  49. So let's take a peek at a couple of these.
  50. We've also got a url one. Let's see what's in there.
  51. That's the url we requested. No big surprise.
  52. We can look at the headers.
  53. So this is an HTTP message instance.
  54. Now, I happen to know that this is a dictionary,
  55. and dictionaries have a function on them
  56. called items.
  57. If we were to run items on this in Python,
  58. this is what you can call in any dictionary--items--
  59. to view the keys and the values,
  60. and it will actually print them, generally, nicely for you.
  61. We can see all of the headers we got back from Google.
  62. This is an actual dictionary, so we can say p.headers,
  63. for example, content type,
  64. and we can see the content type that we got back from Google.
  65. It's actually kind of interesting; we're getting an ISO charset,
  66. which is--I was expecting UTF 8,
  67. but, hey, you learn things every day.
  68. So in the future, especially for your Windows users
  69. who had trouble using telnet,
  70. you can just use urllib and get the same answer.
  71. What I'd like you to do now is play with this library a little bit
  72. in the form of a quiz.