forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0013.py
More file actions
23 lines (20 loc) · 660 Bytes
/
Copy path0013.py
File metadata and controls
23 lines (20 loc) · 660 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from urllib.error import URLError, HTTPError
import urllib.request
import urllib.parse
url = 'http://tieba.baidu.com/p/2166231880'
values={'wd':'python',
'opt-webpage':'on',
'ie':'gbk'}
url_values=urllib.parse.urlencode(values)
#print(url_values)
url_values=url_values.encode(encoding='UTF8')
full_url=urllib.request.Request(url,url_values)
#or ony one sentense:full_url=url+'?'+url_values
try:
response=urllib.request.urlopen(full_url) #open=urlopen
except HTTPError as e:
print('Error code:',e.code)
except URLError as e:
print('Reason',e.reason)
the_page=response.read()
print(the_page)