如何使用requests库在Python爬虫中设置代理?

2026-04-30 18:411阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计548个文字,预计阅读时间需要3分钟。

如何使用requests库在Python爬虫中设置代理?

目录+ 安装requests库+ GET方法+ 谷歌浏览器的开发者工具+ POST方法+ 使用代理+ 在阅读本文之前,需要掌握以下知识技能: + Python基础 + HTML基础 + HTTP状态码

目录
  • 安装上requests库
  • GET方法
  • 谷歌浏览器的开发者工具
  • POST方法
  • 使用代理

在看这篇文章之前,需要大家掌握的知识技能:

  • python基础
  • html基础
  • www.python.org') >>> r.status_code 200 >>> 'Python is a programming language' in r.content True ... or POST: >>> payload = dict(key1='value1', key2='value2') >>> r = requests.post('python-requests.org>. :copyright: (c) 2017 by Kenneth Reitz. :license: Apache 2.0, see LICENSE for more details.

    这里解释下,requests库是由python编写的对人类友好的www.baidu.com') print(r.status_code) #打印返回的www.baidu.com', headers=headers) print(r.status_code) print(r.text)

    嗯~~~数据有点多,往下翻翻,这下就正常了嘛,数据都有了。。。PS:不信?可以自己输出一个html文件,浏览器打开看看呗

    如何使用requests库在Python爬虫中设置代理?

    POST方法

    只需要把get改成post就好了

    import requests headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'} r = requests.post('www.baidu.com', headers=headers) print(r.status_code) print(r.text)

    运行下试试看。一般post都是用来提交表单信息的,嗯,这里找一个能提交数据的url,去post下。

    用我自己写的接口(PS:django写的,挺方便),大家复制过去就好了。注意看代码,data是要post的数据,post方法里加了一个data参数。

    import requests headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'} # post的数据 data = {"info": "biu~~~ send post request"} r = requests.post('dev.kdlapi.com/testproxy', headers=headers, data=data) #加一个data参数 print(r.status_code) print(r.text)

    截个图给大家看下,%(proxy)s/" % {'proxy': proxy}, "%(proxy)s/" % {'proxy': proxy} } r = requests.post('dev.kdlapi.com/testproxy', headers=headers, data=data, proxies=proxies) #加一个proxies参数 print(r.status_code) print(r.text)

    主要方法里加个proxies参数,这就用上代理IP了。

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

本文共计548个文字,预计阅读时间需要3分钟。

如何使用requests库在Python爬虫中设置代理?

目录+ 安装requests库+ GET方法+ 谷歌浏览器的开发者工具+ POST方法+ 使用代理+ 在阅读本文之前,需要掌握以下知识技能: + Python基础 + HTML基础 + HTTP状态码

目录
  • 安装上requests库
  • GET方法
  • 谷歌浏览器的开发者工具
  • POST方法
  • 使用代理

在看这篇文章之前,需要大家掌握的知识技能:

  • python基础
  • html基础
  • www.python.org') >>> r.status_code 200 >>> 'Python is a programming language' in r.content True ... or POST: >>> payload = dict(key1='value1', key2='value2') >>> r = requests.post('python-requests.org>. :copyright: (c) 2017 by Kenneth Reitz. :license: Apache 2.0, see LICENSE for more details.

    这里解释下,requests库是由python编写的对人类友好的www.baidu.com') print(r.status_code) #打印返回的www.baidu.com', headers=headers) print(r.status_code) print(r.text)

    嗯~~~数据有点多,往下翻翻,这下就正常了嘛,数据都有了。。。PS:不信?可以自己输出一个html文件,浏览器打开看看呗

    如何使用requests库在Python爬虫中设置代理?

    POST方法

    只需要把get改成post就好了

    import requests headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'} r = requests.post('www.baidu.com', headers=headers) print(r.status_code) print(r.text)

    运行下试试看。一般post都是用来提交表单信息的,嗯,这里找一个能提交数据的url,去post下。

    用我自己写的接口(PS:django写的,挺方便),大家复制过去就好了。注意看代码,data是要post的数据,post方法里加了一个data参数。

    import requests headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'} # post的数据 data = {"info": "biu~~~ send post request"} r = requests.post('dev.kdlapi.com/testproxy', headers=headers, data=data) #加一个data参数 print(r.status_code) print(r.text)

    截个图给大家看下,%(proxy)s/" % {'proxy': proxy}, "%(proxy)s/" % {'proxy': proxy} } r = requests.post('dev.kdlapi.com/testproxy', headers=headers, data=data, proxies=proxies) #加一个proxies参数 print(r.status_code) print(r.text)

    主要方法里加个proxies参数,这就用上代理IP了。

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。