steamdb cookie
import requests, re, os, pymysql, time from lxml import etree from steamdb.YDM import YDMHttp # MYSQL_HOST = '192.168.107.229' # MYSQL_POST = 3306 # MYSQL_DATABASE = 'spider_app' # MYSQL_PASSWORD = '123456' # MYSQL_USER = 'root' MYSQL_HOST = '10.133.3.26' MYSQL_POST = 3306 MYSQL_DATABASE = 'spider_app' MYSQL_PASSWORD = 'root' MYSQL_USER = 'root' class steamdb(object): def __init__(self): requests.packages.urllib3.disable_warnings() self.headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", } self.post_headers = { "Content-Type": "application/x-www-form-urlencoded", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36", "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8", } self.data = {} self.post_url = '' self.session_url = 'https://captcha.su.baidu.com/session_cb?pub=377e4907e1a3b419708dbd00df9e8f79&callback=callback' self.image_url = 'https://captcha.su.baidu.com/image?session={}&pub=377e4907e1a3b419708dbd00df9e8f79' self.request = requests.Session() self.cookies = '' self.cfduid = '' self.cf_clearance = '' self.state = 0 self.db = pymysql.connect(host=MYSQL_HOST, port=MYSQL_POST, database=MYSQL_DATABASE, user=MYSQL_USER, password=MYSQL_PASSWORD, charset='utf8', autocommit=True) self.cursor = self.db.cursor() while True: if self.state: break self.steamdb_info() self.session_info() self.image_info() self.post_info() def steamdb_info(self): response = self.request.get(url='https://steamdb.info' + self.post_url, headers=self.headers, verify=False) cookies = requests.utils.dict_from_cookiejar(response.cookies) if '__cfduid' in cookies.keys(): self.cfduid = cookies['__cfduid'] print('steamdb_info:{}'.format(cookies)) etree_html = etree.HTML(response.content.decode('utf-8')) self.post_url = etree_html.xpath('//*[@id="challenge-form"]/@action')[0].strip() self.data['r'] = etree_html.xpath('//*[@id="challenge-form"]/input[1]/@value')[0].strip() self.data['id'] = etree_html.xpath('//*[@id="challenge-form"]/script/@data-ray')[0].strip() def session_info(self): html = self.request.get(url=self.session_url, headers=self.headers).content.decode('utf-8') self.data['captcha_challenge_field'] = re.findall(r'sessionstr":"(.+?)"}', html)[0] def image_info(self): path = os.path.dirname(os.path.abspath(__file__)) image_response = self.request.get(url=self.image_url.format(self.data['captcha_challenge_field']), headers=self.headers) filename = '{}/yzm.jpg'.format(path) with open(filename, 'wb') as f: f.write(image_response.content) print('------') codetype = 1000 # 超时时间,秒 timeout = 60 ydm = YDMHttp() result = ydm.run(filename, codetype, timeout) print(result) code = result[1] # code = input('请输入验证码:') print('code:{}'.format(code)) self.data['manual_captcha_challenge_field'] = code def post_info(self): response = self.request.post(url='https://steamdb.info' + self.post_url, headers=self.post_headers, data=self.data, verify=False) cookies = requests.utils.dict_from_cookiejar(response.cookies) print('post_info:{}'.format(cookies)) print(response.url) if cookies: if 'cf_clearance' in cookies.keys(): self.cf_clearance = cookies['cf_clearance'] self.cookies = '__cfduid={cfduid}; cf_clearance={cf_clearance}'.format(cfduid=self.cfduid, cf_clearance=self.cf_clearance) self.cursor.execute( 'UPDATE spider_cookies set cookies="{}", update_time="{}" WHERE platform="steamdb"'.format( self.cookies, time.strftime('%Y-%m-%d %X'))) print( 'UPDATE spider_cookies set cookies="{}", update_time="{}" WHERE platform="steamdb"'.format( self.cookies, time.strftime('%Y-%m-%d %X'))) self.state = 1 if __name__ == '__main__': steamdb()