1

我正在学习使用 urllib2 和 urllib。我根据http://docs.python.org/howto/urllib2.html的给定示例编写了相同的脚本。但是好像不行。响应的是登录页面的源代码!我用firebug检查是否返回相同的源代码,答案是肯定的!但是我仍然可以在浏览器上登录我的博客!我需要知道什么样的知识?网址?或者 ......

#! /usr/bin/python
# -*- coding: utf-8 -*-

import urllib2
import urllib

url='http://www.mywordpressblog.com/wp-login.php'
values={'log':'my login name',
        'pwd':'my password'}
data=urllib.urlencode(values)
req=urllib2.Request(url,data)
response=urllib2.urlopen(req)
the_page=response.read()
print the_page
4

3 回答 3

3

我不确定这个原则是否适用于 Wordpress,但是要使用基本的 http 身份验证来扩充您的代码,您应该:

import urllib2
import urllib
import base64


USERNAME="user"
PASSWORD="password"

url='http://www.mywordpressblog.com/wp-login.php'
req = urllib2.Request(url)

# create http header with base64 encoded user and password
base64string = base64.encodestring(
    '%s:%s' % (USERNAME, PASSWORD))[:-1]
authheader =  "Basic %s" % base64string
req.add_header("Authorization", authheader)

response =urllib2.urlopen(req)
the_page=response.read()
print the_page

要获取更多信息,请查看基本身份验证教程

更新

POST您应该发出带有标题字段的https请求,并User-Agent模拟浏览器之一和 urlencoded 凭据,如下所示:

import urllib2
import urllib


url='https://en.wordpress.com/wp-login.php'

headers = [
  ("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5")]

data = [
    ("log","YOURNAME"), 
    ("pwd","YOURPASSWORD"), 
    ("testcookie",1), 
    ("submit","Log In"), 
    ("redirect_to","http://wordpress.com/"), 
    ("rememberme","forewer")]

req = urllib2.Request(url, urllib.urlencode(dict(data)), dict(headers))
response = urllib2.urlopen(req)

the_page=response.read()
print the_page
于 2012-05-17T07:21:15.900 回答
0

You must use HTTPBasicAuthHandler for login into your blog. See docs at "Basic Authentication" section.

于 2012-05-17T06:40:57.387 回答
0

使用requests.

data = {'username': 'foo', 'password': 'secret'}
url = 'http://www.example.com/'
r = requests.post(url, data=data)
于 2012-05-18T19:48:44.617 回答