关于程序员:seleniumphantomjs设置蚂蚁动态代理

41次阅读

共计 1513 个字符,预计需要花费 4 分钟才能阅读完成。

#!coding:utf-8
from selenium import webdriver
import time,hashlib,warnings
warnings.filterwarnings('ignore')

dcap = dict(webdriver.DesiredCapabilities.PHANTOMJS)

def authCode(appkey,secret):
    appkey = appkey
    secret = secret
    paramMap = {
        "app_key": appkey,
        "timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
    }
    keys = sorted(paramMap)
    codes = "%s%s%s" % (secret, str().join('%s%s' % (key, paramMap[key]) for key in keys), secret)
    sign = hashlib.md5(codes.encode('utf-8')).hexdigest().upper()
    paramMap["sign"] = sign
    keys = paramMap.keys()
    authHeader = "MYH-AUTH-MD5" + str('&').join('%s=%s' % (key, paramMap[key]) for key in keys)
    return authHeader

# 创立实例
def createSelenium(appkey,secret,proxy,phantomjs_path,url):
    auth = authCode(appkey,secret)
    # 此处开始设置头信息
    dcap["phantomjs.page.settings.userAgent"] = ('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36')
    # 此处带入 proxy 验证信息
    dcap["phantomjs.page.customHeaders.Proxy-Authorization"] = (auth)

    # By PhantomJS
    args = ['--load-images=false', '--disk-cache=true', f'--proxy={proxy}', '--ignore-ssl-errors=true', '--ssl-protocol=TLSv1']


    # For Windows
    br = webdriver.PhantomJS(service_args=args, desired_capabilities=dcap, executable_path=phantomjs_path)

    # 拜访页面
    br.get(url)
    # 设置页面等待时间, 不重要
    br.implicitly_wait(10)
    br.set_page_load_timeout(15)
    print(br.page_source)
    br.quit()
    # 接下来执行具体动作


if __name__ == '__main__':
    appkey = 'xxxxxxx'
    secret = 'xxxxxxxxxxxxxxxxxxxxx'
    proxy = 'http://xxx.xx.xx.xx:xxxx' #蚂蚁服务器的地址与端口
    phantomjs_path = 'phantomjs.exe'   #phantomjs 门路
    url = 'http://members.3322.org/dyndns/getip'    #要申请网址
    createSelenium(appkey,secret,proxy,phantomjs_path,url)

正文完
 0