python2中urllib2模块带cookies使用方法
#!/usr/bin/python
# coding=utf-8
#############方式1#########################
import urllib2 cookie = "anonymid=jn5lbcm4-5e6p6j; depovince=HUB; _r01_=1; ick_login=32e4276a-5bbf-4711-a88d-2f28630c3763; ick=3cc0f79b-01d2-485d-8640-2bcaa5021e6b; __utma=151146938.1263734026.1539323353.1539323353.1539323353.1; __utmc=151146938; __utmz=151146938.1539323353.1.1.utmcsr=renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/; first_login_flag=1; ln_uact=15827325743; ln_hurl=http://head.xiaonei.com/photos/0/0/women_main.gif; jebe_key=c7507606-efe7-4c38-8e9c-7e0012754caa%7Cb59bd55b2f9ca172123871fb5854c08d%7C1539323487713%7C1%7C1539323486869; wp_fold=0; jebecookies=3c9f3547-c4c8-4fd5-804a-d1d25bcfe3d6|||||; _de=7789F161F392486A84BB56A3325ED357; p=7f56135138ee5b2b9670a6396256e15b4; t=b9b13d669f798463bb9161405ceda0544; societyguester=b9b13d669f798463bb9161405ceda0544; id=967341964; ver=7.0; xnsid=98e715c7; loginfrom=null"
cookie_dict = {i.split("=")[0]: i.split("=")[1] for i in cookie.split("; ")}
print("cookie_dict", cookie_dict)
opener = urllib2.build_opener()
opener.addheaders.append(('Cookie', cookie))
response = opener.open("http://www.renren.com/941954027/profile")
html = response.read().decode('utf-8') with open("./test_html/renren_urllib2_1.html", "w") as f:
f.write(html.encode("utf-8")) ############方式2##################
import urllib2 opener = urllib2.build_opener()
str = "anonymid=jn5lbcm4-5e6p6j; depovince=HUB; _r01_=1; ick_login=32e4276a-5bbf-4711-a88d-2f28630c3763; ick=3cc0f79b-01d2-485d-8640-2bcaa5021e6b; __utma=151146938.1263734026.1539323353.1539323353.1539323353.1; __utmc=151146938; __utmz=151146938.1539323353.1.1.utmcsr=renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/; first_login_flag=1; ln_uact=15827325743; ln_hurl=http://head.xiaonei.com/photos/0/0/women_main.gif; jebe_key=c7507606-efe7-4c38-8e9c-7e0012754caa%7Cb59bd55b2f9ca172123871fb5854c08d%7C1539323487713%7C1%7C1539323486869; wp_fold=0; jebecookies=3c9f3547-c4c8-4fd5-804a-d1d25bcfe3d6|||||; _de=7789F161F392486A84BB56A3325ED357; p=7f56135138ee5b2b9670a6396256e15b4; t=b9b13d669f798463bb9161405ceda0544; societyguester=b9b13d669f798463bb9161405ceda0544; id=967341964; ver=7.0; xnsid=98e715c7; loginfrom=null;"
opener.addheaders.append(('Cookie', str))
response = opener.open("http://www.renren.com/941954027/profile")
html = response.read().decode('utf-8') with open("./test_html/renren_urllib2_2.html", "w") as f:
f.write(html.encode("utf-8")) ##########################################
import urllib2 opener = urllib2.build_opener()
str = "csrftoken=XeyGDIvTQqIYceqiYTAtvphEj916AkbK; sessionid=15g6yuqojngdo35dz1djwzisvm7q2jwt;"
# str = "anonymid=jn5lbcm4-5e6p6j; depovince=HUB; _r01_=1; ick_login=32e4276a-5bbf-4711-a88d-2f28630c3763; ick=3cc0f79b-01d2-485d-8640-2bcaa5021e6b; __utma=151146938.1263734026.1539323353.1539323353.1539323353.1; __utmc=151146938; __utmz=151146938.1539323353.1.1.utmcsr=renren.com|utmccn=(referral)|utmcmd=referral|utmcct=/; first_login_flag=1; ln_uact=15827325743; ln_hurl=http://head.xiaonei.com/photos/0/0/women_main.gif; jebe_key=c7507606-efe7-4c38-8e9c-7e0012754caa%7Cb59bd55b2f9ca172123871fb5854c08d%7C1539323487713%7C1%7C1539323486869; wp_fold=0; jebecookies=3c9f3547-c4c8-4fd5-804a-d1d25bcfe3d6|||||; _de=7789F161F392486A84BB56A3325ED357; p=7f56135138ee5b2b9670a6396256e15b4; t=b9b13d669f798463bb9161405ceda0544; societyguester=b9b13d669f798463bb9161405ceda0544; id=967341964; ver=7.0; xnsid=98e715c7; loginfrom=null;"
opener.addheaders.append(('Cookie', str))
response = opener.open(
"http://10.67.19.77:9006/bsa_tsa_cqmc/api/v1/analysis/searchEvent/?endTime=1539330356&page=1&pageSize=10&startTime=1536738356&typename=event")
html = response.read().decode('utf-8') with open("./test_html/renren_urllib2_3.json", "w") as f:
f.write(html.encode("utf-8"))
python2中urllib2模块带cookies使用方法的更多相关文章
- 慕课网-Java入门第一季-7-3 Java 中无参带返回值方法的使用
来源:http://www.imooc.com/code/1579 如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值 ...
- Java 中无参带返回值方法的使用
如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值为 int 类型的方法,执行的操作为计算两数之和,并返回结果 在 c ...
- Ruby中Enumerable模块的一些实用方法
我在查看 Array 类和 Hash 类的祖先链的时候都发现了 Enumerable,说明这两个类都mixin了Enumerable模块.Enumerable模块为集合型类提供了遍历.检索.排序等方法 ...
- python中计时模块timeit的使用方法
timeit 模块: timeit 模块定义了接受两个参数的 Timer 类.两个参数都是字符串. 第一个参数是你要计时的语句或者函数. 传递给 Timer 的第二个参数是为第一个参数语句构建环境的导 ...
- python中re模块的match,search方法的比较
match 匹配字符串的开头, search匹配整个字符串
- 爬虫中Requests模块
Requests: 让 HTTP 服务人类 虽然Python的标准库中 urllib2 模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests 自称 “ ...
- 详解:Python2中的urllib、urllib2与Python3中的urllib以及第三方模块requests
在python2中,urllib和urllib2都是接受URL请求的相关模块,但是提供了不同的功能.两个最显著的不同如下: 1.urllib2可以接受一个Request类的实例来设置URL请求的hea ...
- Python2中的urllib、urllib2和 Python3中的urllib、requests
目录 Python2.x中 urllib和urllib2 常用方法和类 Python3.x中 urllib requests Python2.x中 urllib和urllib2 urllib 和 ur ...
- Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块
Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 ...
随机推荐
- java数组知识点总结
数组是一个用来存储同一个数据类型多个元素的一个容器(数组长度是固定的,数组中存储的元素的数据类型要求一致) 1.格式: 格式1: 数据类型[] 数组名 = new 数据类型[数组长度]; 格式2: 数 ...
- lrzsz linix 远程文件传输工具。
安装方法 #yum install lrzsz -y 使用方法 #rz -y 上传指定文档到当前目录
- 2016-06-19 exshop第5天
昨天对grails3和spring-security进行了全面的调研并进行了试验,试用下来发现grails3的启动速度.代码修改后刷新速度.内存占用以及架构的设计上更加合理,asset-pipelin ...
- python全栈开发day69-cookie、session
一.ORM回顾 1. 内容回顾 1. Django中使用ORM连接MySQL的步骤: 1. 创建数据库 2. 告诉Django连接哪个数据库 在settings.py中设置数据库相关的链接信息 3. ...
- windows server远程连接提示“终端服务器超出了最大允许连接”
- PHP远程下载图片,微信头像存到本地,本地图片转base64
方法一(推荐): function download_remote_pic($url){ $header = [ 'User-Agent: Mozilla/5.0 (Windows NT 6.1; W ...
- 51Nod1367 完美森林 贪心
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1367.html 题目传送门 - 51Nod1367 题意 有一棵N个点的树,树中节点标号依次为0,1 ...
- CodeSignal 刷题 —— almostIncreasingSequence
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly incr ...
- 爬虫1 socket方式下载一张图片
import socket import re client = socket.socket() # 图片url img_url = 'https://img03.sogoucdn.com/app/a ...
- es6的分析总结
1,var let const对比 1,箭头函数的总结 /** * 1,箭头函数没有this,箭头函数this没有被箭头的函数,所以不能使用call,apply,bind改变this指向 * 2,箭头 ...