English subtitles

← Introduction - Intro to Computer Science

Get Embed Code
9 Languages

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

  1. [Dave] Welcome back for Unit 2!
  2. I hope everyone is getting confidence in the things you learned in unit 1.
  3. We're going to continue to build on those in this unit as well as in rest of the course.
  4. Our main goal for this unit is to make the web crawler, instead of just finding one link in the page,
  5. to find all the links on a page,
  6. so that we can follow these links and collect more and more pages on the web.
  7. To do that we need two big new concepts in computer science.
  8. The first is procedures. Procedures are a way to package code so that we can reuse it more easily.
  9. The second is control. We need a way to be able to make decisions, and to do repetition, to find all those links on a page.
  10. What we saw at the end of Unit 1 was a way to extract the first URL from a web page--
  11. that's great--we could find the first target.
  12. But if we want to build a good crawler,
  13. we don't just care about the first one,
  14. we care about all of the links on the page.
  15. We need to extract all of those links--
  16. figure out where all of those links point to--
  17. so we'll find many more pages to crawl than just the first one.
  18. So, that's the goal for this class as far as building a web browser.
  19. To do that, we're going to learn two really big ideas in computer science.
  20. The first is about procedures,
  21. and that's a way to package up code so we can use it
  22. in a much more useful way than we could before.
  23. The second is about control.
  24. Control structures will give us a way to keep going
  25. to find all the links on a page.
  26. So, let's remember the code we had at the end of Unit 1.
  27. We solved this problem of extracting the first URL from the page--
  28. we assumed the page was initialized to the contents of some web page.
  29. We initialized the variable "start_link"
  30. to the result of invoking "find" on "page,"
  31. passing in the start of the link tag.
  32. Then, we initialized the variable "start_quote"
  33. to the result of finding, in the page,
  34. the first quote following that link tag.
  35. Then, we initialized the variable "end_quote"
  36. to the result of invoking "find" on "page,"
  37. to find the first quote following the start quote.
  38. And, then, we assigned to the variable "url"--
  39. extracting from the page--
  40. from the character after the "start_quote,"
  41. to the character just before the "end_quote,"
  42. we could print out that URL.
  43. This worked to find the first URL on the page.
  44. If we wanted to find the second one,
  45. we could do it all again.
  46. We could say, now we want to advance
  47. so we're only looking at the rest of the page.
  48. We could do that by updating the variable "page,"
  49. assigning to it the result of the rest of the page, starting from "end_quote"--
  50. and, remember, when there's a blank after the colon
  51. that means select from this position, to the very end--
  52. and then we can do all the same stuff.
  53. We'll do "start_link" again; ...
  54. we'll do "start_quote" again; ...
  55. Now we've got code that's going to print out the first URL--
  56. keep going, updating the variable "page"--
  57. and then doing the exact same thing--
  58. printing out the second URL.
  59. If we wanted to print out the first three,
  60. we could do it again...
  61. So now, we've got code to print out the first three
  62. URL's on the page--
  63. let's scroll all the way up--so you've got--
  64. print out the first one--keep going--
  65. print out the second one--keep going--
  66. so this can go on forever.
  67. The reason we have computers
  68. is to save humans from doing lots of tedious work.
  69. We don't want to make humans do lots of tedious work--
  70. certainly, typing this out, over and over again,
  71. would be really tedious,
  72. and it wouldn't really even work that well.
  73. We have pages with hundreds of links,
  74. but there are other pages with only one or two links.
  75. So, it wouldn't make sense to copy this hundreds of times.
  76. There's always going to be some web page that has more links
  77. than we have copies of it--
  78. and any page that has fewer copies, we're going to run into problems
  79. because we're not going to be finding any of those links.
  80. So, our goal today is to solve all of those problems.