【发布时间】:2014-03-24 17:39:36
【问题描述】:
我正在尝试编写一个程序来读取网页以查找文件链接,然后尝试使用 curl/libcurl/pycurl 进行下载。我让 pycurl 正常工作,当我在终端中使用 curl 命令时,我可以下载文件。 curl 命令如下所示:
curl -LO https://archive.org/download/TheThreeStooges/TheThreeStooges-001-WomanHaters1934moeLarryCurleydivxdabaron19m20s.mp4
这会导致一个重定向(一个在输出中读取为全 0 的文件),然后它会正确下载该文件。当我删除 -L 标志(所以命令只是 -O)时,它只会到达第一行,在那里找不到文件,然后停止。
但是当我尝试在 Python 脚本中使用 pycurl 执行相同的操作时,我无法成功地将 [Curl object].FOLLOWLOCATION 设置为 1,这应该相当于 -L 标志。 python代码如下所示:
c = [class Curl object] # get a Curl object
fp = open(file_name,'wb')
c.setopt(c.URL , full_url) # set the url
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.WRITEDATA , fp)
c.perform()
当它运行时,它会到达 c.perform() 并显示以下内容:
python2.7: src/pycurl.c:272: get_thread_state: Assertion `self->ob_type == p_Curl_Type' failed.
是错过了重定向,还是因为我对 cURL 比较陌生,所以我之前错过了其他东西?
【问题讨论】:
标签: python curl file-io libcurl pycurl