使用requests(上)

3.2-使用requests

test_01.py

import requests  

r = requests.get('https://www.baidu.com/')  
print(type(r))  # <class 'requests.models.Response'>
print(r.status_code)  # 200
print(type(r.text))  # <class 'str'>  
print(r.text)  
print(r.cookies)  # <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>


# 其余请求类型的使用
r = requests.post('http://httpbin.org/post')  
r = requests.put('http://httpbin.org/put')  
r = requests.delete('http://httpbin.org/delete')  
r = requests.head('http://httpbin.org/get')  
# print(r.headers)  
r = requests.options('http://httpbin.org/get')

test_02.py

import requests  

r = requests.get('http://httpbin.org/get')  
print(r.text)
r"""
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.24.0", 
    "X-Amzn-Trace-Id": "Root=1-6103b24e-7336e8ca7bbecdad06161609"
  },
  "origin": "124.160.154.45",
  "url": "http://httpbin.org/get"
}
"""

test_03.py

import requests  

data = {  
    'name': 'germey',  
    'age': 22  
}  
# r = requests.get("http://httpbin.org/get", params=data)  
r = requests.get('http://httpbin.org/get?name=germey&age=22')
print(r.text)

r"""
{
  "args": {
    "age": "22", 
    "name": "germey"
  }, 
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.24.0",
    "X-Amzn-Trace-Id": "Root=1-6103b3b8-65814dc91fc64c8403bce346"
  },
  "origin": "124.160.154.45",
  "url": "http://httpbin.org/get?name=germey&age=22"
}
"""



r"""
{
  "args": {
    "age": "22", 
    "name": "germey"
  }, 
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.24.0",
    "X-Amzn-Trace-Id": "Root=1-6103b3d9-3b0b54527368d3255b4f1174"
  },
  "origin": "124.160.154.45",
  "url": "http://httpbin.org/get?name=germey&age=22"
}
"""

test_04.py

import requests  

r = requests.get("http://httpbin.org/get")  
print(type(r.text))  # <class 'str'>
print(r.json())  
print(type(r.json()))  # <class 'dict'>

test_05.py

import requests
import re

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
}
r = requests.get("https://www.zhihu.com/explore", headers=headers)
pattern = re.compile('explore-feed.*?question_link.*?>(.*?)</a>', re.S)
titles = re.findall(pattern, r.text)
print(titles)

test_06.py

import requests

r = requests.get("https://github.com/favicon.ico")
print(r.text)
print(r.content)

test_07.py

import requests

r = requests.get("https://github.com/favicon.ico")
with open('favicon.ico', 'wb') as f:
    f.write(r.content)

test_08.py

import requests

r = requests.get("https://www.zhihu.com/explore")
print(r.text)

print('--'*10)

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
r = requests.get("https://www.zhihu.com/explore", headers=headers)
print(r.text)

test_09.py

import requests

data = {'name': 'germey', 'age': '22'}
r = requests.post("http://httpbin.org/post", data=data)
print(r.text)
# print(r.content)

test_10.py

import requests

r = requests.get('http://www.jianshu.com')
print(type(r.status_code), r.status_code)
print(type(r.headers), r.headers)
print(type(r.cookies), r.cookies)
print(type(r.url), r.url)
print(type(r.history), r.history)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值