美文网首页
4、Python requests库,简单调用

4、Python requests库,简单调用

作者: 波罗的海de夏天 | 来源:发表于2019-03-08 14:46 被阅读0次
'''
requests库,简单调用
'''

import requests
from bs4 import BeautifulSoup

1、简单调用

response = requests.get('http://httpbin.org/')
print(response)
print(type(response))
print(response.status_code)
print(response.text)
print(type(response.text))
print(response.cookies)

2、get请求

print('0 ', '---'*20)
response = requests.get('http://httpbin.org/get')
print(response.text)
# 方法一:路由
print('1 ', '---'*20)
response = requests.get('http://httpbin.org/get?name=germey&age=22')
print(response.text)
# 方法二:参数字典
print('2 ', '---'*20)
data = {'name':'jermey', 'age':22}
response = requests.get('http://httpbin.org/get', params=data)
print(response.text)

3、添加headers

print('1 ', '---'*20)
response = requests.get('http://www.zhihu.com/explore')
print(response.text)
print('2 ', '---'*20)
headers = {'user-agent':'Mozilla/5.0(windows NT 10.0;win64;X64)AppleWebkit/537.36(KHTML,like Gecko)chrome/70.0.3538.102 Safari/537.36'}
response = requests.get('http://www.zhihu.com/explore', headers=headers)
print(response.text)

4、post请求

data = {'name':'jermey', 'age':22}
response = requests.post('http://httpbin.org/post', data=data)
print(response.text)
# post请求 -- 添加headers
data = {'name':'jermey', 'age':22}
headers = {'user-Agent':"Mozilla/5.0(windows NT 10.0;win64;X64)AppleWebkit/537.36(KHTML,like Gecko)chrome/70.0.3538.102 Safari/537.36"}
response = requests.post('http://httpbin.org/post', data=data, headers=headers)
print(response.text)

5、cookies

response = requests.get('https://www.baidu.com/')
print(response.cookies)
for key, value in response.cookies.items():
    print(key + '=' + value)
# 案例
headers = {'user-Agent':"Mozilla/5.0(windows NT 10.0;win64;X64)AppleWebkit/537.36(KHTML,like Gecko)chrome/70.0.3538.102 Safari/537.36"}
requests.get('http://httpbin.org/cookies/set/number/24680', headers=headers)
response = requests.get('http://httpbin.org/cookies', headers=headers)
print(response.text)
'''
运行结果:cookies是一个空的状态。
原因:这里发起的两次请求,都是用requests.get发起的。两次请求实际上是两次完全独立的过程。
可以理解为用一个浏览器设置了一个cookies,用另外一个浏览器访问一个cookies页面。也就是说在两个页面分别操作的,是获取不到任何cookies信息的。
需要用Session解决。
'''
# Session方法
sess = requests.Session()
sess.get('http://httpbin.org/cookies/set/number/24680')
response = sess.get('http://httpbin.org/cookies')
print(response.text)

6、网络请求 -- 异常处理

'''
异常会导致程序中断。
写爬虫的时候异常处理模块非常有必要的,可以保证程序一直不间断的运行。
先捕捉子类的异常,再捕捉父类的异常。
'''
from requests.exceptions import ReadTimeout, ConnectionError, RequestException
try:
    response = requests.get('http://httpbin.org/get', timeout=0.4)  # timeout:超时设置
    print(response.status_code)
except ReadTimeout:
    # 超时报错
    print('Timeout')
except ConnectionError:
    # 连接报错
    print('Connection error')
except RequestException:
    # 请求报错
    print('Error')

7、response属性

'''
status_code
headers
cookies
url
history
'''
response = requests.get('http://www.jianshu.com')
print(type(response.status_code), response.status_code)
print(type(response.headers), response.headers)
print(type(response.cookies), response.cookies)
print(type(response.url), response.url)
print(type(response.history), response.history)

相关文章

网友评论

      本文标题:4、Python requests库,简单调用

      本文链接:https://www.haomeiwen.com/subject/wnyrpqtx.html