WebRequest.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. """
  3. -------------------------------------------------
  4. File Name: WebRequest
  5. Description : Network Requests Class
  6. Author : J_hao
  7. date: 2017/7/31
  8. -------------------------------------------------
  9. Change Activity:
  10. 2017/7/31:
  11. -------------------------------------------------
  12. """
  13. __author__ = 'J_hao'
  14. import requests
  15. import random
  16. import time
  17. from requests.models import Response
  18. class WebRequest(object):
  19. def __init__(self, *args, **kwargs):
  20. pass
  21. @property
  22. def user_agent(self):
  23. """
  24. return an User-Agent at random
  25. :return:
  26. """
  27. ua_list = [
  28. 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101',
  29. 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122',
  30. 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71',
  31. 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95',
  32. 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71',
  33. 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)',
  34. 'Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50',
  35. 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
  36. ]
  37. return random.choice(ua_list)
  38. @property
  39. def header(self):
  40. """
  41. basic header
  42. :return:
  43. """
  44. return {'User-Agent': self.user_agent,
  45. 'Accept': '*/*',
  46. 'Connection': 'keep-alive',
  47. 'Accept-Language': 'zh-CN,zh;q=0.8'}
  48. def get(self, url, header=None, retry_time=5, timeout=30,
  49. retry_flag=list(), retry_interval=5, use_proxy=False, *args, **kwargs):
  50. """
  51. get method
  52. :param url: target url
  53. :param header: headers
  54. :param retry_time: retry time when network error
  55. :param timeout: network timeout
  56. :param retry_flag: if retry_flag in content. do retry
  57. :param retry_interval: retry interval(second)
  58. :param use_proxy: 是否使用代理
  59. :param args:
  60. :param kwargs:
  61. :return:
  62. """
  63. headers = self.header
  64. if header and isinstance(header, dict):
  65. headers.update(header)
  66. while True:
  67. try:
  68. if use_proxy:
  69. proxy_url = "http://127.0.0.1:5010/get"
  70. ip_proxy = requests.get(proxy_url).text
  71. proxies = {
  72. "http": "http://" + ip_proxy,
  73. "https": "https://" + ip_proxy
  74. }
  75. html = requests.get(url, headers=headers, timeout=timeout, proxies=proxies)
  76. else:
  77. html = requests.get(url, headers=headers, timeout=timeout)
  78. if any(f in html.content for f in retry_flag):
  79. raise Exception
  80. return html
  81. except Exception as e:
  82. print(e)
  83. retry_time -= 1
  84. if retry_time <= 0:
  85. # 多次请求失败时,返回百度页面
  86. resp = Response()
  87. resp.status_code = 200
  88. return resp
  89. time.sleep(retry_interval)