python初步爬虫之requests

对于初步学习python爬虫,我最先接触的是requests库
这是一个python的第三方库,下面我就简单介绍一下requests的安装和使用。

requests安装:win+r打开运行窗口,输入cmd,在cmd窗口中直接pip install requests
python初步爬虫之requests
requests使用:requests最常用的方法是requests.get(),这样就能通过url获得我们想要的信息。
简单试验一下
打开python的shell界面导入requests库,import requests
python初步爬虫之requests
导入requests之后我们使用requests.get()方法,在requests.get()中放入url
这里以百度为例http://www.baidu.com
导入url之后想知道是否访问成功了,用r.status_code查看状态码,如果是200表示正常,404或者其他都表示异常。
python初步爬虫之requests
最后用r.text查看获取的内容
python初步爬虫之requests
第一次写博客,很多地方不是很详细,希望大家谅解。