打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
PycURL登录页面下载文件

PycURL example
Thu, 2006-12-21 22:04 — christianmlong

Here’s a little sample of Python code demonstrating the use of PycURL, the Python interface to libcURL. It does the same thing as my cURL example. Refer to this page for a detailed list of libcurl options.

import pycurl, StringIO
# Constants
DOWNLOADED_FILE = r’C:\temp\downloaded_file.txt’
USER_AGENT = ‘Mozilla/4.0 (compatible; MSIE 6.0)’
LOGIN_URL = ‘http://interesting.website.com/LogIn.asp’
LOGIN_POST_DATA = ‘FormField=URL%20Encoded%20Value’
DOWNLOAD_URL = ‘http://interesting.website.com/do_it.asp?do=0&something=0&interesting=0′
DOWNLOAD_REFERER = ‘http://interesting.website.com/referer.asp’
FILE_MODE = ‘wb’

# Set up objects
dev_null = StringIO.StringIO()
slurpp = pycurl.Curl()

# Request login page
slurpp.setopt(pycurl.USERAGENT, USER_AGENT)
slurpp.setopt(pycurl.FOLLOWLOCATION, 1)
#slurpp.setopt(pycurl.AUTOREFERER, 1) # not yet implemented in pycURL
slurpp.setopt(pycurl.WRITEFUNCTION, dev_null.write)
slurpp.setopt(pycurl.COOKIEFILE, ”)
slurpp.setopt(pycurl.URL, LOGIN_URL)
slurpp.perform()

# Log in to site
slurpp.setopt(pycurl.POSTFIELDS, LOGIN_POST_DATA)
slurpp.setopt(pycurl.POST, 1)
slurpp.perform()

# Download relevant data
slurpp.setopt(pycurl.HTTPGET, 1)
slurpp.setopt(pycurl.URL, DOWNLOAD_URL)
slurpp.setopt(pycurl.REFERER, DOWNLOAD_REFERER)
outfile = file(DOWNLOADED_FILE, FILE_MODE)
slurpp.setopt(pycurl.WRITEFUNCTION, outfile.write)
slurpp.perform()

# Clean up and close out
outfile.close()
dev_null.close()
slurpp.close()

本文标签:
本文链接: PycURL登录页面下载文件
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Bloglines | My Blogs
Python PycURL 网络编程 [Python俱乐部]
python中使用pycurl抓取https链接,及pycurl的参数设置
Py之pycurl:Python 库之pycurl的简介、安装、使用方法之详细攻略
pycurl的学习
(1)python写简单爬虫的五种方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服