import requests import time from libs.proxy import Proxy from log.print_log import PrintLog class DyUserLiveInfo: @staticmethod def get_data(uid): url = 'http://webcast-hl.amemv.com/webcast/room/reflow/info/?room_id=6798432314704530190&type_id=2&user_id=' + uid + '&live_id=1&app_id=1128' headers = { 'Host': 'webcast-hl.amemv.com', 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'Upgrade-Insecure-Requests': '1', 'User-Agent': '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', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', 'Sec-Fetch-Site': 'none', 'Sec-Fetch-Mode': 'navigate', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8' } retry = 0 response = None while True: if retry > 10: break retry += 1 proxy = Proxy.get() proxies = { "http": "http://" + proxy, "https": "http://" + proxy } try: response = requests.get( url, headers=headers, proxies=proxies, timeout=8 ) if (response.status_code == 200) and (response.text is not None) and (response.text != ''): break else: PrintLog.print( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' 爬取http连接失败!' + str(response.status_code) + '\n' + Proxy.proxy_info + '\n' + uid + '\n' ) time.sleep(1) except requests.exceptions.ProxyError as e: PrintLog.print( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' 代理过期!' + str(e) + '\n' + uid + '\n' + Proxy.proxy_info ) Proxy.del_proxy(proxy) pass except requests.exceptions.ConnectTimeout as e: PrintLog.print( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' ConnectTimeout!' + str(e) + '\n' + uid + '\n' + Proxy.proxy_info ) Proxy.del_proxy(proxy) pass except Exception as e: PrintLog.print( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' 请求抛出异常!' + str(e) + '\n' + uid + '\n' + Proxy.proxy_info ) pass return response.text