python爬取西刺网代理IP地址

1、ip信息

IP是英文Internet Protocol的缩写,意思是“网络之间互连的协议”,也就是为计算机网络相互连接进行通信而设计的协议。在因特网中,它是能使连接到网上的所有计算机网络实现相互通信的一套规则,规定了计算机在因特网上进行通信时应当遵守的规则。任何厂家生产的计算机系统,只要遵守IP协议就可以与因特网互连互通。正是因为有了IP协议,因特网才得以迅速发展成为世界上最大的、开放的计算机通信网络。因此,IP协议也可以叫做“因特网协议”。

2、使用python爬取西刺代理IP信息

import requests
import traceback
import re


headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
        }

def download(url):
    print('正在下载页面:{}'.format(url))
    try:
        res = requests.get(url, headers=headers)
        res.encoding = res.apparent_encoding

        if res.status_code == 200:
            return res.text
        else:
            raise ConnectionError
    except Exception:
        print('下载页面出错:{}'.format(url))
        traceback.print_exc()

def get_ip_list(resp):
    try:
        pattrens = 'alt="Cn" /></td>([\d\D]*?)</tr>'
        root = re.findall(pattrens ,resp)
        list_ip = []
        # 再次匹配数据的正则
        for i in range(len(root)):
            key = re.findall('<td>([\d\D]*?)</td>' ,root[i])
            list_ip.append(key[3].lower( ) + '://' +key[0 ] + ':' +key[1])
        return list_ip
    except Exception:
        print('解析IP地址出错l')
        traceback.print_exc()

def main():
    url ='https://www.xicidaili.com/'
    res = download(url)
    info = get_ip_list(res)
    for i in info:
        print(i)


if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/mengxj168/article/details/88259566