'You should use urllib to retrieve the HTML Pages',
'BeautifulSoup' => 'You should use BeautifulSoup to parse the HTML'
);
// Compute the stuff for the output
$sample_pages = 4;
$sample_pos = 2;
$actual_pages = 7;
$actual_pos = 17;
$code = 12345;
$sample_names = array();
$names = getShuffledNames($code);
$name = $names[$sample_pos];
$sample_names[] = $name;
for($p=0;$p<$sample_pages;$p++) {
$code = array_search($name, $NAMES);
$names = getShuffledNames($code);
$name = $names[$sample_pos];
$sample_last = $name;
$sample_names[] = $name;
}
if ( isset($_SESSION['debug']) && is_string($_SESSION['debug']) ) {
$code = array_search($_SESSION['debug'], $NAMES);
$name = $_SESSION['debug'];
} else {
$code = $USER->id+$LINK->id+$CONTEXT->id;
$names = getShuffledNames($code);
$name = $names[$actual_pos];
}
$actual_names = array();
$actual_names[] = $name;
for($p=0;$p<$actual_pages;$p++) {
$code = array_search($name, $NAMES);
$names = getShuffledNames($code);
$name = $names[$actual_pos];
$actual_last = $name;
$actual_names[] = $name;
}
$oldgrade = $RESULT->grade;
if ( isset($_POST['name']) && isset($_POST['code']) ) {
if ( $USER->instructor && strpos($_POST['name'],'42') === 0 ) {
$pieces = explode(',',$_POST['name']);
$_SESSION['success'] = "Debug Mode Unlocked";
if ( count($pieces) == 2 ) {
$_SESSION['debug'] = $pieces[1];
} else {
$_SESSION['debug'] = true;
}
header('Location: '.addSession('index.php'));
return;
}
$RESULT->setJsonKey('code', $_POST['code']);
if ( $_POST['name'] != $actual_last ) {
$_SESSION['error'] = "Your name did not match";
header('Location: '.addSession('index.php'));
return;
}
$val = validate($sanity, $_POST['code']);
if ( is_string($val) ) {
$_SESSION['error'] = $val;
header('Location: '.addSession('index.php'));
return;
}
LTIX::gradeSendDueDate(1.0, $oldgrade, $dueDate);
// Redirect to ourself
header('Location: '.addSession('index.php'));
return;
}
if ( $RESULT->grade > 0 ) {
echo('Your current grade on this assignment is: '.($RESULT->grade*100.0).'%
'."\n");
}
if ( $dueDate->message ) {
echo(''.$dueDate->message.'
'."\n");
}
$sample_url = dataUrl('known_by_'.$sample_names[0].'.html');
$actual_url = dataUrl('known_by_'.$actual_names[0].'.html');
?>
Following Links in Python
In this assignment you will write a Python program that expands on
http://www.py4e.com/code3/urllinks.py.
The program will use urllib to read the HTML from the data files below,
extract the href= vaues from the anchor tags, scan for a tag that is in
a particular position relative to the first name in the list,
follow that link and repeat the process
a number of times and report the last name you find.
We provide two files for this assignment. One is a sample file where we give
you the name for your testing and the other is the actual data you need
to process for the assignment
- Sample problem: Start at
Find the link at position (the first name is 1).
Follow that link. Repeat this process times. The
answer is the last name that you retrieve.
Sequence of names:
\n");
?>
Last name in sequence:
- Actual problem: Start at:
Find the link at position (the first name is 1).
Follow that link. Repeat this process times. The
answer is the last name that you retrieve.
Hint: The first character of the name of the last page
that you will load is:
\n");
echo("Debug sequence of names: \n");
foreach($actual_names as $name) {
echo(" $name\n");
}
echo("\n");
}
?>
Strategy
The web pages tweak the height between the links and hide the page after a few seconds
to make it difficult for you to do the assignment without writing a Python program.
But frankly with a little effort and patience you can overcome these attempts to make it
a little harder to complete the assignment without writing a Python program.
But that is not the point. The point is to write a clever Python program to solve the
program.
Sample execution
Here is a sample execution of a solution:
$ python3 solution.py
Enter URL:
Enter count: 4
Enter position: 3
Retrieving:
Retrieving:
Retrieving:
Retrieving:
Retrieving:
The answer to the assignment for this execution is "Anayah".
Turning in the Assignment