基于Python模拟浏览器发送http请求


Posted in Python onNovember 06, 2020

1.使用 urllib2 实现

#! /usr/bin/env python
# -*- coding=utf-8 -*- 

import urllib2
url="https://www.baidu.com"
req_header = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
"Accept":"text/html;q=0.9,*/*;q=0.8",
"Accept-Charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"Accept-Encoding":"gzip",
"Connection":"close",
"Referer":None #注意如果依然不能抓取的话,这里可以设置抓取网站的host
}
req_timeout = 5
req = urllib2.Request(url,None,req_header)
resp = urllib2.urlopen(req,None,req_timeout)
html = resp.read()
print(html)

2.使用 requests 模块

(1).get请求

#-*- coding:utf-8 -*-
import requests

url = "https://www.baidu.com"
payload = {"key1": "value1", "key2": "value2"}
r = requests.get(url, params=payload)
print r.text

(2).post请求

#-*- coding:utf-8 -*-
import requests
url1 = "http://www.exanple.com/login"#登陆地址
url2 = "http://www.example.com/main"#需要登陆才能访问的地址
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
      "Accept-Encoding":"gzip",
      "Accept-Language":"zh-CN,zh;q=0.8",
      "Referer":"http://www.example.com/",
      "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
      }
res1 = requests.post(url1, data=data, headers=headers)
res2 = requests.get(url2, cookies=res1.cookies, headers=headers)

print res2.content#获得二进制响应内容
print res2.raw#获得原始响应内容,需要stream=True
print res2.raw.read(50)
print type(res2.text)#返回解码成unicode的内容
print res2.url
print res2.history#追踪重定向
print res2.cookies
print res2.cookies["example_cookie_name"]
print res2.headers
print res2.headers["Content-Type"]
print res2.headers.get("content-type")
print res2.json#讲返回内容编码为json
print res2.encoding#返回内容编码
print res2.status_code#返回http状态码
print res2.raise_for_status()#返回错误状态码

(3).使用session对象的写法

#-*- coding:utf-8 -*-
import requests
s = requests.Session()
url1 = "http://www.exanple.com/login"#登陆地址
url2 = "http://www.example.com/main"#需要登陆才能访问的地址
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
      "Accept-Encoding":"gzip",
      "Accept-Language":"zh-CN,zh;q=0.8",
      "Referer":"http://www.example.com/",
      "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
      }

prepped1 = requests.Request("POST", url1,
  data=data,
  headers=headers
).prepare()
s.send(prepped1)


"""
也可以这样写
res = requests.Request("POST", url1,
data=data,
headers=headers
)
prepared = s.prepare_request(res)
# do something with prepped.body
# do something with prepped.headers
s.send(prepared)
"""

prepare2 = requests.Request("POST", url2,
  headers=headers
).prepare()
res2 = s.send(prepare2)

print res2.content


"""另一种写法"""
#-*- coding:utf-8 -*-
import requests
s = requests.Session()
url1 = "http://www.exanple.com/login"#登陆地址
url2 = "http://www.example.com/main"#需要登陆才能访问的页面地址
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
      "Accept-Encoding":"gzip",
      "Accept-Language":"zh-CN,zh;q=0.8",
      "Referer":"http://www.example.com/",
      "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
      }
res1 = s.post(url1, data=data)
res2 = s.post(url2)
print(resp2.content)

3.其他的一些请求方式

>>> r = requests.put("http://httpbin.org/put")
>>> r = requests.delete("http://httpbin.org/delete")
>>> r = requests.head("http://httpbin.org/get")
>>> r = requests.options(http://httpbin.org/get)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
初学Python实用技巧两则
Aug 29 Python
Python用模块pytz来转换时区
Aug 19 Python
定制FileField中的上传文件名称实例
Aug 23 Python
Python安装Numpy和matplotlib的方法(推荐)
Nov 02 Python
python之pexpect实现自动交互的例子
Jul 25 Python
正则给header的冒号两边参数添加单引号(Python请求用)
Aug 09 Python
Python多叉树的构造及取出节点数据(treelib)的方法
Aug 09 Python
django 文件上传功能的相关实例代码(简单易懂)
Jan 22 Python
pycharm 配置svn的图文教程(手把手教你)
Jan 15 Python
【超详细】八大排序算法的各项比较以及各自特点
Mar 31 Python
教你怎么用python selenium实现自动化测试
May 27 Python
Pytest中skip skipif跳过用例详解
Jun 30 Python
python如何写个俄罗斯方块
Nov 06 #Python
基于Python实现全自动下载抖音视频
Nov 06 #Python
Python3读写ini配置文件的示例
Nov 06 #Python
Python Serial串口基本操作(收发数据)
Nov 06 #Python
python基于exchange函数发送邮件过程详解
Nov 06 #Python
Python Unittest原理及基本使用方法
Nov 06 #Python
python中的yield from语法快速学习
Nov 06 #Python
You might like
海贼王:最美的悬赏令!
2020/03/02 日漫
基于Zend的Config机制的应用分析
2013/05/02 PHP
功能强大的php文件上传类
2016/08/29 PHP
PHP快速排序quicksort实例详解
2016/09/28 PHP
php-beanstalkd消息队列类实例分享
2017/07/19 PHP
使用PHP连接数据库_实现用户数据的增删改查的整体操作示例
2017/09/01 PHP
jQuery-Tools-overlay 使用介绍
2012/07/14 Javascript
将json对象转换为字符串的方法
2014/02/20 Javascript
jquery 操作css样式、位置、尺寸方法汇总
2014/11/28 Javascript
DOM节点删除函数removeChild()用法实例
2015/01/12 Javascript
javascript多行字符串的简单实现方式
2015/05/04 Javascript
javascript检测两个数组是否相似
2015/05/19 Javascript
基于jQuery实现淡入淡出效果轮播图
2020/07/31 Javascript
微信小程序 本地存储及登录页面处理实例详解
2017/01/11 Javascript
javascript实现简易聊天室
2019/07/12 Javascript
vue列表数据发生变化指令没有更新问题及解决方法
2020/01/16 Javascript
详解JavaScript作用域 闭包
2020/07/29 Javascript
使用PyV8在Python爬虫中执行js代码
2017/02/16 Python
Python3实现发送QQ邮件功能(附件)
2020/12/23 Python
Python机器学习之SVM支持向量机
2017/12/27 Python
python实现员工管理系统
2018/01/11 Python
浅析python打包工具distutils、setuptools
2018/04/20 Python
python实现将读入的多维list转为一维list的方法
2018/06/28 Python
Python图像滤波处理操作示例【基于ImageFilter类】
2019/01/03 Python
Python使用Pickle模块进行数据保存和读取的讲解
2019/04/09 Python
在python shell中运行python文件的实现
2019/12/21 Python
基于python及pytorch中乘法的使用详解
2019/12/27 Python
Python 利用Entrez库筛选下载PubMed文献摘要的示例
2020/11/24 Python
基于Python中Remove函数的用法讨论
2020/12/11 Python
Python接口自动化系列之unittest结合ddt的使用教程详解
2021/02/23 Python
韩国流行时尚女装网站:Dintchina(中文)
2018/07/19 全球购物
介绍一下Make? 为什么使用make
2013/12/08 面试题
医学生实习自荐信
2013/10/01 职场文书
新闻编辑专业自荐信
2014/07/02 职场文书
财务工作犯错检讨书
2014/10/07 职场文书
Python Pandas pandas.read_sql_query函数实例用法分析
2021/06/21 Python