下载文件时python是否有自己的cookie

时间:2015-03-14 02:07:57

标签: python download operating-system

当我使用python下载web文件时。它得到

  

---------------------------服务器回复说您没有权限下载此文件。

     

详细信息:HTTP / 1.1 403 FORBIDDEN

但是当我使用chrome网站点击链接时,可以毫无问题地下载。为什么呢?

如果是,我可以清理它。我的代码是:

#!/usr/bin/python  
#-*-coding:utf-8-*- 
import urllib2
import os
from subprocess import check_output
def data(url):



    up=urllib2.urlopen(url)

    cont=up.read()

    key1="http://videolectures.net/site/secure_dl/"
    key2=".mp4"

    pa=cont.find(key1)
    pt=cont.find(key2,pa)

    urlx=cont[pa:pt+4]

    return urlx

if __name__ == "__main__":  
    url_main=["http://videolectures.net/mit600f08_grimson_lec01/",
        "http://videolectures.net/mit600f08_grimson_lec02/",
        "http://videolectures.net/mit600f08_grimson_lec03/",
        "http://videolectures.net/mit600f08_grimson_lec04/",
        "http://videolectures.net/mit600f08_grimson_lec07/",
        "http://videolectures.net/mit600f08_grimson_lec08/",
        "http://videolectures.net/mit600f08_grimson_lec09/",
        "http://videolectures.net/mit600f08_grimson_lec10/",
        "http://videolectures.net/mit600f08_grimson_lec15/",
        "http://videolectures.net/mit600f08_grimson_lec16/",
        "http://videolectures.net/mit600f08_guttag_lec05/",
        "http://videolectures.net/mit600f08_guttag_lec06/",
        "http://videolectures.net/mit600f08_guttag_lec11/",
        "http://videolectures.net/mit600f08_guttag_lec12/",
        "http://videolectures.net/mit600f08_guttag_lec13/",
        "http://videolectures.net/mit600f08_guttag_lec14/",
        "http://videolectures.net/mit600f08_guttag_lec17/",
        "http://videolectures.net/mit600f08_guttag_lec18/",
        "http://videolectures.net/mit600f08_guttag_lec19/",
        "http://videolectures.net/mit600f08_guttag_lec20/",
        "http://videolectures.net/mit600f08_guttag_lec21/",
        "http://videolectures.net/mit600f08_guttag_lec22/",
        "http://videolectures.net/mit600f08_guttag_lec23/",
        "http://videolectures.net/mit600f08_guttag_lec24/"]
    file = open('file.txt', 'w')
    file.close()
    for url in url_main:

        print url
        url_video=data(url)
        file = open('file.txt', 'a')
        file.write(url_video+'\n')
        file.close()
        check_output("cmd.exe /k "+"\"C:\Program Files (x86)\Internet Download Manager\IDMan.exe\"  /d "+url_video, shell=True)  

1 个答案:

答案 0 :(得分:0)

你是对的,python有自己的cookie,你必须使用该库,请参阅下面的链接:

https://docs.python.org/2/library/cookie.html