标签:pass 服务 记录 nbsp ons submit web urllib2 proxy
新建test14来实现一个简单的代理Demo:
import urllib2 enable_proxy = True proxy_handler = urllib2.ProxyHandler({"http" : ‘http://some-proxy.com:8080‘}) null_proxy_handler = urllib2.ProxyHandler({}) if enable_proxy: opener = urllib2.build_opener(proxy_handler) else: opener = urllib2.build_opener(null_proxy_handler) urllib2.install_opener(opener)
这样后面的使用会非常方便,但不能做更仔细的控制,比方想在程序中使用两个不同的 Proxy 设置等。
比較好的做法是不使用 install_opener 去更改全局的设置。而仅仅是直接调用 opener 的 open 方法取代全局的 urlopen 方法。
2.Timeout 设置
在老版 Python 中(Python2.6前)。urllib2 的 API 并没有暴露 Timeout 的设置。要设置 Timeout 值,仅仅能更改 Socket 的全局 Timeout 值。
import urllib2 import socket socket.setdefaulttimeout(10) # 10 秒钟后超时 urllib2.socket.setdefaulttimeout(10) # 还有一种方式
import urllib2 response = urllib2.urlopen(‘http://www.google.com‘, timeout=10)
import urllib2 request = urllib2.Request(‘http://www.baidu.com/‘) request.add_header(‘User-Agent‘, ‘fake-client‘) response = urllib2.urlopen(request) print response.read()
常见的取值有:
application/xml : 在 XML RPC,如 RESTful/SOAP 调用时使用
application/json : 在 JSON RPC 调用时使用
application/x-www-form-urlencoded : 浏览器提交 Web 表单时使用
在使用server提供的 RESTful 或 SOAP 服务时, Content-Type 设置错误会导致server拒绝服务
4.Redirect
urllib2 默认情况下会针对 HTTP 3XX 返回码自己主动进行 redirect 动作。无需人工配置。
要检測是否发生了 redirect 动作,仅仅要检查一下 Response 的 URL 和 Request 的 URL 是否一致就能够了。
import urllib2 my_url = ‘http://www.google.cn‘ response = urllib2.urlopen(my_url) redirected = response.geturl() == my_url print redirected my_url = ‘http://rrurl.cn/b1UZuP‘ response = urllib2.urlopen(my_url) redirected = response.geturl() == my_url print redirected
import urllib2 class RedirectHandler(urllib2.HTTPRedirectHandler): def http_error_301(self, req, fp, code, msg, headers): print "301" pass def http_error_302(self, req, fp, code, msg, headers): print "303" pass opener = urllib2.build_opener(RedirectHandler) opener.open(‘http://rrurl.cn/b1UZuP‘)
import urllib2 import cookielib cookie = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) response = opener.open(‘http://www.baidu.com‘) for item in cookie: print ‘Name = ‘+item.name print ‘Value = ‘+item.value
import urllib2 request = urllib2.Request(uri, data=data) request.get_method = lambda: ‘PUT‘ # or ‘DELETE‘ response = urllib2.urlopen(request)
import urllib2 try: response = urllib2.urlopen(‘http://bbs.csdn.net/why‘) except urllib2.HTTPError, e: print e.code
import urllib2 httpHandler = urllib2.HTTPHandler(debuglevel=1) httpsHandler = urllib2.HTTPSHandler(debuglevel=1) opener = urllib2.build_opener(httpHandler, httpsHandler) urllib2.install_opener(opener) response = urllib2.urlopen(‘http://www.google.com‘)
# -*- coding: utf-8 -*- import urllib import urllib2 postdata=urllib.urlencode({ ‘username‘:‘汪小光‘, ‘password‘:‘why888‘, ‘continueURI‘:‘http://www.verycd.com/‘, ‘fk‘:‘‘, ‘login_submit‘:‘登录‘ }) req = urllib2.Request( url = ‘http://secure.verycd.com/signin‘, data = postdata ) result = urllib2.urlopen(req) print result.read()
#… headers = { ‘User-Agent‘:‘Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6‘ } req = urllib2.Request( url = ‘http://secure.verycd.com/signin/*/http://www.verycd.com/‘, data = postdata, headers = headers ) #...
比如。有些站点喜欢读取header中的X-Forwarded-For来看看人家的真实IP。能够直接把X-Forwarde-For改了。
參考来源:
零基础写python爬虫之urllib2使用指南
http://www.lai18.com/content/384669.html
2零基础写python爬虫之使用urllib2组件抓取网页内容
4零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers
标签:pass 服务 记录 nbsp ons submit web urllib2 proxy
原文地址:http://www.cnblogs.com/ljbguanli/p/6937953.html