Python爬虫学习纪要(十):Requests 库学习笔记5

来源:互联网 发布:mac mini重装系统教程 编辑:程序博客网 时间:2024/06/06 19:41
一、reqeusts.get:
def get(url, params=None, **kwargs)
其中**kwargs:十二个控制访问参数,均为可选项
1)params:字典或者字节序列,作为参数增加到url中
2)data:字典、字节序列或者文件对象,作为Request的内容json:JSON格式的数据,作为Request的内容
3)headers:字典,HTTP定制头
4)cookies:字典或Cookiejar, Request中的cookie
5)auth:元组,支持HTTP认证功能
6)files:字典类型,传输文件
7)timeout:设定超时时间,单位秒
8)proxies:字典类型,设定访问代理服务器,可以增加登录认证
9)allow_redirects:Ture/False,默认为True,重定向开关
10)stream:True/False,默认为True,获取内容立即下载开关
11)verify:True/False,默认为True,认证SSL证书开关
12)cert:本地SSL证书路径
13)url:拟更新页面的url链接
14)data:字典、字节序列或文件,Request的内容
15)json:JSON格式的数据,Request的内容

二、传输RUL参数
例·1:
import requests

test = {'key1':'value1', 'key2':'value2'}
r = requests.get('http://www.baidu.com', params=test)
print(r.url)

输出:
http://www.baidu.com/?key1=value1&key2=value2

例·2:
import requests

test = {'key1':'value1', 'key2':['value2', 'value3']}
r = requests.get('http://www.baidu.com', params=test)
print(r.url)

输出:
http://www.baidu.com/?key1=value1&key2=value2&key2=value3

三、响应内容
响应内容(r.text)、二进制响应内容(r.content)、JSON响应内容(r.json)区别:
import requests

r = requests.get('https://github.com/timeline.json')
print(r.text)
print('==========')
print(r.content)
print('==========')
print(r.json)

输出:
{"message":"Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}
==========
b'{"message":"Hello there, wayfaring stranger. If you\xe2\x80\x99re reading this then you probably didn\xe2\x80\x99t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}'
==========
<bound method Response.json of <Response [410]>>
阅读全文
1 0
原创粉丝点击