python requests使用

参考
python爬虫---requests库的用法

基本的get请求:

import requests

response = requests.get('https://movie.douban.com/top250')
print(response.status_code)  # 打印状态码
print(response.url)          # 打印请求url
print(response.headers)      # 打印头信息
print(response.cookies)      # 打印cookie信息
print(response.text)  #以文本形式打印网页源码
print(response.content) #以字节流形式打印

各种请求方式:

import requests

requests.get('http://httpbin.org/get')
requests.post('http://httpbin.org/post')
requests.put('http://httpbin.org/put')
requests.delete('http://httpbin.org/delete')
requests.head('http://httpbin.org/get')
requests.options('http://httpbin.org/get')

带参数、头部、代理

import requests

#头部
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
}

#参数
params = {
    'wd': 123
}

#代理
proxies = {
    'http': 'http://180.118.135.81:9000'
}

response = requests.get('https://www.baidu.com/s', params=params, headers=headers, proxies=proxies)

print(response.status_code)  # 打印状态码
print(response.text)  # 以文本形式打印网页源码

你可能感兴趣的:(python requests使用)