美文网首页我用Pythonpython爬虫Python
Python爬虫学习(十)Requests库探探

Python爬虫学习(十)Requests库探探

作者: 弃用中 | 来源:发表于2017-09-12 15:52 被阅读227次

经过上文Python爬虫学习(九)Requests库初探的尝试,相信大家已经对Requests有了初步的体会,接下来就再体会一下!

代理设置

对于频繁爬取的网站,网站可能会把我我们的IP给禁掉,这时,代理设置是有必要的。

如果需要设置代理,可以通过为请求方法提供proxies参数来配置:

import requests

proxies = {
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}

requests.get("http://example.org", proxies=proxies)
超时处理

我们可以设置请求时间,一旦超过时间就抛出异常:

带参数的请求
  • URL 参数
    对于一些类似这样的请求:http://study.163.com/course/courseMain.htm?courseId=271005

格式如下:
requests.get(url,params={'key1':'value1'})

  • 表单参数提交

  • Content-Type:application/x-www-form-urlencode
    格式如下:
    requests.post(url,data={'key1':'value1'})

  • json参数提交

  • Content-Type:application/json
    格式如下:
    requests.post(url,json={'key1':'value1'})

至于身份认证,自定义Request等等,附上文档地址:http://docs.python-requests.org/zh_CN/latest/

接下来,燥起来吧!

以上。

相关文章

网友评论

    本文标题:Python爬虫学习(十)Requests库探探

    本文链接:https://www.haomeiwen.com/subject/ezkcsxtx.html