python爬虫学习第八天

今天学习完了requests.get(),requests.post()以及requests的其他一些基础功能
大体内容就是给get/post请求添加头信息,以及验证返回结果的状态(200/404)

练习8 不传递头信息,就不能正常请求某些网页(实际上是许多网页)

# import requests

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

练习9 传递头信息,正常获取了网页内容

# import requests

# 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)

练习10 post方法

# import requests

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

练习11 r的其他属性

# 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)

练习12 requests.codes

# import requests

# r = requests.get('http://www.jianshu.com')
# exit() if not r.status_code==requests.codes.ok else print('successful!')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值