我不确定这个原则是否适用于 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