coding=UTF-8

import re
import urllib.request, http.cookiejar, urllib.parse #
# print('---------------第一种方法----------------------')
# URL = 'https://baike.baidu.com/item/%E5%B7%B4%E6%B2%99%E5%B0%94%C2%B7%E9%98%BF%E8%90%A8%E5%BE%B7/2867946?fromtitle=%E9%98%BF%E8%90%A8%E5%BE%B7&fromid=9693472'
# response = urllib.request.urlopen(URL)
# if response.getcode() == 200:
# conf = response.read()
# print(conf)
# else:
# print('Fail')
#
# print('---------------第二种方法----------------------')
# # 创建 request 对象
# request = urllib.request.Request(URL)
#
# # 封装 request 对象
# request.add_header('User-Agent', 'Mozilla/5.0')
#
# # 发送带头信息的请求
# response1 = urllib.request.urlopen(request)
# if response1.getcode() == 200:
# conf = response1.read()
# print(conf)
# else:
# print('Fail') print('---------------第三种方法----------------------')
URL2 = 'http://lczl.cnki.net/jbdetail/index?query=1'
URL3 = 'http://r.cnki.net/Klogin/Login.aspx?ReturnUrl=http://lczl.cnki.net/jbdetail/index?query=1'
# 创建cookieJar作为cookie容器
cj = http.cookiejar.CookieJar() # 创建一个opener
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) # 给urllib.request安装opener
urllib.request.install_opener(opener) # 封装 request 对象
request1 = urllib.request.urlopen(URL2) def getVIEWSTATE(data):
cer = re.compile('name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*)\"', flags=0)
strlist = cer.findall(data)
# print(data)
return strlist[0] VIEWSTATE = getVIEWSTATE(request1.read().decode())
# print(VIEWSTATE)
data = {'__VIEWSTATE': VIEWSTATE.encode(), 'userName': '345666561@qq.com', 'passWord': '215501',
'iplogin': 0} # 登陆用户名和密码
post_data = urllib.parse.urlencode(data).encode() request2 = urllib.request.Request(URL3, post_data)
request2.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0') response2 = urllib.request.urlopen(request2)
if response2.getcode() == 200:
conf = response2.read()
print(conf.decode('utf8'))
print(cj)
else:
print('Fail')

三种urllib实现网页下载,含cookie模拟登陆的更多相关文章

  1. 使用ApiPost测试接口时需要先登录怎么办?利用Cookie模拟登陆!

    ApiPost简介: ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工具 . 下 ...

  2. Django-多对多关系的三种创建方式-forms组件使用-cookie与session-08

    目录 表模型类多对多关系的三种创建方式 django forms 组件 登录功能手写推理过程 整段代码可以放过来 forms 组件使用 forms 后端定义规则并校验结果 forms 前端渲染标签组件 ...

  3. 体温数据上传程序开发+获取时间的三种方法+DB Browser下载及安装

    今天开始了体温上传程序的开发 今日所学: 获取时间 (21条消息) (转)安卓获取时间的三种方法_sharpeha的博客-CSDN博客_安卓获取时间 DB Browser安装教程 (20条消息) sq ...

  4. springboot三种配置文件上传下载大小的配置

    配置文件为application.yml格式: spring: http: multipart: enabled: true max-file-size: 30MB max-request-size: ...

  5. React中使用 react-router-dom 路由传参的三种方式详解【含V5.x、V6.x】!!!

    路由传值的三种方式(v5.x) params参数 //路由链接(携带参数): <Link to='/demo/test/tom/18'}>详情</Link> //或 <L ...

  6. 三种方法教你如何用PHP模拟post提交数据

    php模拟post传值在日常的工作中用到的不是很多,但是在某些特定的场合还是经常用到的. 下面,我整理了三种php模拟post传值的方法,file_get_contents.curl和socket. ...

  7. selenium3.7+ python3 添加cookie模拟登陆

    一.背景介绍 最近做一个爬虫项目,用selenium调用浏览器去获取渲染后的源码,但是每次登陆都需要手机验证,这真的是头痛啊,这种验证方式不要想着去破解,还是老老实实用手机收验证码去吧!反正我是不知道 ...

  8. ZYNQ SGI、PPI、SPI三种中断的实例(含代码)

    ZYNQ中断分为3类: SGI(Software Generated Interrupts)软件中断 PPI(Private Peripheral Interrupts)私有外设中断 SPI(Shar ...

  9. 爬虫——cookie模拟登陆

    cookie适用于抓取需要登录才能访问的页面网站 cookie和session机制 http协议为无连接协议,cookie: 存放在客户端浏览器,session: 存放在Web服务器 人人网登录案例 ...

随机推荐

  1. 2.GlusterFS 安装配置

    2.1 GlusterFS 安装前的准备 服务器规划:(vmware 实验) 操作系统 IP 主机名 数据盘(2 块) CentOS 6.8 x86_64 10.1.0.151 mystorage1 ...

  2. March 15 2017 Week 11 Wednesday

    The starting point of all achievements is desire. 成功的第一步是渴望. Only you desire for somethings, you can ...

  3. 卡方分布、卡方独立性检验和拟合性检验理论及其python实现

    如果你在寻找卡方分布是什么?如何实现卡方检验?那么请看这篇博客,将以通俗易懂的语言,全面的阐述卡方.卡方检验及其python实现. 1. 卡方分布 1.1 简介 抽样分布有三大应用:T分布.卡方分布和 ...

  4. 【转】ssh timed out 超时解决方案

    转自:http://www.cnblogs.com/niutouzdq/p/4091268.html 在使用阿里云ECS服务器的时候,winsftp经常被服务器断开,想必是过一会没有操作,防火墙喜欢对 ...

  5. Centos6.5(Linux)安装Nginx

    1.安装nginx依赖的库pcre       下载地址:http://sourceforge.net/projects/pcre/    2.解压pcre        zip解压方式:unzip ...

  6. 【luogu P1306 斐波那契公约数】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1306#sub gcd(f[m],f[n]) = f[gcd(m,n)] #include <iostr ...

  7. WPF中 ItemsSource 和DataContext不同点

    此段为原文翻译而来,原文地址 WPF 中 数据绑定 ItemSource和 DataContext的不同点: 1.DataContext 一般是一个非集合性质的对象,而ItemSource 更期望数据 ...

  8. c语言描述的双向链表的基本操作

    #include<stdio.h> #include<stdlib.h> #define ok 1 #define error 0 typedef int Status; ty ...

  9. 18年selenium3+python3+unittest自动化测试教程(下)

    第六章 自动化测试进阶实战篇幅 1.自动化测试实战进阶之网页单选性别资料实战 简介:讲解使用selenium修改input输入框和单选框 2.自动化测试之页面常见弹窗处理 简介:讲解使用seleniu ...

  10. CentOS 7 下 Oracle 11g 安装教程

    一.准备工作 1.关闭selinux   查看selinux状态:   getenforce或者sestatus -v   临时关闭:   setenforce 0   永久关闭:   vim /et ...