| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- # -*- coding: utf-8 -*-
- """
- -------------------------------------------------
- File Name: utilFunction.py
- Description : tool function
- Author : JHao
- date: 2016/11/25
- -------------------------------------------------
- Change Activity:
- 2016/11/25: 添加robustCrawl、verifyProxy、getHtmlTree
- -------------------------------------------------
- """
- # noinspection PyPep8Naming
- def robustCrawl(func):
- def decorate(*args, **kwargs):
- try:
- return func(*args, **kwargs)
- except Exception as e:
- print u"sorry, 抓取出错。错误原因:"
- print e
- return decorate
- def verifyProxy(proxy):
- """
- 检查代理格式
- :param proxy:
- :return:
- """
- import re
- verify_regex = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,4}"
- return True if re.findall(verify_regex, proxy) else False
- def getHtmlTree(url, **kwargs):
- """
- 获取html树
- :param url:
- :param kwargs:
- :return:
- """
- import requests
- from lxml import etree
- html = requests.get(url=url).content
- return etree.HTML(html)
|