python Spider Man(爬虫侠)三,之cookie进行人人网页面爬取

from urllib import request,parse

from http import cookiejar

cookie = cookiejar.CookieJar()#生成cookie对象
cookie_handler = request.HTTPCookieProcessor(cookie)
http_handler = request.HTTPHandler()#  http请求管理器
https_handler = request.HTTPSHandler()#  https请求管理器
#发起请求管理器
opener = request.build_opener(https_handler,https_handler,cookie_handler)

#登录
def login():
        login_url = "http://www.renren.com/PLogin.do"
        data = {
                "email": "你的人人网账号",
                "password": "你的人人网密码"
        }
        data = parse.urlencode(data)
        req = request.Request(login_url,data=bytes(data,'utf-8'))
        opener.open(req)

def ht():
       
 base_url = 'http://www.renren.com/964508633/profile'#这个通过你的人人网个人主页修改,下面有查找图片
resp = opener.open(base_url).read().decode()
with open('wwww.html', 'w', encoding='utf-8') as f:
                f.write(resp)
if __name__ == "__main__":
    login()
    ht()
python Spider Man(爬虫侠)三,之cookie进行人人网页面爬取_第1张图片

你可能感兴趣的:(python3爬虫学习路上)