-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_ip
52 lines (46 loc) · 1.62 KB
/
test_ip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
import pandas as pd
import numpy as np
import itertools
import requests
import json
def get_proxy(headers):
#API_url为您在网站上的API
API_url = 'http://get.3ip.cn/dmgetip.asp?apikey=37c26b33&pwd=c15287e1366c93e0c5f0ccf7e645112c&getnum=1&httptype=2&geshi=2&fenge=1&fengefu=&Contenttype=1&operate=2'
if not API_url:
print('出错了:请从www.3ip.cn生成api地址')
proxy = {}
return proxy
try:
aaa = requests.get(API_url, headers=headers).text
aaa_json = json.loads(aaa)
if not aaa_json or aaa_json['code'] != 0:
proxy = {}
else:
proxy_host = aaa_json['data'][0]['ip'] + ':' + str(aaa_json['data'][0]['port'])
proxy_city = aaa_json['data'][0]['city']
print('代理IP为:'+proxy_host)
proxy = 'socks5://' + proxy_host
except Exception as e:
print('获取代理IP失败:', e)
proxy = {}
return proxy
# proxy_list = ["socks5://154.12.95.148:1080"]
headers = {
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)'
}
proxy = get_proxy(headers)
if not proxy:
print('获取代理失败,停止继续执行')
else:
edge_options = webdriver.EdgeOptions()
edge_options.add_argument('--proxy-type=http')
edge_options.add_argument(f' --proxy-server={proxy}')
one_driver = webdriver.Edge(options=edge_options)
try:
one_driver.get(r'http://whatismyipaddress.com')
one_driver.implicitly_wait(10)
except Exception as e:
print(e)