aiohttp
发起请求
async def fetch():
async with aiohttp.ClientSession() as session:
async with session.get('https://www.baidu.com') as resposne:
print(await resposne.text())
loop = asyncio.get_event_loop()
tasks = [fetch(),]
loop.run_until_complete(asyncio.wait(tasks))
添加请求参数
params = {'key': 'value', 'page': 10}
async def fetch():
async with aiohttp.ClientSession() as session:
async with session.get('https://www.baidu.com/s',params=params) as resposne:
print(await resposne.url)
loop = asyncio.get_event_loop()
tasks = [fetch(),]
loop.run_until_complete(asyncio.wait(tasks))
自定义User-Agent
url = 'http://httpbin.org/user-agent'
headers = {'User-Agent': 'test_user_agent'}
async def fetch():
async with aiohttp.ClientSession() as session:
async with session.get(url,headers=headers) as resposne:
print(await resposne.text())
loop = asyncio.get_event_loop()
tasks = [fetch(),]
loop.run_until_complete(asyncio.wait(tasks))
自定义cookies
url = 'http://httpbin.org/cookies'
cookies = {'cookies_name': 'test_cookies'}
async def fetch():
async with aiohttp.ClientSession() as session:
async with session.get(url,cookies=cookies) as resposne:
print(await resposne.text())
loop = asyncio.get_event_loop()
tasks = [fetch(),]
loop.run_until_complete(asyncio.wait(tasks))
post字符串
url = 'http://httpbin.org'
payload = {'username': 'zhang', 'password': '123456'}
async def fetch():
async with aiohttp.ClientSession() as session:
async with session.post(url, data=payload) as resposne:
print(await resposne.text())
loop = asyncio.get_event_loop()
tasks = [fetch(), ]
loop.run_until_complete(asyncio.wait(tasks))
post文件
url = 'http://httpbin.org'
files = {'file': open('test.txt', 'rb')}
async def fetch():
async with aiohttp.ClientSession() as session:
async with session.post(url, data=files) as resposne:
print(await resposne.text())
loop = asyncio.get_event_loop()
tasks = [fetch(), ]
loop.run_until_complete(asyncio.wait(tasks))
设置代理
url = "http://python.org"
async def fetch():
async with aiohttp.ClientSession() as session:
async with session.get(url, proxy="http://some.proxy.com") as resposne:
print(resposne.status)
loop = asyncio.get_event_loop()
tasks = [fetch(), ]
loop.run_until_complete(asyncio.wait(tasks))
设置认证代理
url = "http://python.org"
async def fetch():
async with aiohttp.ClientSession() as session:
proxy_auth = aiohttp.BasicAuth('user', 'pass')
async with session.get(url, proxy="http://some.proxy.com", proxy_auth=proxy_auth) as resposne:
print(response.status)
loop = asyncio.get_event_loop()
tasks = [fetch(), ]
loop.run_until_complete(asyncio.wait(tasks))
# 下面的方法也可以
url = "http://python.org"
async def fetch():
async with aiohttp.ClientSession() as session:
async with session.get(url, proxy="http://user:pass@some.proxy.com") as response:
print(response.status)
loop = asyncio.get_event_loop()
tasks = [fetch(), ]
loop.run_until_complete(asyncio.wait(tasks))
aiohttp的更多相关文章
- 【转】aiohttp 源码解析之 request 的处理过程
[转自 太阳尚远的博客:http://blog.yeqianfeng.me/2016/04/01/python-yield-expression/] 使用过 python 的 aiohttp 第三方库 ...
- aiohttp使用队列
获取百度的搜索结果,然后把百度的长链接,获取到真实的url import time import aiofiles import aiohttp import asyncio from lxml im ...
- aiohttp AppRunner的用法
参考廖雪峰的aiohttp教程,会出现两个DeprecationWarning, loop argument is deprecated Application.make_handler(...) i ...
- python requests与aiohttp 速度对比
环境:centos7 python3.6 测试网址:www.bai.com 测试方式:抓取百度100次 结果: aio: 10.702147483825684srequests: 12.4046785 ...
- 利用aiohttp制作异步爬虫
asyncio可以实现单线程并发IO操作,是Python中常用的异步处理模块.关于asyncio模块的介绍,笔者会在后续的文章中加以介绍,本文将会讲述一个基于asyncio实现的HTTP框架--a ...
- aiohttp的笔记之TCPConnector
TCPConnector维持链接池,限制并行连接的总量,当池满了,有请求退出再加入新请求.默认是100,limit=0的时候是无限制 1.use_dns_cache: 使用内部DNS映射缓存用以查询D ...
- Python中syncio和aiohttp
CPython 解释器本身就不是线程安全的,因此有全局解释器锁(GIL),一次只允许使用一个线程执行 Python 字节码.因此,一个 Python 进程通常不能同时使用多个 CPU 核心.然而,标准 ...
- aiohttp文档翻译-server(一)
web server 快速入门 运行一个简单的web server 为了实现web server, 首先需要实现request handler 一个 request handler 必须是一个coro ...
- python链家网高并发异步爬虫asyncio+aiohttp+aiomysql异步存入数据
python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...
- aiohttp分流处理
# -*- coding: utf-8 -*- # @Time : 2018/12/26 9:55 PM # @Author : cxa # @Software: PyCharm import asy ...
随机推荐
- 提高效率!15款最好的 Bug 跟踪应用程序
当涉及到开发项目时,其中比较重要的事情是它需要某种形式的错误和问题跟踪工具来发现和解决问题,否则会浪费大量的时间. 此外,你总是要标签应用来标示那些悬而未决的问题,而这种分期执行的项目进度将帮助您 ...
- 最常用的8款 PHP 调试工具,你用过吗?
Web 开发并不是一项轻松的任务,有超级多服务端脚本语言提供给开发者,但是当前 PHP 因为具有额外的一些强大的功能而越来越流行.PHP 是最强大的服务端脚本语言之一,同时也是 Web 开发者和设计者 ...
- 我的Mac使用笔记
此篇记录mac使用相关的一些东西,不断更新中... 1.Mac 安装homebrew : https://brew.sh/index_zh-cn.html /usr/bin/ruby -e " ...
- 41、用Python实现一个二分查找的函数
data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35] def binary_search(dataset ...
- HTML表单属性与全局属性
1.全局属性
- 【appium】根据UIAutomator定位元素等等资料
https://www.cnblogs.com/paulwinflo/p/4742529.html http://www.cnblogs.com/meitian/p/6103391.html http ...
- Serv-U设置允许用户更改密码【转】
最近,公司上了一套Serv-U10.5.0.6的ftp软件,应该是目前最新的版本了.上的第一天就遇到了一个问题,有领导发话了,他需要自己更改密码.找了N久才找到,分享一下. 点击管理界面的用户. 进入 ...
- sicily 1259. Sum of Consecutive Primes
Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...
- 利用json模块解析dict报错找不到attribute 'dumps'[python2.7]
[背景] 环境: RHEL 7.3 版本: python2.7 [错误情况] 写了一个简单的python脚本 将dict转换为json 脚本如下: #!/usr/bin/python #-*- cod ...
- 使用常见的网络命令查看当前网络状态——Mac OS X篇
转载自:http://blog.csdn.net/zkh90644/article/details/50539948 操作系统拥有一套通用的实用程序来查明本地主机的有线或者无线链路状态和IP的连接情况 ...