So let's recap the code we have at the end
of unit two. So we defined a procedure, get_next_target, that
would take a page, search for the first linked target
on that page, return that as the value of URL.
That would be the first output, and also return the
position where the end of the quote is so we
know how to continue. And then we define the procedure
print all_links that keeps going as long as we can.
As long as there are more URLs on the page. It
will find the next target. Store these in the variables URL and
endpos to keep track of the location end of string. If
there is a URL, what we did was just print it out
and then we update with the page to keep going. What
we want to do to change this is instead of just printing out
each URL as we find it, we want to collect the URLs. We
want to have a way to use the URLs so we can use
them to keep crawling to find new
pages. The structure we've been learning about this
unit is the way to do that. What we want to do is keep all the
URLs in a list. At the end of this procedure, instead of printing the links
as we go, we want to have a list of all the links that we found.
So this is what the current print_all_links procedure does.
It takes the page as its input and its output
is nothing. It doesn't return anything. All it does
is do some work, prints out all these links. But
we can't actually use them at the end, because
it doesn't return anything. So what we want to do is
change this. Instead of print_all_links, what we want is to
get_all_links. We want to actually have the links in a way
that we can use them. So what we want,
instead of printing all links is to actually get the
links. So we'll change the name of our procedure
to get_all_links. And instead of outputting none, what we want to
do is output a list of links. And that
should be the list that corresponds to the things that
we were printing before, but now instead of just
printing them, we want to output them as a list.
Así que vamos a recapitular el código que tenemos al final
de la unidad dos. Así que hemos definido un procedimiento, get_next_target, que
tomaría una página, buscar el primer objetivo vinculado
en esa página, volver que a medida que el valor de URL.
Esa sería la primera salida, y también devolver el
posición en la que el final de la cita es así que
saber cómo continuar. Y a continuación, se define el procedimiento
all_links impresión que sigue adelante, siempre y cuando podamos.
Mientras hay más direcciones URL de la página. Ello
encontrarán el próximo objetivo. Guarde estos en la URL de variables y
endpos no pierdan de vista el final localización de cadena. Si
hay una URL, lo que hicimos fue simplemente imprimirlo
y luego nos ponemos al día con la página para seguir adelante. Qué
queremos hacer para cambiar esto es en lugar de simplemente imprimir
cada URL como lo encontramos, queremos recoger las direcciones URL. Nosotros
quiero tener una manera de utilizar las direcciones URL para que podamos usar
ellos mantener el rastreo para encontrar nueva
páginas. La estructura que hemos estado aprendiendo acerca de esto
unidad es la manera de hacerlo. Lo que queremos hacer es mantener todo el
URL en una lista. Al final de este procedimiento, en lugar de imprimir los enlaces
a medida que avanzamos, queremos tener una lista de todos los vínculos que nos encontramos.
Así que esto es lo que hace el procedimiento print_all_links actual.
Toma la página como su entrada y su salida
no es nada. No devuelve nada. Todo lo que hace
es hacer un trabajo, imprime todos estos enlaces. Pero
no podemos utilizar realmente ellos al final, porque
no devuelve nada. Así que lo que queremos hacer es
cambia esto. En lugar de imprimir todos los enlaces, lo que queremos es
obtener todos los vínculos. Queremos tener realmente los eslabones de una manera
que podemos utilizarlos. Así que lo que queremos,
en lugar de imprimir todos los enlaces es conseguir realmente el
campo de golf. Así que vamos a cambiar el nombre de nuestro procedimiento
para obtener todos los vínculos. Y en vez de dar salida a ninguno, lo que queremos
hacer es emitir una lista de enlaces. Y eso
debe ser la lista que corresponde a las cosas que
estábamos imprimiendo antes, pero ahora en lugar de sólo
imprimirlos, queremos salida de ellos, como una lista.
レッスン2までのコードをまとめてみましょう
まずget_next_target関数を定義しました
これはpageを使用し
そのページの1つ目のリンクターゲットを検索します
urlの値としてそれを返します
それが1つ目の出力になります また引用の終わりで
継続するか分かる場合その位置も返します
その後可能な限り長く継続できる
print all_links関数を定義します
ページ上にurlがある場合に限り
次のターゲットを見つけます
文字列の終了位置を把握するために
urlとendposにそれを保持します
urlがある場合はただ表示させるだけです
続けるためにpageでアップデートします
これを変更するために行う必要があることは
見つけたurlを1つずつ
ただ表示するのではなくurlを集めることです
新しいページを見つけるクロールを
継続するためにURLを使用します
このレッスンで学んでいる構造体がその方法です
リストにすべてのURLを保持することが必要です
この関数の終わりにリンクを表示するのではなく
見つけたすべてのリンクのリストが必要です
これが現在のprint_all_links関数で行うことです
入力としてページを用い何も出力しません
何も返さずいくつかの操作を行うだけです
このすべてのリンクを表示します
しかし実際にはそれを使用できません
なぜなら何も返していないからです
そこで必要なのがこれを変えることです
print_all_linksの代わりに
必要なのがget_all_linksです
これを使用できるリンクを
実際に持たなければなりません
必要なのは表示することではなく
実際にリンクを取得することです
関数の名前をget_all_linksに変更します
何も出力しない代わりにリンクのリストを出力します
それは以前表示したものと
一致するリストになるはずです
それをただ表示するのではなく
リストとして出力する必要があります