The urllib of Python

Source: Internet
Author: User

#!/usr/bin/env python

#coding: UTF8

Import Urllib


def print_list (list):

For I in list:

Print I

Def demo ():

S=urllib.urlopen ("http://dbspace.blog.51cto.com") #打开URL

Lines=s.readlines ()

Print S.read (#读出所有的数据)

Print s.readlines# read out all displayed in a list

Print s.readline# Read only one row of data, to more can be used for loop

Print_list (lines) #逐行打印出来, with Print_list

Print S.getcode () #打印应答码, which can be returned as 200, does not exist for 404

###

info (): Returns information about the HTTP header

Msg=s.info ()

Contents of print Msg.headers#http header

Print Msg.items () #解析过的头的列表

# # #通过dir (msg) method to see more methods

# #把远程的文件下载到本地 (remote address, save file name, download progress)

Def retrieve ():

Urllib.urlretrieve ("http://dbspace.blog.51cto.com", "index.html", reporthook=process)


def process (blk,blk_size,total_size): #监控当前下载状态进度信息reporthook (number of blocks currently transferred, block size, total data size)

def process (blk,blk_size,total_size):

print '%d/%d-%.02f% '% (blk*blk_size,total_size,float (blk*blk_size) *100/total_size)

More parameters:

Urllib.urlencode (parametric) #加密

Import Urlparse

Urlparse.parse_qs (parametric) #解密

If __name__== "__main__":

Demo ()

Retrieve ()


def download (stock_list):

For SID in Stock_list:

Url= ' http://table.finace.yahoo.com/table.csv?s= ' +sid

fname=sid+ '. csv '

Urllib.urlretrieve (url,fname) # #这样就是下载所有数据


def download_data_in_period (stock_list,start,end):

For SID in Stock_list:

Params={' A ': start.month-1, ' B ': Start.day, ' C ': start.year, ' d ': end.month-1, ' e ': End.day, ' F ': end.year, ' s ': Sid}

Qs=urllib.urlencode (params)

Url=url+qs

Fname= '%s_%d%d%d_%d%d%d.csv '% (sid,start.year,.....)

Urllib.urlretrieve (Url,fname)

If __name__== "__name__":

stock_list=[' 222.sz ', ' 3333.SZ ']

Download (stock_list)

End=datetime.date (Year=2016,month=12,day=1)

Start=datetime.date (Year=2016,month=11,day=1)

Download_data_in_period (Stock_list,start,end)

This article is from the "DBSpace" blog, so be sure to keep this source http://dbspace.blog.51cto.com/6873717/1878690

The urllib of Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.