欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Python urllib、urllib2、httplib抓取網(wǎng)頁(yè)代碼實(shí)例

系統(tǒng) 1846 0

使用urllib2,太強(qiáng)大了
試了下用代理登陸拉取cookie,跳轉(zhuǎn)抓圖片......
文檔:http://docs.python.org/library/urllib2.html

直接上demo代碼了
包括:直接拉取,使用Reuqest(post/get),使用代理,cookie,跳轉(zhuǎn)處理

            
#!/usr/bin/python
# -*- coding:utf-8 -*-
# urllib2_test.py
# author: wklken
# 2012-03-17 wklken@yeah.net


import urllib,urllib2,cookielib,socket

url = "http://www.testurl....." #change yourself
#最簡(jiǎn)單方式
def use_urllib2():
 try:
  f = urllib2.urlopen(url, timeout=5).read()
 except urllib2.URLError, e:
  print e.reason
 print len(f)

#使用Request
def get_request():
 #可以設(shè)置超時(shí)
 socket.setdefaulttimeout(5)
 #可以加入?yún)?shù) [無(wú)參數(shù),使用get,以下這種方式,使用post]
 params = {"wd":"a","b":"2"}
 #可以加入請(qǐng)求頭信息,以便識(shí)別
 i_headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1) Gecko/20090624 Firefox/3.5",
       "Accept": "text/plain"}
 #use post,have some params post to server,if not support ,will throw exception
 #req = urllib2.Request(url, data=urllib.urlencode(params), headers=i_headers)
 req = urllib2.Request(url, headers=i_headers)

 #創(chuàng)建request后,還可以進(jìn)行其他添加,若是key重復(fù),后者生效
 #request.add_header('Accept','application/json')
 #可以指定提交方式
 #request.get_method = lambda: 'PUT'
 try:
  page = urllib2.urlopen(req)
  print len(page.read())
  #like get
  #url_params = urllib.urlencode({"a":"1", "b":"2"})
  #final_url = url + "?" + url_params
  #print final_url
  #data = urllib2.urlopen(final_url).read()
  #print "Method:get ", len(data)
 except urllib2.HTTPError, e:
  print "Error Code:", e.code
 except urllib2.URLError, e:
  print "Error Reason:", e.reason

def use_proxy():
 enable_proxy = False
 proxy_handler = urllib2.ProxyHandler({"http":"http://proxyurlXXXX.com:8080"})
 null_proxy_handler = urllib2.ProxyHandler({})
 if enable_proxy:
  opener = urllib2.build_opener(proxy_handler, urllib2.HTTPHandler)
 else:
  opener = urllib2.build_opener(null_proxy_handler, urllib2.HTTPHandler)
 #此句設(shè)置urllib2的全局opener
 urllib2.install_opener(opener)
 content = urllib2.urlopen(url).read()
 print "proxy len:",len(content)

class NoExceptionCookieProcesser(urllib2.HTTPCookieProcessor):
 def http_error_403(self, req, fp, code, msg, hdrs):
  return fp
 def http_error_400(self, req, fp, code, msg, hdrs):
  return fp
 def http_error_500(self, req, fp, code, msg, hdrs):
  return fp

def hand_cookie():
 cookie = cookielib.CookieJar()
 #cookie_handler = urllib2.HTTPCookieProcessor(cookie)
 #after add error exception handler
 cookie_handler = NoExceptionCookieProcesser(cookie)
 opener = urllib2.build_opener(cookie_handler, urllib2.HTTPHandler)
 url_login = "https://www.yourwebsite/?login"
 params = {"username":"user","password":"111111"}
 opener.open(url_login, urllib.urlencode(params))
 for item in cookie:
  print item.name,item.value
 #urllib2.install_opener(opener)
 #content = urllib2.urlopen(url).read()
 #print len(content)
#得到重定向 N 次以后最后頁(yè)面URL
def get_request_direct():
 import httplib
 httplib.HTTPConnection.debuglevel = 1
 request = urllib2.Request("http://www.google.com")
 request.add_header("Accept", "text/html,*/*")
 request.add_header("Connection", "Keep-Alive")
 opener = urllib2.build_opener()
 f = opener.open(request)
 print f.url
 print f.headers.dict
 print len(f.read())

if __name__ == "__main__":
 use_urllib2()
 get_request()
 get_request_direct()
 use_proxy()
 hand_cookie()

          


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 九九视频九九热 | 综合二区 | 欧美精品一二三区 | 黄色网页在线 | 射综合网 | 国产午夜小视频 | 国产精品久久久免费视频 | 99在线精品视频在线观看 | 精品一区二区三区免费视频 | 久久久中文| 色屁屁www影院免费观看软件 | 日本不卡一区二区 | 国内精品一区二区 | 一级黄色毛片子 | 老司机免费福利视频无毒午夜 | 亚洲欧美在线观看一区二区 | 狠狠操在线观看 | 成人午夜视频一区二区国语 | 欧美特黄a级高清免费看片 欧美精品一二区 | 日韩精品久久一区二区三区 | 亚洲欧美久久婷婷爱综合一区天堂 | 亚洲视频在线观看网站 | 特黄一区二区三区 | 久久久蜜桃 | 无码日韩精品一区二区免费 | 国产高清网址 | www亚洲精品 | 欧美精品一区二区在线观看 | www.天天操.com | 操操影视| 成年人免费网站在线观看 | 国产成人精品免高潮在线观看 | 江苏少妇性BBB搡BBB爽爽爽 | 亚洲欧洲日本在线 | 久久国产亚洲 | 久操伊人 | 色综合天天综合网国产成人网 | 91看点 | 狠狠综合久久av一区二区小说 | 欧美日韩视频在线第一区 | 日韩黄在线 |