店播爬取Python脚本

live_commodity_detail_v1.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import requests
  2. import time
  3. import random
  4. from libs.proxy import Proxy
  5. from log.print_log import PrintLog
  6. class LiveCommodityDetailV1:
  7. @staticmethod
  8. def get_data(product_id):
  9. url = 'http://ec.snssdk.com/product/lubanajaxstaticitem?id=' + product_id + '&page_id=&scope_type=5&item_id=&b_type_new=0'
  10. userAgentList = [
  11. 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0',
  12. 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3908.2 Mobile Safari/537.36'
  13. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116'
  14. ]
  15. headers = {
  16. 'Host': 'ec.snssdk.com',
  17. 'Connection': 'keep-alive',
  18. 'Cache-Control': 'max-age=0',
  19. 'User-Agent': random.choice(userAgentList),
  20. 'Referer': 'https://haohuo.jinritemai.com/',
  21. 'Accept': 'application/json, text/plain, */*',
  22. }
  23. while True:
  24. proxy = Proxy.get()
  25. proxies = {
  26. "http": "http://" + proxy,
  27. "https": "http://" + proxy
  28. }
  29. try:
  30. response = requests.get(
  31. url,
  32. headers=headers,
  33. proxies=proxies,
  34. timeout=10
  35. )
  36. if response.text is not None:
  37. break
  38. else:
  39. print(response)
  40. print('爬取http连接失败!')
  41. time.sleep(1)
  42. except requests.exceptions.ProxyError as e:
  43. PrintLog.print(
  44. time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '代理过期!' + str(e) + '\n'
  45. + product_id + '\n'
  46. + Proxy.proxy_info
  47. )
  48. Proxy.del_proxy(proxy)
  49. pass
  50. except requests.exceptions.ConnectTimeout as e:
  51. PrintLog.print(
  52. time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' ConnectTimeout!' + str(e) + '\n'
  53. + product_id + '\n'
  54. + Proxy.proxy_info
  55. )
  56. Proxy.del_proxy(proxy)
  57. pass
  58. except Exception as e:
  59. PrintLog.print(
  60. time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' 请求抛出异常!' + str(e) + '\n'
  61. + product_id + '\n'
  62. + Proxy.proxy_info
  63. )
  64. pass
  65. return response.text