-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_http_request.py
More file actions
44 lines (37 loc) · 1.18 KB
/
python_http_request.py
File metadata and controls
44 lines (37 loc) · 1.18 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
__author__ = 'k22li'
import requests
from xml.etree import ElementTree
def requestUrlResources():
"""
request resources from the targe urls
"""
urlToVisit = raw_input('Pls. give your web address to visit: ')
req = requests.request('GET', urlToVisit)
return req.text
def encodingRequestedConents(contents = '', coding = 'gb2312'):
"""
decode the page contents in case there are illegal encodings
"""
if not isinstance(contents, unicode):
print 'non-unicode confirmed'
decodedContents = contents.decode(coding)
else:
print 'unicode confirmed'
decodedContents = contents.encode('utf-8')
return decodedContents
def getChildrenContents(strings = ''):
"""
return the key nodes from the downloaded url resources
"""
strings = strings.rstrip()
tree = ElementTree.fromstring(strings)
root = tree.getroot()
print root.tag
# for node in tree.getchildren():
# print node.tag, node.attrib
if __name__ == '__main__':
urlContent = requestUrlResources()
decodedContent = encodingRequestedConents(urlContent, 'ascii')
print decodedContent
#
k = getChildrenContents(decodedContent)