forked from csev/py4e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment_html.php
More file actions
170 lines (152 loc) · 5.54 KB
/
comment_html.php
File metadata and controls
170 lines (152 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
use \Tsugi\Core\LTIX;
use \Tsugi\Util\LTI;
$sanity = array(
'urllib' => 'You should use urllib to retrieve the data from the URL',
'BeautifulSoup' => 'You should use the BeautifulSoup library to parse the HTML'
);
// A random code
if ( isset($_SESSION['code_override']) ) {
$code = $_SESSION['code_override'];
$override = true;
} else {
$code = $USER->id+$LINK->id+$CONTEXT->id;
$override = false;
}
// Set the data URLs
$sample_url = dataUrl('comments_42.html');
$actual_url = dataUrl('comments_'.$code.'.html');
// Compute the sum data
$json = getJsonOrDie(dataUrl('comments_42.json'));
$sum_sample = sumCommentJson($json);
$json = getJsonOrDie(dataUrl('comments_'.$code.'.json'));
$sum = sumCommentJson($json);
$oldgrade = $RESULT->grade;
if ( isset($_POST['sum']) && isset($_POST['code']) ) {
if ( $USER->instructor && strpos($_POST['sum'],'code:') === 0 ) {
$pieces = explode(':',$_POST['sum']);
if ( count($pieces) == 2 && is_numeric($pieces[1]) ) {
if ( $pieces[1] == 0 ) {
unset($_SESSION['code_override']);
} else {
$_SESSION['code_override'] = $pieces[1]+0;
}
header('Location: '.addSession('index.php'));
}
}
$RESULT->setJsonKey('code', $_POST['code']);
if ( $_POST['sum'] != $sum ) {
$_SESSION['error'] = "Your sum 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;
}
// echo($goodsha);
if ( $RESULT->grade > 0 ) {
echo('<p class="alert alert-info">Your current grade on this assignment is: '.($RESULT->grade*100.0).'%</p>'."\n");
}
if ( $dueDate->message ) {
echo('<p style="color:red;">'.$dueDate->message.'</p>'."\n");
}
$sample_url = dataUrl('comments_42.html');
$actual_url = dataUrl('comments_'.$code.'.html');
?>
<p>
<!--
If you are having problems with this assignment, give this code to the
instructor: <?= $code ?>
-->
<b>Scraping Numbers from HTML using BeautifulSoup</b>
In this assignment you will write a Python program similar to
<a href="http://www.pythonlearn.com/code/urllink2.py" target="_blank">http://www.pythonlearn.com/code/urllink2.py</a>.
The program will use <b>urllib</b> to read the HTML from the data files below, and parse the data,
extracting numbers and compute the sum of the numbers in the file.
</p>
<p>
We provide two files for this assignment. One is a sample file where we give you the sum for your
testing and the other is the actual data you need to process for the assignment.
<?php
if ( $override ) {
echo('<p style="color:red">You are running emulating a student with a code of '.$code);
echo(' and an expected sum of '.$sum.".</p>\n");
}
?>
<ul>
<li> Sample data: <a href="<?= deHttps($sample_url) ?>" target="_blank"><?= deHttps($sample_url) ?></a>
(Sum=<?= $sum_sample ?>) </li>
<li> Actual data: <a href="<?= deHttps($actual_url) ?>" target="_blank"><?= deHttps($actual_url) ?></a>
(Sum ends with <?= $sum%100 ?>)<br/> </li>
</ul>
You do not need to save these files to your folder since your
program will read the data directly from the URL.
<b>Note:</b> Each student will have a distinct data url for the assignment - so only use your
own data url for analysis.
</p>
<b>Data Format</b>
<p>
The file is a table of names and comment counts. You can ignore most of the data in the
file except for lines like the following:
<pre>
<tr><td>Modu</td><td><span class="comments">90</span></td></tr>
<tr><td>Kenzie</td><td><span class="comments">88</span></td></tr>
<tr><td>Hubert</td><td><span class="comments">87</span></td></tr>
</pre>
You are to find all the <span> tags in the file and pull out the numbers from the
tag and sum the numbers.
<p>
Look at the
<a href="http://www.pythonlearn.com/code/urllink2.py" target="_blank">sample code</a>
provided. It shows how to find all of a certain kind of tag, loop through the tags and
extract the various aspects of the tags.
<pre>
...
# Retrieve all of the anchor tags
tags = soup('a')
for tag in tags:
# Look at the parts of a tag
print 'TAG:',tag
print 'URL:',tag.get('href', None)
print 'Contents:',tag.contents[0]
print 'Attrs:',tag.attrs
</pre>
You need to adjust this code to look for <b>span</b> tags and pull out
the text content of the span tag, convert them to integers and
add them up to complete the assignment.
</p>
<p><b>Sample Execution</b>
<p>
<pre>
$ python solution.py
Enter - http://python-data.dr-chuck.net/comments_42.html
Count 50
Sum 2...
</pre>
</p>
<p><b>Turning in the Assignment</b>
<form method="post">
Enter the sum from the actual data and your Python code below:<br/>
Sum: <input type="text" size="20" name="sum">
(ends with <?= $sum%100 ?>)
<?php if ( $USER->instructor ) { ?>
<p style="color:green">If you want to emulate a student, ask them to view source on
their page and find their "code" value in the comments. Then enter 'code:' and their code
in the sum area (above) and you can switch to their code and see what they are seeing.
</p>
<p>
Enter 'code:0' to go back to your own view of the assignment.
</p>
<?php } ?>
<input type="submit" value="Submit Assignment"><br/>
Python code:<br/>
<textarea rows="20" style="width: 90%" name="code"></textarea><br/>
</form>