# Python 之 多线程下载

QQ图片20151025173309.png

QQ截图20151025173510.png

``````import urllib.request;                        #导入urllib.request库

url='http://www.example.com/1.txt';
req=urllib.request.Request(url);            #得到该URL的请求HTTP头
u=urllib.request.urlopen(req);                #用修改好的请求头打开链接
``````

``````import threading;
def __init__(self, ):                        #定义构造方法
self.url = url;
def run():
#以下写分片下载的代码
``````

``````Download_Thread(#需要的参数).start();
``````

``````得到每个线程返回的数据并将其插入文件中。
``````

``````f=open('1.txt','a+');
f.seek(#插入位置);            #这里的插入位置指从文件起始的第n个字节处开始
f.write(data);
``````

``````import urllib.request,threading;
def __init__(self, url,start_bit,end_bit,f):    #构造方法，形参
self.url = url;
self.start_bit = start_bit;
self.end_bit=end_bit;
self.f=f;

def run():
req = urllib.request.Request(url);            #得到请求头
u=urllib.request.urlopen(url);                #上面是添加Range字段

self.data=[];                                #下载开始
block=8192;
while True:
if(len(buf)==0):
break;
self.data.append(buf);
Insert();                                    #调用插入文件函数

def Insert():
self.f.seek(self.start_bit);                #文件寻址到start_bit
i=0;
while(i``````

1. xiao说道：

博主 你的博客小人物怎么实现的
{"ft_lang_label":"__label__zh","ft_lang_prob":0.6413263,"math_prob":0.42558122,"size":1879,"snap":"2020-45-2020-50","text_gpt3_token_len":888,"char_repetition_ratio":0.14293334,"word_repetition_ratio":0.018181818,"special_character_ratio":0.25651944,"punctuation_ratio":0.2874618,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9746291,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-21T13:03:09Z\",\"WARC-Record-ID\":\"<urn:uuid:ce794c59-de56-48d1-bfb8-87fd7e0af01d>\",\"Content-Length\":\"38250\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:eecc396c-6683-4467-88d5-819ffecf6300>\",\"WARC-Concurrent-To\":\"<urn:uuid:ed80d1be-4d64-43e2-9174-9179e748f60a>\",\"WARC-IP-Address\":\"47.75.42.144\",\"WARC-Target-URI\":\"https://rickyhao.com/2015/10/25/103/\",\"WARC-Payload-Digest\":\"sha1:5WIBKCFYH3J2TLU2SYZ5CSWPKN7R55YR\",\"WARC-Block-Digest\":\"sha1:N77EXUG5J7JI6EOVBIBGXVTYR3KOAZ4O\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107876500.43_warc_CC-MAIN-20201021122208-20201021152208-00148.warc.gz\"}"}