1. 程式人生 > >Python爬取代理IP

Python爬取代理IP

safari () get ons too user import tool com

環境Python3.6

#!/usr/bin/env python
#-*-coding=utf-8 -*-
#AUTHOR:duwentao


import requests

import re


print("獲取代理IP地址")

header = {

    "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"
}

reponse = requests.get("https://www.kuaidaili.com/free/inha/",header)
reponse.encoding='utf-8'
html = reponse.text


#p = r'<tr><td data-title="IP">(.*?)</td><td data-title="PORT">(.*?)</td><td data-title="匿名度">(.*?)</td><td data-title="類型">(.*?)</td><td data-title="位置">(.*?)</td><td data-title="響應速度">(.*?)</td><td data-title="最後驗證時間">(.*?)</td></tr>'

ip = r'<td data-title="IP">(.*?)</td>'
IP = re.findall(ip,html,re.M|re.S)

dk = r'<td data-title="PORT">(.*?)</td>'
DK = re.findall(dk,html,re.M|re.S)


f=open("ip_list.txt","a")
for ip in IP:
    for dk in DK:
        IP_DK = ip +":"+ dk + "\n"
        f.write(IP_DK)

f.close()

print("保存完畢")


Python爬取代理IP