requests

import requests
response = requests.get('https://www.baidu.com/')
print(type(response))
print(response.status_code)
print(type(response.text))
print(response.text)
print(response.cookies)
get请求

import requests
params = {
    'name': 'germey'
}
response = requests.get('http://httpbin.org/get', params=params)
print(response.text)

json解析

import requests
import json
params = {
    'name': 'germey'
}
response = requests.get('http://httpbin.org/get', params=params)
print(type(response.text))
print(response.text)
print(response.json())
print(json.loads(response.text))
print(type(response.json()))

{"args":{"name":"germey"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.4"},"origin":"1.198.6.194","url":"http://httpbin.org/get?name=germey"}
{'args': {'name': 'germey'}, 'headers': {'User-Agent': 'python-requests/2.18.4', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'Accept': '*/*', 'Connection': 'close'}, 'url': 'http://httpbin.org/get?name=germey', 'origin': '1.198.6.194'}
{'args': {'name': 'germey'}, 'headers': {'User-Agent': 'python-requests/2.18.4', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'Accept': '*/*', 'Connection': 'close'}, 'url': 'http://httpbin.org/get?name=germey', 'origin': '1.198.6.194'}

获取二进制参数

import requests
response = requests.get('https://github.com/favicon.ico')
print(type(response.text), type(response.content))
print(response.text)
print(response.content)

:�������O                               
b'\x00\x00\x01\x00\x02\x00\x10\x10\x00\x00\x01\x00 \x00(\x05\x00\x00&\x00\x00\x00  \x00\x00\x01\x00 

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

Post请求

import requests
data = {'name':'germey', 'age': '22'}
response = requests.post('http://httpbin.org/post', data=data)
print(response.text)

响应

import requests
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)
403
{'Date': 'Wed, 30 May 2018 08:11:08 GMT', 'X-Dscp-Value': '0', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'Content-Type': 'text/html', 'Connection': 'keep-alive', 'Server': 'Tengine', 'Content-Encoding': 'gzip', 'X-Via': '1.1 PSfjfzdx2mj93:9 (Cdn Cache Server V2.0), 1.1 jiangwzhoudxin14:10 (Cdn Cache Server V2.0)', 'Transfer-Encoding': 'chunked'}

https://www.jianshu.com/

[]

文件上传

import requests
files = {'file': open('favicon.ico','rb')}
response = requests.post('http://httpbin.org/post', files=files)
print(response.text)
{"args":{},"data":"","files":{"file":"data:application/octet-stream;base64,AAABAAIAEBAAAAEAIAAoBQAAJgAAACAgAAABACA

获取cookie

import requests
response = requests.get('https://www.baidu.com')
print(response.cookies)
for key, value in response.cookies.items():
    print(key, value, sep='=')
]>

BDORZ=27315

会话维持

import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456789')
response = s.get('http://httpbin.org/cookies')
print(response.text)
{"cookies":{"number":"123456789"}}

证书验证

import requests
import urllib3
urllib3.disable_warnings()
response = requests.get('https://www.12306.cn', verify=False)
print(response.status_code)

200

代理设置

import requests
proxies = {
    'http': 'http://127.0.0.1:9743',
    'https': 'https://127.0.0.1:9743'
}
response = requests.get('https://www.taobao.com', proxies=proxies)
print(response.status_code)

你可能感兴趣的:(python网络爬虫)