Skip to content

Commit 28b92c2

Browse files
committed
notes
1 parent 32cbf84 commit 28b92c2

18 files changed

+130
-0
lines changed

PythonStu/stu/HTML/getURL_urllib.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
# 利用标准库urllib获取想要的HTML文件
55
import urllib
66
import sys
7+
import httplib
8+
9+
#打开urllib的debug模式,清晰的查看http请求的过程
10+
httplib.HTTPConnection.debuglevel=1
711

812
#获取python.org的首页并打印
913
def demo_urlopen():
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#! /usr/bin/python
2+
#encoding=UTF-8
3+
4+
5+
import urllib2
6+
import cookielib
7+
8+
cookie = cookielib.CookieJar()
9+
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
10+
response = opener.open('http://www.baidu.com')
11+
12+
#debug
13+
print type(cookie)
14+
print cookie
15+
for item in cookie:
16+
print item.name,"=>",item.value
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#! /usr/bin/python
2+
#encoding=UTF-8
3+
4+
5+
import urllib2
6+
7+
"""Open Debug Mode in urllib2"""
8+
9+
def get_debugHandle():
10+
"""return debug mode handler for handler http"""
11+
http_debug_handler = urllib2.HTTPHandler(debuglevel=1)
12+
opener = urllib2.build_opener(http_debug_handler)
13+
return opener
14+
15+
if __name__ == "__main__":
16+
opener = get_debugHandle()
17+
if opener:
18+
opener.open('http://www.baidu.com')
19+
else:
20+
print 'init http Handler error'
21+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#! /usr/bin/python
2+
#encoding=UTF-8
3+
4+
import urllib2
5+
6+
url = "http://www.baidu.com"
7+
8+
request = urllib2.Request(url)
9+
request.add_header('User-Agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Safari/537.36')
10+
http_debug_handler = urllib2.HTTPHandler(debuglevel=1)
11+
opener =urllib2.build_opener(http_debug_handler)
12+
urllib2.install_opener(opener)
13+
fp = urllib2.urlopen(request)
14+
15+
16+
17+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /usr/bin/python
2+
#encoding=UTF-8
3+
4+
5+
import urllib2
6+
7+
8+
print "对于请求的请求可以通过response.getcode()就能获得"
9+
print "对于错误的请求,urllib2会抛出异常,这时可以检测异常对象的code属性"
10+
11+
12+
response = urllib2.urlopen('http://www.baidu.com')
13+
print response.getcode()
14+
15+
16+
try:
17+
response = urllib2.urlopen('http://127.0.0.1/admin.php')
18+
except urllib2.HTTPError,e:
19+
print e.code
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#! /usr/bin/python
2+
#encoding=UTF-8
3+
4+
import urllib2
5+
def isRedirect(url):
6+
"""request url judge is redirect?"""
7+
redirected = False
8+
response = urllib2.urlopen(url)
9+
redirected = response.geturl() == url
10+
print 'response url',response.geturl()
11+
print 'request url',url
12+
return not redirected
13+
14+
class RedirectHandler(urllib2.HTTPRedirectHandler):
15+
def http_error_301(self,req,fp,code,msg,headers):
16+
print req
17+
print fp
18+
print code
19+
print msg
20+
print headers
21+
22+
def http_error_302(self,req,fp,code,msg,headers):
23+
print req
24+
print fp
25+
print code
26+
print msg
27+
print headers
28+
29+
30+
def myRedirect():
31+
opener = urllib2.build_opener(RedirectHandler)
32+
33+
opener.open('http://www.g.cn')
34+
35+
if __name__=="__main__":
36+
print isRedirect('http://www.baidu.com')
37+
print isRedirect('http://www.g.cn')
38+
39+
myRedirect()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#! /usr/bin/python
2+
#encoding=UTF-8
3+
4+
import urllib2
5+
"""setting http proxy"""
6+
7+
enable_proxy = True
8+
proxy_handler = urllib2.ProxyHandler({'http':'http://www.proxy.org:8080'})
9+
if enable_proxy:
10+
opener = urllib2.build_opener(proxy_handler)
11+
else:
12+
opener = urllib2.build_opener(urllib2.ProxyHnadler({}))
13+
14+
urllib2.install_opener(opener)
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)