python爬虫 requests基本用法

一些基本用法,ssl证书和身份认证是尚未验证的 

import requests

headers = {
    'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
                   ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
    # 'Cookie' : 'XXXXXXXXXXXXXXXX'
}

# 取得图片
# res = requests.get("https://pic2.zhimg.com/d3dd87a0feae0a3db82973157eee89c0_m.jpg", headers=headers)
# with open("1.jpg", "wb") as pic:
#     pic.write(res.content)
# print(res.content)

# 测试cookies,知乎取不到cookies,其他网站可以
# res = requests.get("https://www.zhihu.com", headers=headers)
# print(res.cookies)
# for key, value in res.cookies.items() :
#     print(key+"="+value)

# 测试session, 两次的cookies值是一致的,证明维持了同一个会话
# session = requests.Session()
# res1 = session.get("http://httpbin.org/cookies/set/number/1234567")
# print(res1.text)
# res2 = session.get("http://httpbin.org/cookies")
# print(res2.text)

# 测试ssl证书, 没有找到合适的网站,12306的证书已经被修改好了
# session = requests.Session()
# res = session.get("https://www.ccb.com/cn/home/indexv3.html/", verify=False)
# print(res.status_code)

# 测试超时设置
# session = requests.Session()
# 如果你设置了一个单一的值作为 timeout,这一timeout值将会用作connect和read二者的总和timeout:
#res = session.get("https://www.baidu.com", timeout=1)
#如果要分别指定,就传入一个元组:
# res = session.get("https://www.baidu.com", timeout=(0.1, 3))
# print(res.status_code)

# 身份认证,这种简单的方法无效,总是返回 status_code=200
# res = requests.get("https://passport.csdn.net/login?code=public", auth=('17671766710', 'ylbs4342017'))
# print(res.status_code)

 

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