基于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实现tcp自动重连
Jul 02 Python
Python判断两个对象相等的原理
Dec 12 Python
解决Python 命令行执行脚本时,提示导入的包找不到的问题
Jan 19 Python
Python使用pymongo库操作MongoDB数据库的方法实例
Feb 22 Python
浅谈python标准库--functools.partial
Mar 13 Python
Django单元测试工具test client使用详解
Aug 02 Python
win10安装tensorflow-gpu1.8.0详细完整步骤
Jan 20 Python
利用 Python ElementTree 生成 xml的实例
Mar 06 Python
利用Python裁切tiff图像且读取tiff,shp文件的实例
Mar 10 Python
基于pycharm实现批量修改变量名
Jun 02 Python
详解Python遍历列表时删除元素的正确做法
Jan 07 Python
Python使用Turtle模块绘制国旗的方法示例
Feb 28 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
php如何实现只替换一次或N次
2015/10/29 PHP
twig模板获取全局变量的方法
2016/02/05 PHP
Zend Framework上传文件重命名的实现方法
2016/11/25 PHP
php实现生成带二维码图片并强制下载功能
2018/02/24 PHP
php关联数组与索引数组及其显示方法
2018/03/12 PHP
Laravel5.1 框架路由基础详解
2020/01/04 PHP
JavaScript中setAttribute用法介绍
2013/07/20 Javascript
javaScript 动态访问JSon元素示例代码
2013/08/30 Javascript
javascript 原型链维护和继承详解
2014/11/26 Javascript
node.js中使用socket.io的方法
2014/12/15 Javascript
JavaScript代码轻松实现网页内容禁止复制(代码简单)
2015/10/23 Javascript
javascript作用域链(Scope Chain)用法实例解析
2015/11/30 Javascript
易操作的jQuery表单提示插件
2015/12/01 Javascript
Nodejs中Express 常用中间件 body-parser 实现解析
2017/05/22 NodeJs
使用vue构建一个上传图片表单
2017/07/04 Javascript
vue-resource拦截器设置头信息的实例
2017/10/27 Javascript
ExtJs使用自定义插件动态保存表头配置(隐藏或显示)
2018/09/25 Javascript
微信小程序学习笔记之跳转页面、传递参数获得数据操作图文详解
2019/03/28 Javascript
在Koa.js中实现文件上传的接口功能
2019/10/08 Javascript
基于element-ui封装表单金额输入框的方法示例
2021/01/06 Javascript
python打开url并按指定块读取网页内容的方法
2015/04/29 Python
python模块之paramiko实例代码
2018/01/31 Python
Python实现批量读取图片并存入mongodb数据库的方法示例
2018/04/02 Python
Pycharm使用远程linux服务器conda/python环境在本地运行的方法(图解))
2019/12/09 Python
Python selenium爬虫实现定时任务过程解析
2020/06/08 Python
详解python定时简单爬取网页新闻存入数据库并发送邮件
2020/11/27 Python
详解webapp页面滚动卡顿的解决办法
2018/12/26 HTML / CSS
美国在线家具网站:GDFStudio
2021/03/13 全球购物
大学新生欢迎词
2014/01/10 职场文书
初中校园之声广播稿
2014/01/15 职场文书
优秀大学生求职自荐信范文
2014/04/19 职场文书
洗手间标语
2014/06/23 职场文书
优秀班主任材料
2014/12/16 职场文书
教师工作表现自我评价
2015/03/05 职场文书
地震捐款简报
2015/07/21 职场文书
高一化学教学反思
2016/02/22 职场文书