哆啦A梦 50周年应援

python爬取ip地址

ip查询,异步get请求

分析接口,请求接口响应json

 发现可以data中获取 result.json()['data'][0]['location']

# _*_ coding : utf-8 _*_
# @Time : 2021/11/1 20:29
# @Author : 秋泊酱
# @File : ip抓取

import requests
ips = ['8.8.8.8']
result = requests.get('https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query='+ips[0]+'&co=&resource_id=5809',verify = False)
print(result.json()['data'][0]['location'])

for构造255个ip池演示

# _*_ coding : utf-8 _*_
# @Time : 2021/11/1 20:29
# @Author : 秋泊酱
# @File : ip抓取

import requests

import warnings
#忽视,InsecureRequestWarning(不安全请求警告)
warnings.filterwarnings('ignore')
for i in range(255):
    #修改第一位数
    ips = str(i) + '.8.8.8'
    result = requests.get('https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query='+ips+'&co=&resource_id=5809',verify = False)
    print(ips,result.json()['data'][0]['location'])

写入txt中

# _*_ coding : utf-8 _*_
# @Time : 2021/11/1 20:29
# @Author : 秋泊酱
# @File : ip爬取

import requests

import warnings
# 忽视,InsecureRequestWarning(不安全请求警告)
warnings.filterwarnings('ignore')

# 打开文件
f = open('./ip_lo.txt', mode='w', encoding='utf8')
try:
    for i in range(15):

        # 修改第一位数
        ips = str(i) + '.8.8.8'
        result = requests.get('https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query='+ips+'&co=&resource_id=5809',verify = False)
        print(ips,result.json()['data'][0]['location'])
        # 写入
        f.write(ips + '\t' + result.json()['data'][0]['location'] + '\n')

except BaseException:
    print("写入失败")
f.close()

posted @ 2021-11-01 23:23  秋泊ソース  阅读(224)  评论(0编辑  收藏  举报