Python爬虫入门之request函数定制
来源:互联网 发布:淘宝buy 宣传片 编辑:程序博客网 时间:2024/05/11 04:30
爬虫入门 - 定制合适的请求函数
(以下代码仅供参考和学习使用,勿非法使用,thx)
#coding=utf-8import timeimport randomfrom requests.exceptions import *from requests import request'''example:options = { 'method':'get', 'url':'http://www.eprc.com.hk/EprcWeb/multi/transaction/login.do', 'form':None, 'params':None, 'cookies':None, 'headers':headers,}response = basicRequest(options)'''def basicRequest(options,resend_times=3): ''' :param options: 请求参数 :param resend_times: 重发次数 :return: response对象或False ''' # proxies = {'http':'http://127.0.0.1:8888','https':'http://127.0.0.1:8888'} options['timeout'] = options['timeout'] if 'timeout' in options.keys() else 3 try : response = request( options['method'], options['url'], verify = True, timeout = options['timeout'], # proxies = proxies, data = options['form'], params = options['params'], cookies = options['cookies'], headers = options['headers'] ) except Timeout : if resend_times > 0 : time.sleep(random.uniform(0,2)) options['timeout'] += 3 return basicRequest(options, resend_times-1) else: return False except RequestException : if resend_times > 0 : time.sleep(random.uniform(0,2)) return basicRequest(options, resend_times-1) else : return False else : return response
afanty 的分析:
对request的函数进行了函数包装,参数都是一个demo,这样便于开发,同时对异常的处理可以自己来指定。
0 0
- Python爬虫入门之request函数定制
- python爬虫基础之request设置问题
- python爬虫之Request,BeautifulSoup进阶
- python爬虫之BeautifulSoup入门
- Python爬虫open定制
- python爬虫之urllib.request和cookie登录CSDN
- Python爬虫入门一之综述
- Python爬虫入门七之正则表达式
- python基础入门之简单爬虫编写
- Python入门网络爬虫之精华版
- Python爬虫入门一之综述
- Python爬虫入门七之正则表达式
- Python爬虫入门一之综述
- Python爬虫入门七之正则表达式
- Python爬虫大杀器之Requests快速入门
- python爬虫入门之爬取贴吧标题
- Python爬虫入门七之正则表达式
- Python爬虫入门七之正则表达式
- LinearLayout(二)
- 构造函数和析构函数
- windows下安装apache并配置ssl过程
- 6款职场发型,造就你的气质不凡!
- g++生成共享库之参数fPIC
- Python爬虫入门之request函数定制
- 一个黑客写的黑客教程
- 教你用MSChart控件绘制正态分布图形
- MFC 打开保存文件对话框
- C8051F系列单片机相关
- php字符串处理函数-->strpor()
- Fragment两种加载
- CSS-合理使用z-index控制盒子视轴高度,解决z-index失效
- 【机房收费个人重构—思想篇】