0x01 request.get(post)

import requests

html = requests.get(url="http://49.235.54.135/")
print(html.text)

输出内容源码

html = requests.post('http://httpbin.org/post',data={'name':'yicunyiye','pwd':'hk'})
print(html.text)

data参数post内容

0x02 url传参

payload = {
    'wd':'python'
}
html = requests.get('https://www.baidu.com/s',params=payload)
print(html.url)

输出内容https://www.baidu.com/s?wd=python,text是源代码,content是二进制,图片就要用二进制写入

img = requests.get('https://ns-strategy.cdn.bcebos.com/ns-strategy/upload/fc_big_pic/part-00132-1104.jpg')
with open('img.jpg','wb') as f:
    f.write(img.content)

就可以本地保存图片
这里就用百度图片为例,百度图片都是保存在json里面
python-request_Python编程

可以看到在data里面
python-request_Python开发_02

然后要获取的图片url在thumbURL里面,但是最后一个为空就会抛一个KeyError 错误所以就用try except防错机制

url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%9B%BE%E7%89%87&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=&copyright=&word=%E5%9B%BE%E7%89%87&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&fr=&expermode=&force=&pn=30&rn=30&gsm=1e&1599110152564='
headers = {
    'Referer': 'https://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E5%9B%BE%E7%89%87',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
}

html = requests.get(url,headers=headers)
if html.status_code==200:
    print(html.json()['data'])
    for i in html.json()['data']:
        try:
            print(i['thumbURL'])
        except KeyError as e:
            print('有的字典是空的')
else:
    print('请求出错')

假如说一些网站你必须使用cookies才能访问,然后登录进去了cookies又要更新的情况下,所以就可以用requests.session()然后再session.post()和session.get(),一直在这里请求就可以了,每次也要session.close()来关闭,避免内存泄露,所以也可以使用with requests.session() as s: s.get()操作,退出的时候就可以自动关闭,还有就是ssl证书

import requests
print(requests.get('https://requestb.in').text)

报错requests.exceptions.SSLError: hostname 'requestb.in' doesn't match either of '*.herokuapp.com', 'herokuapp.com',我们就可以把verify改为False,就可以忽略掉证书验证,如果本地有证书就可以把证书路径写在里面
然后就是代理,

import requests
proxies = {
    "http":"http://1.1.1.1:3333",
    "http":"http://2.2.2.2:1111",
}
requests.get("http://xx.com",proxies=proxies)