从经过身份验证的站点获取文件(使用 python urllib、urllib2) [英] Getting a file from an authenticated site (with python urllib, urllib2)

查看:25
本文介绍了从经过身份验证的站点获取文件(使用 python urllib、urllib2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从站点获取查询的 excel 文件.当我输入直接链接时,它会进入一个登录页面,一旦我输入了我的用户名和密码,它将继续自动下载 Excel 文件.我试图避免安装不属于标准 python 的附加模块(此脚本将在标准化机器"上运行,如果未安装该模块,它将无法工作)

I'm trying to get a queried-excel file from a site. When I enter the direct link, it will lead to a login page and once I've entered my username and password, it will proceed to download the excel file automatically. I am trying to avoid installing additional module that's not part of the standard python (This script will be running on a "standardize machine" and it won't work if the module is not installed)

我尝试了以下操作,但我在 excel 文件中看到了页面登录"信息:-|

I've tried the following but I see a "page login" information in the excel file itself :-|

import urllib

url = "myLink_queriedResult/result.xls"
urllib.urlretrieve(url,"C:\\test.xls")

SO .. 然后我考虑使用 urllib2 和密码认证,但后来我被卡住了.

SO.. then I looked into using urllib2 with password authentication but then I'm stuck.

我有以下代码:

import urllib2
import urllib

theurl = 'myLink_queriedResult/result.xls'
username = 'myName'
password = 'myPassword'

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, theurl, username, password)

authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(theurl)
pagehandle.read()  ##but seems like it still only contain a 'login page'   

提前感谢任何建议.:)

Appreciate any advice in advance. :)

推荐答案

Urllib 现在通常被避开用于 请求.

Urllib is generally eschewed these days for Requests.

这会做你想做的:

import requests
from requests.auth import HTTPBasicAuth

theurl= 'myLink_queriedResult/result.xls'
username = 'myUsername'
password = 'myPassword'

r=requests.get(theurl, auth=HTTPBasicAuth(username, password))

您可以在此处找到更多有关使用请求进行身份验证的信息.

这篇关于从经过身份验证的站点获取文件(使用 python urllib、urllib2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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