爬虫学习——(一)Request库请求网页

目录

一、requests库常见方式请求网页

1.GET请求

基本实例

添加请求头

百度网页http请求实战

抓取网页数据

抓取二进制数据

2.POST请求

3.响应


在使用之前,确保安装requests库,可以在任务栏cmd里使用pip3来安装:

pip3 install requests

一、requests库常见方式请求网页

import requests

r = requests.get('https://www.httpbin.org/get')
r = requests.post('https://www.httpbin.org/post')
r = requests.put('https://www.httpbin.org/put')
r = requests.delete('https://www.httpbin.org/delete')
r = requests.patch('https://www.httpbin.org/patch')

1.GET请求

  • 基本实例

import requests

r = requests.get('https://www.httpbin.org/get')
print(r.text)

运行结果:

{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate, br", 
    "Host": "www.httpbin.org", 
    "User-Agent": "python-requests/2.27.1", 
    "X-Amzn-Trace-Id": "Root=1-62d27f11

你可能感兴趣的:(爬虫,学习,python)