一、requests模块介绍
???????Python内置的urllib模块,用于访问网络资源。但是,它用起来比较麻烦,而且,缺少很多实用的高级功能。因此requests模块在python内置模块的基础上进行了高度的封装,从而使得python进行网络请求时,变得更加简洁和人性化。
官方模块中说明:
"""
Requests HTTP Library
~~~~~~~~~~~~~~~~~~~~~
Requests is an HTTP library, written in Python, for human beings.
Basic GET usage:
>>> import requests
>>> r = requests.get('https://www.python.org')
>>> r.status_code
200
>>> b'Python is a programming language' in r.content
True
... or POST:
>>> payload = dict(key1='value1', key2='value2')
>>> r = requests.post('https://httpbin.org/post', data=payload)
>>> print(r.text)
{
...
"form": {
"key1": "value1",
"key2": "value2"
},
...
}
The other HTTP methods are supported - see `requests.api`. Full documentation
is at <https://requests.readthedocs.io>.
:copyright: (c) 2017 by Kenneth Reitz.
:license: Apache 2.0, see LICENSE for more details.
"""
二、使用步骤
import requests
def get_method_simple_with_requests_module():
url = "https://so.gushiwen.cn/mingjus/"
reponse_obj = requests.get(url, timeout=1)
print(reponse_obj.status_code)
print(reponse_obj.headers)
print(reponse_obj.url)
print(reponse_obj.history)
print(reponse_obj.encoding)
print(reponse_obj.reason)
print(reponse_obj.cookies)
print(reponse_obj.elapsed)
print(reponse_obj.request.method)
print(reponse_obj.request.url)
print(reponse_obj.request.headers)
print(reponse_obj.request.body)
print(reponse_obj.request.hooks)
from requests.models import PreparedRequest
print(reponse_obj.ok)
print(reponse_obj.is_redirect)
print(reponse_obj.is_permanent_redirect)
print(reponse_obj.next)
print(reponse_obj.apparent_encoding)
def get_method_whole_with_requests_module():
url = "https://so.gushiwen.cn/mingjus/default.aspx"
params_set = {'page': 4, 'tstr': '边塞', 'astr': '李白', 'cstr': '唐代', 'xstr': '诗文'}
headers_set = {
'Content-Type': 'text/html;charset=utf-8',
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
}
response_obj = requests.get(url, params=params_set, headers=headers_set)
print(response_obj.url)
print(response_obj.content)
def post_method_whole_with_requests_module():
url='https://fanyi.baidu.com/sug'
data_res = {'kw':'hi',}
headers_set = {
'Content-Type': 'text/html;charset=utf-8',
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
}
response_obj = requests.post(url, data=data_res, headers=headers_set)
print(response_obj.status_code)
print(response_obj.content)
if __name__ == '__main__':
get_method_simple_with_requests_module()
get_method_whole_with_requests_module()
post_method_whole_with_requests_module()
from requests import api
总结
提示:这里对文章进行总结: 例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
|