我有一些我的朋友帮助创建的代码:
1 use LWP::Simple;
2 use HTML::TreeBuilder;
3 use Data::Dumper;
4
5 my $tree = url_to_tree( 'http://www.registrar.ucla.edu/schedule/schedulehome.aspx' );
6
7 my @selects = $tree->look_down( _tag => 'select' );
8 my @quarters = map { $_->attr( 'value' ) } $selects[0]->look_down( _tag => 'option' );
9 my @courses = map { my $s = $_->attr( 'value' ); $s =~ s/&/%26/g; $s =~ s/ /+/g; $s } $selects[1]->look_down( _tag => 'option' );
10
11 my $n = 0;
12
13 my %hash;
14
15 for my $quarter ( @quarters )
16 {
17 for my $course ( @courses )
18 {
19 my $tree_b = url_to_tree( "http://www.registrar.ucla.edu/schedule/crsredir.aspx?termsel=$quarter&subareasel=$course" );
20
21 my @options = map { my $s = $_->attr( 'value' ); $s =~ s/&/%26/g; $s =~ s/ /+/g; $s } $tree_b->look_down( _tag => 'option' );
22
23 for my $option ( @options )
24 {
25
26
27 print "trying: http://www.registrar.ucla.edu/schedule/detselect.aspx?termsel=$quarter&subareasel=$course&idxcrs=$option\n";
28
29 my $content = get( "http://www.registrar.ucla.edu/schedule/detselect.aspx?termsel=$quarter&subareasel=$course&idxcrs=$option" );
30
31 next if $content =~ m/No classes are scheduled for this subject area this quarter/;
32
33 $hash{"$course-$option"} = 1;
34 #my $tree_c = url_to_tree( "http://www.registrar.ucla.edu/schedule/detselect.aspx?termsel=$quarter&subareasel=$course&idxcrs=$option" );
35
36 #my $table = ($tree_c->look_down( _tag => 'table' ))[2]->as_HTML;
37
38 #print "$table\n\n\n\n\n\n\n\n\n\n";
39
40 $n++;
41 }
42 }
43 }
44
45 my $hash_count = keys %hash;
46 print "$n, $hash_count\n";
47
48 sub url_to_tree
49 {
50 my $url = shift;
51
52 my $content = get( $url );
53
54 my $tree = HTML::TreeBuilder->new_from_content( $content );
55
56 return $tree;
57 }
我无法理解线路33
和45
正在做什么。我认为在大多数情况下,我得到了其他所有东西在做的事情,即将@selects
网站上的主 .aspx 文件中的两个选择标签中包含的所有内容都放在了考虑范围内——我认为大小@selects
是 2。我也得到了从这一点开始,第 0 个插槽@selects
被传递到@quarters
中,同样,位置 1 插槽被传递到 @courses。每场独特的比赛都会被列举出来,n
全年提供的课程总数也是如此。现在,我没有得到 $hash_count 正在枚举的内容。我怀疑这是提供的独特课程的数量,所以n
类似于动物的地方(在伪代码中)
sizeof( ['math1 FALL 2014' , 'math1 SPRING 2014'] ) = 2
我怀疑hash_count
是一种动物
sizeof( ['math1 FALL 2014' , 'math1 SPRING 2014'] ) = 1
正确的?