大漠駝鈴

          置身浩瀚的沙漠,方向最為重要,希望此blog能向大漠駝鈴一樣,給我方向和指引。
          Java,Php,Shell,Python,服務器運維,大數據,SEO, 網站開發、運維,云服務技術支持,IM服務供應商, FreeSwitch搭建,技術支持等. 技術討論QQ群:428622099
          隨筆 - 238, 文章 - 3, 評論 - 117, 引用 - 0
          數據加載中……

          Python Urllib2


          http://docs.python.org/library/urllib.html


          設置超時時間

               import socket

          import urllib2

          socket.setdefaulttimeout(seconds)

          open = urllib2.urlopen("

          過socket.setdefaulttimeout() 設置了全局默認超時時間,從而給urllibe2.urlopen()也設置了默認的超時時間





          抓取圖片(這個例子里的Request找不到,我一般只有urllib2)
          soup=urlllib2.open(url)

          # Let's create a function that downloads a file, and saves it locally.
          #
           This function accepts a file name, a read/write mode(binary or text),
          #
           and the base url.

          def stealStuff(file_name,file_mode,base_url):
              
          from urllib2 import Request, urlopen, URLError, HTTPError

              
          #create the url and the request
              url = base_url + file_name
              req 
          = Request(url)

              
          # Open the url
              try:
                  f 
          = urlopen(req)
                  
          print "downloading " + url

                  
          # Open our local file for writing
                  local_file = open(file_name, "w" + file_mode)
                  
          #Write to our local file
                  local_file.write(f.read())
                  local_file.close()

              
          #handle errors
              except HTTPError, e:
                  
          print "HTTP Error:",e.code , url
              
          except URLError, e:
                  
          print "URL Error:",e.reason , url

          # Set the range of images to 1-50.It says 51 because the
          #
           range function never gets to the endpoint.
          image_range = range(1,51)

          # Iterate over image range
          for index in image_range:

              base_url 
          = 'http://www.techniqal.com/'
              
          #create file name based on known pattern
              file_name =  str(index) + ".jpg"
              
          # Now download the image. If these were text files,
              # or other ascii types, just pass an empty string
              # for the second param ala stealStuff(file_name,'',base_url)
              stealStuff(file_name,"b",base_url)

          posted on 2011-03-04 16:02 草原上的駱駝 閱讀(1173) 評論(0)  編輯  收藏 所屬分類: Python

          主站蜘蛛池模板: 青阳县| 牡丹江市| 枣阳市| 磐石市| 石屏县| 甘洛县| 黔西县| 安陆市| 贡觉县| 新邵县| 萨嘎县| 乌兰县| 淮南市| 太湖县| 利津县| 独山县| 施秉县| 皋兰县| 东安县| 桐梓县| 尖扎县| 澄江县| 鹤壁市| 乐业县| 霸州市| 南溪县| 金门县| 渭源县| 宝山区| 凤山县| 古交市| 吉安县| 南投市| 吉水县| 蓬溪县| 新绛县| 瓦房店市| 台前县| 左贡县| 乌恰县| 哈巴河县|