基于Python模拟浏览器发送http请求
作者:shuzihua 发布时间:2023-01-11 22:22:05
标签:Python,模拟,浏览器,http
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)
来源:https://www.cnblogs.com/xinxihua/p/12819997.html
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 前言:内容提要:本文比较了 Python 中用于列表排序的两种函数 sort() 和 sorted(),帮助您选择合适的排序函数。对列表排序
- step函数概述step函数用于绘制阶梯图。根据源码可知,step函数是对plot函数的轻量级封装,很多概念和用法与plot函数非常相似。d
- Python是一种解释型、面向对象、动态数据类型的高级程序设计语言,本文就举一例Python类继承的实例。实例代码如下:#! /usr/bi
- 前言今晚就是新年夜啦,为了 刷一波存在感 送出我的祝福,同时让它看起来不像群发消息,我们简单地用三步来实现定制QQ祝福~
- 打包成exe文件可以让python代码在没有python环境的条件下,依然能够运行,实在是码农们写追女朋友表白、情人节浪漫的必需品!1、使用
- 最近因需要用python写一个微服务来用MQTT给硬件传输图片,其中python用的是flask框架,大概流程如下:协议为:需要将图片数据封
- 这篇文章主要为大家详细介绍了python批量爬取下载抖音视频,具有一定的参考价值,感兴趣的小伙伴们可以参考一下项目源码展示:'
- 本文实例讲述了python冒泡排序简单实现方法。分享给大家供大家参考。具体实现方法如下:#!/usr/bin/pythonimport ra
- 1. 排名函数与PARTITION BY --所有数据 SELECT * FROM dbo.student AS a INNER JOIN
- 一、php中pcntl_fork函数概述pcntl_fork()函数是php中用于创建子进程的一个函数,返回创建的子进程的pid。该函数创建
- DropPath/drop_path 是一种正则化手段,其效果是将深度学习模型中的多分支结构随机”删除&ldqu
- 很多时候,由于程序设计需要,要求在asp的include包含文件里调用动态的文件。如<!--#include file=&q
- 在处理表格型数据时,常会用到排序,比如,按某一行或列的值对表格排序,要怎么做呢?这就要用到 pandas 中的 sort_values()
- 他们是如何不让我的Teleport和Webzip工作的?你也可以做得到哦: <%dim UserAgentUser
- 内容摘要:本文介绍了使用asp来JMail v4.3发信的大部分常用方法,包括邮件基本信息、身份验证、附件等。无需很多的修改就可以
- 本文实例讲述了python协程用法。分享给大家供大家参考。具体如下:把函数编写为一个任务,从而能处理发送给他的一系列输入,这种函数称为协程d
- 目录jiaba库的使用1、jieba库的安装2、统计荷塘月色词频总结jiaba库的使用jieba库是一款优秀的 Python 第三方中文分词
- 作为一个诞生已有 30 余年的编程语言,Python 的流行度在近几年迎来了 * 性的增长。为了方便大家能够更好的采用 Python,利用该语
- 本文讲述了python开发之IDEL(Python GUI)的使用方法。分享给大家供大家参考,具体如下:在安装完Python后,我们希望能够
- 由于惯性思维,导致使用for循环修改列表中的值出现问题首次尝试:def make_great(original): for magician