用requests模块重新写了下。
需要启用requests的会话保持。要不然登录后,访问其他网页会断掉连接
import urllib2
import urllib
import cookielib
import re
import sys
reload(sys)
import requests
from bs4 import BeautifulSoup
sys.setdefaultencoding(“utf-8”)
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders=[(“User-Agent”,”Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36”),]
urllib2.install_opener(opener)
url = ‘http://www.xinxianwang.com/login/login.asp’
values = {
‘username’:’cqmyg12342322’,
‘password’:’cq123456’,
‘k’:’Fri Oct 23 2015 23:39:12 GMT+0800 (中国标准时间)40000’
}
post_header ={
“Referer”:”http://www.xinxianwang.com/login/“,
“User-Agent”:”Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36”,
“Accept-Encoding”:”gzip, deflate”,
“Accept-Language”:”zh-CN,zh;q=0.8”,
“Connection”:”keep-alive”,
“Content-Length”:”175”,
“Content-Type”:”application/x-www-form-urlencoded”
}
cj.save(‘H:\python_learn/wangyi.txt’)
s=requests.Session()
r = s.post(url, data=values,headers=post_header)
print r.content
mm = s.get(‘http://www.xinxianwang.com/my/‘,timeout=20).content
soup = BeautifulSoup(mm)
print soup.title