检测代理ip是否成功
def get_ip():import requests
from fake_useragent import UserAgent
from lxml import etree
proxies = {'http':'117.68.245.140:4251',#此处为代理IP及端口
'https':'117.68.245.140:4251'}
headers = {'User-Agent': UserAgent().random}
url = 'http://www.ip38.com/'
res = requests.get(url = url,headers=headers,proxies=proxies)
ip = etree.HTML(res.content).xpath("//span[@class='right']/a/text()")
print('代理ip为:{}'.format(ip))#如果此ip与输入ip相同则证明代理ip可用 如果这个网站哪天不在了呢,最根本的是你要ping成功这个ip才算可用
import subprocess as sp
#输入你要测试的ip
name_='www.baidu.com'
cmd='ping -n 3 -w 3 %s'
p=sp.Popen(cmd %name_,stdin=sp.PIPE,stdout=sp.PIPE,stderr=sp.PIPE,shell=True)
out=p.stdout.read().decode('gbk')
print(out)
v.ki 发表于 2021-3-7 15:16
如果这个网站哪天不在了呢,最根本的是你要ping成功这个ip才算可用
多谢多谢
页:
[1]