HTTPS与urllib2的登录 [英] HTTPS log in with urllib2

查看:159
本文介绍了HTTPS与urllib2的登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有下载一个网页,并提取了一些数据我感兴趣的,没什么特别的。

I currently have a little script that downloads a webpage and extracts some data I'm interested in. Nothing fancy.

目前,我下载的页面,像这样:

Currently I'm downloading the page like so:

import commands
command = 'wget --output-document=- --quiet --http-user=USER --http-password=PASSWORD https://www.example.ca/page.aspx'
status, text = commands.getstatusoutput(command)

虽然这个完美的作品,我觉得这一定是有意义的消除对wget的依赖。我认为这应该是微不足道的上述转换的urllib2,但迄今为止我已经成功为零。互联网是充满urllib2的例子,但我还没有找到符合我的需要进行简单的用户名和密码的HTTP认证与HTTPS服务器的任何东西。

Although this works perfectly, I thought it'd make sense to remove the dependency on wget. I thought it should be trivial to convert the above to urllib2, but thus far I've had zero success. The Internet is full urllib2 examples, but I haven't found anything that matches my need for simple username and password HTTP authentication with a HTTPS server.

推荐答案

借助请求模块提供了现代化的API到HTTP / HTTPS的能力。

The requests module provides a modern API to HTTP/HTTPS capabilities.

import requests

url = 'https://www.someserver.com/toplevelurl/somepage.htm'

res = requests.get(url, auth=('USER', 'PASSWORD'))

status = res.status_code
text   = res.text

这篇关于HTTPS与urllib2的登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆