ipip.net IP位置信誉信息爬取

批量查询IP位置和信誉信息
import requests
from lxml import etree
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

requests.packages.urllib3.disable_warnings()#忽略警告
filename="ip_black_file.txt"
result="ipip.csv"
url="https://www.ipip.net/ip.html"

page_headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0',
        'Connection': 'keep-alive',
        'Content-Type':'application/x-www-form-urlencoded',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
    }

def url_open(check_ip):
    try:
        post_data={'ip':check_ip}
        res=requests.post(url,headers=page_headers,data=post_data,verify=False)
        status=res.status_code
        content=res.text
        #print(status)
        return status,content
    except Exception as e:
        return 0,0

def check_ipip(check_ip):
    try:
        status,content=url_open(check_ip)
        if status==200:
            html=etree.HTML(content)
            tag=html.xpath('//div[@class="outer tableNormal ipSearch"]//div[@class="inner"]//table//tr//td/span/text()')
            tag=str(tag[1:]).replace("(更多数据请查看', ')","").replace(", ' '","").replace("(', ')","").replace("', '","';'").replace(" ","").replace("['","").replace("']","").replace("'","")
            #print(tag)
            return tag
        else:
            return "查询失败"
    except Exception as e:
        print(str(e))
        return "查询失败"
        
if __name__=="__main__":  
	with open(filename,'r',encoding='utf-8') as fh:
		data=fh.read().split('\n')
		with open(result,'a+',encoding='utf-8') as f:
			for ip in data:
				if len(ip)>0:
					print("正在查询IP:"+ip)
					tag=check_ipip(ip)
					f.write(ip+";"+tag+'\n')


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jepson2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值