from urllib import request,parse,error
from http import cookiejar
import random
import ssl
import time
ssl._create_default_https_context = ssl._create_default_https_context
proxy_list = [
{"http":"121.17.210.114:8060"}, #111
{"http":"60.217.64.237:31923"}, #111
{"https":"27.42.168.46:48919"}, #111
{"https":"117.88.5.121:3000"}, #1111
{"https":"101.254.136.130:8080"},
{"https":"60.184.173.236:808"}, #111
{"https":"222.85.39.8:808"}, #1111
#{"https":"223.240.208.210:8010"}, #error
{"http":"60.217.64.237:31923"}
]
agents = [
{"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36 Edg/81.0.416.45"}, #微软
{"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4094.1 Safari/537.36"}, #UC
{"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv,2.0.1) Gecko/20100101 Firefox/4.0.1"}, #火狐
{"User-Agent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)"}, #搜狗
{"User-Agent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)"}, #360
{"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0"} #IE9
]
#生成cookie对象、管理器
cookie = cookiejar.CookieJar()
cookie_handler = request.HTTPCookieProcessor(cookie)
#生成HTTP与HTTPS管理器
http_handler = request.HTTPHandler()
https_handler = request.HTTPSHandler()
#生成proxy对象与管理器
proxy = random.choice(proxy_list)
proxy_handler = request.ProxyHandler(proxy)
#构建管理请求管理器
opener = request.build_opener(cookie_handler,http_handler,https_handler,proxy_handler)
#request.install_opener(opener) #加载失败
def login(url):
global agents
agent = random.choice(agents)
print("IP代理地址:{}".format(proxy))
print("游览器身份:{}".format(agent))
data = {
"userName": "18835064513",
"pwd": "syc19980923",
"languages": "zh",
"fromsite": "www.huawei.com",
"authMethod": "password"
}
print("失败了???")
time.sleep(1)
print("1秒后:\n真的失败了........")
data = parse.urlencode(data)
req = request.Request(url, data=bytes(data,"utf-8"),headers=agent)
res = opener.open(req)
html = res.read().decode()
print(html)
print("-"*20)
print("成功了!!!")
print("IP代理地址:{}".format(proxy))
print("游览器身份:{}".format(agent))
if __name__ == "__main__":
try:
start = time.time()
url = "https://www.huawei.com/en/accounts/LoginPost"
login(url)
end = time.time()
print("{}秒后:\n成功!!!!!!!!!!!!".format(end-start))
except error.HTTPError as e:
print(e)
except Exception as e:
print(e)
12-12
894
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-12
283
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交