首页 > 编程语言 >python获取IP工具

python获取IP工具

时间:2022-12-01 20:33:35浏览次数:37  
标签:获取 python IP resp encoding url text print page

一、获取IP地址

二、判断地址是否可用。并把可的地址保存。


import requests
from lxml import etree
import time


headers = {
"User-Agent": "Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.36(KABUL, like Gecko) "
"Chrome/86.0.4240.198Safari/537.36 "
}
url = "http://www.66ip.cn/areaindex_10/"
for page in range(1, 11):
page_url = url + str(page) + ".html"
resp = requests.get(url=page_url, headers=headers)
resp.encoding = resp.apparent_encoding
e = etree.HTML(resp.text)
ips = e.xpath("//div[1]/table//tr/td[1]/text()")
ports = e.xpath("//div[1]/table//tr/td[2]/text()")
addrs = e.xpath("//div[1]/table//tr/td[3]/text()")
# print(ips)
# print(ports)
# print(addrs)
with open("06-IP代理.txt","w",encoding="utf-8") as f:
for i,p,a in zip(ips,ports,addrs):
f.write(f"---{i}---{p}---{a}\n")
time.sleep(5)
print("保存完毕")


import requests
from lxml import etree
import time


headers = {
"User-Agent": "Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.36(KABUL, like Gecko) "
"Chrome/86.0.4240.198Safari/537.36 "
}
url = "http://www.66ip.cn/areaindex_10/"
for page in range(1, 11):
page_url = url + str(page) + ".html"
resp = requests.get(url=page_url, headers=headers)
resp.encoding = resp.apparent_encoding
e = etree.HTML(resp.text)
ips = e.xpath("//div[1]/table//tr/td[1]/text()")
ports = e.xpath("//div[1]/table//tr/td[2]/text()")
addrs = e.xpath("//div[1]/table//tr/td[3]/text()")
# print(ips)
# print(ports)
# print(addrs)
for ip,port in zip(ips,ports):
if ip != 'ip':
proxies_dict = {
"https": "https://" + ip + ':' + port,
}
print(proxies_dict)
try:
res = requests.get(url="https://www.baidu.com", headers=headers, proxies=proxies_dict, timeout=2)
res.encoding = res.apparent_encoding
if res.status_code == 200:
with open("06-IP代理-可用.txt","a",encoding="utf-8") as f:
f.write(f"{ip}---{port}\n")
except:
print("当前代理不可用")
time.sleep(3)
print("保存完毕")


python获取IP工具_chrome


1、xpath解析数据。

e = etree.HTML(resp.text)
ips = e.xpath("//div[1]/table//tr/td[1]/text()")

2、txt文本格式保存数据。

  with open("06-IP代理.txt","w",encoding="utf-8") as f:
for i,p,a in zip(ips,ports,addrs):
f.write(f"---{i}---{p}---{a}\n")


标签:获取,python,IP,resp,encoding,url,text,print,page
From: https://blog.51cto.com/u_14012524/5904084

相关文章

  • PowerScript语法参考手册之语言基础
    PowerScript语法参考手册​前言:本文用于PowerBuilder12.6程序员,PowerBuilder最初由Sybase发布,能很快捷开发C/S程序或者多层应用系统。SAP以58亿美元现金收购Sybase以抗衡甲......
  • ajax发送post后台无法获取问题
    作为一名后端小渣渣一名。最近项目中出现了由于get请求携带参数过长而导致无法发出请求的问题,由于才疏学浅只能想到用post请求来解决此问题。get参数长度受限制,post不受......
  • 43. Multiply Strings(重要)
    Giventwonumbersrepresentedasstrings,returnmultiplicationofthenumbersasastring.Note:Thenumberscanbearbitrarilylargeandarenon-negative.Conv......
  • Excel技巧:如何获取IP地址的前三位并获取IP段?
    需求说明:查看服务器的一些日志发现经常有一些爆破的记录,如果我们单个赋值IP,那将是一个巨大的工程,所以我们可以通过Excel来处理;处理步骤:1.下载日志表格;2.新建一个表,输入......
  • 关于iptables封禁国外ip方法
    关于iptables封禁国外ip方法 1)下载安装包yum-yinstalliptablesyum-yinstallipset2)添加集合ipsetcreatechinahash:netmaxelem655363)编写脚本vim/home/ch......
  • eclipse启动失败Could not create the Java virtual machine
    eclipse启动失败CouldnotcreatetheJavavirtualmachine​​​​​​​​解决办法删掉C:\Windows\System32下面的java.exe,javaw.exe和javaws.exe三个文件......
  • python自带静态web服务器搭建代码实现
    #coding:utf-8fromwsgiref.simple_serverimportmake_server#导入simple_server模块#视图函数defhome(*args):return"home"deflogin(*args):ret......
  • python之路40 前端之 CSS 标签查询
    表单标签的补充说明基于form表单发送数据1.用于获取用户数据的标签至少应该含有name属性name属性相当于字典的键用户输入的数据会被保存到标签的value属性中......
  • SpringCloud(四) - 微信获取用户信息
    1、项目介绍2、微信公众平台和微信开放文档2.1微信公众平台2.1.1网址链接https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index......
  • Python学习(五):基本的数据结构——元组及常用方法
    1.元组的概述:元组与列表类似,由任意类型的元素组成序列;元组是不可变的(与列表不同处);2.元组的创建及检验:>>>tuple_1=(1,2,3,4)>>>tuple_1(1,2,3,4)>>>tuple_2......