#coding:utf8 #GET请求 import httplib conn = None try: conn = httplib.HTTPConnection("www.baidu.com") conn.request("GET", "/") response = conn.getresponse() print response.status, response.reason print '-' * 40 headers = response.getheaders() for h in headers: print h print '-' * 40 print response.msg except Exception,e: print e finally: if conn: conn.close() #POST请求 import httplib, urllib conn = None try: params = urllib.urlencode({'name': 'qiye', 'age':22}) headers = {"Content-type": "application/x-www-form-urlencoded" , "Accept": "text/plain"} conn = httplib.HTTPConnection("www.baidu.com", 80, timeout=3) conn.request("POST", "/login", params, headers) response = conn.getresponse() print response.getheaders() #获取头信息 print response.status print response.read() except Exception, e: print e finally: if conn: conn.close()
初识网络爬虫之二:httplib与urllib实现
最新推荐文章于 2023-12-12 21:37:51 发布