如下所示:
import urllib.request import sys import http.cookiejar import urllib.parse from bs4 import BeautifulSoup import codecs import re #登录页面 url = "http://www.abc.com/login.asp" #登录Post数据 postdata =urllib.parse.urlencode({ "username":"abc", "password":"abc" }).encode('utf-8') #构造header header = { "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding":"utf-8", "Accept-Language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3", "Connection":"keep-alive", "Host":"www.abc.com", "Referer":"http://www.abc.com/login.asp", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0" } req = urllib.request.Request(url,postdata,header) #获得cookie cj = http.cookiejar.CookieJar() opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) r = opener.open(req) #继续访问登陆后的页面 get_url = 'http://www.abc.com/user/' get_request = urllib.request.Request(get_url, headers=header) get_response = opener.open(get_request) #打印获得的页面信息 print(get_response.read().decode("utf-8")) #用bs4可以获得你需要的标签内容 soup = BeautifulSoup(get_response.read(),"html.parser") jie = soup.find_all('span') #这里利用正则表达式过滤掉html语言 dr = re.compile(r'<[^>]+>',re.S) #这里jie[2]表示取符合条件的第三个标签 dd = dr.sub('',str(jie[2])) print(dd)