基于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


猜你喜欢
- 先上一张效果图:以前用angularjs操作基本上都是要取到每个列表的id再循环判断是不是当前点击的列表来显示折叠。今天在这个项目 http
- 一、什么是jieba库jieba是优秀的中文分词第三方库,由于中文文本之间每个汉字都是连续书写的,我们需要通过特定的手段来获得其中的每个词组
- 这篇文章主要介绍了Python多线程获取返回值代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- CentOS7服务器中apache、php7以及mysql5.7的配置代码如下所示:yum upgradeyum install net-t
- PS: 我的检索是在文章模块下 forum/article第一步:先安装需要的包:pip install django-haystackpi
- 前面是分部讲解,完整代码在最后。导入模块 :import osfrom shutil import copy, rmtreeimport r
- 1. SyntaxTIMESTAMPDIFF(unit,begin,end); 根据单位返回时间差,对于传入的begi
- 从基础的层面来讲,理解JavaScript的定时器是如何工作的是非常重要的。计时器的执行常常和我们的直观想象不同,那是因为JavaScrip
- 1.安装pyserialhttps://pypi.python.org/pypi/pyserialDoc:http://pythonhost
- 项目信号处理和提取部分用到了matlab,需要应用到工程中方便研究。用具有万能粘合剂之称的“Python”。具体方法如下:1.python中
- 1、开发环境运行项目python mange.py runserver 0.0.0.0:80002、使用gunicorn在生产环境部署Gun
- 在前面的博文中,我们介绍了如何通过软件模拟实现共享磁盘(https://www.jb51.net/network/592807.html),
- 前言最近写的项目中用到了JWT鉴权,因此做个记录原先的jwt-go仓库已经不再维护,迁移到了github.com/golang-jwt/jw
- 【名称】Abs【类别】数学函数【原形】Abs(number)【参数】必选的。Number参数是一个任何有效的数值型表达式【返回值】同numb
- 在安装SQL Server 2005 时出现ASP.Net版本注册要求(警告),提示找不到ASP.Net 在 Microsoft Inter
- 本文实例讲述了Python3写入文件常用方法。分享给大家供大家参考。具体如下:''''' Creat
- 一、管理数据库连接1、使用配置文件管理连接之约定在数据库上下文类中,如果我们只继承了无参数的DbContext,并且在配置文件中创建了和数据
- 首先我们知道python通过pip安装,那么我们安装的所有东西都会在我们pip下的配置目录下,这会显得非常臃肿,同样也让我们部署艰难。pyt
- 一. vue-amap,一个基于 Vue 2.x 和高德地图的地图组件 https://elemefe.github.io/vue-amap
- 前言给新的环境安装pip install tensorflow,结果报错了。跟着我分析解决一波。报错原因这个红字已经说的很清楚了。ERROR