我使用URLlib2(和python 2.7)從網站抓取一些內容。到目前爲止,我一直使用URLlib2 OK來獲取內容,但這是我第一次訪問內容級密碼的網站。我有一個合法的u:p(我顯然不能在這裏分享),看起來我沒有按照我的要求給出正確的憑證。python urllib2,密碼內容和空響應
我使用的方法在這裏:Python urllib2, basic HTTP authentication, and tr.im更換(username, password)
我的憑據作爲字符串(「名爲myUsername」,「MYPASSWORD」)
當我print result.read()
我得到一個空行,當我嘗試print result.headers()
我得到:
<addinfourl at 40895752L whose fp = <socket._fileobject object at 0x00000000026757C8>>
爲例,爲號召,我以爲是指的每一個實例預期有一個文件對象有各種各樣的......
我試圖print result.info()
,看看是否有一個頭回來了,我看到一組頭:
REDACTED
Date: Mon, 01 Oct 2012 10:06:24 GMT
Server: Apache/2.2.3 (Red Hat)
X-Powered-By: PHP/5.1.6
Set-Cookie: OJSSID=mc7u47e674jmpjgk3kspfgc9l3; path=/
Refresh: 0; url=http:REDACTED loginMessage=reader.subscriptionRequiredLoginText
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8
所以我可以從「loginMessage = reader.subscriptionRequiredLoginText」我已經無法正常發送的憑據。
任何指針?
調用代碼:
def getArticle(newLink):
request = urllib2.Request(newLink)
base64string = base64.encodestring('%s:%s' % ("myUsername", "myPassword")).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
print result.read()
和示例URL爲:絕密 - 它不是我的網站!
謝謝,這真的很有用,我打算(1)編輯URL信息和(2)將此標記爲已回答,等待我的解決方案,我相信解決方案在於正確處理表單u:p :)謝謝你的時間。 –