ip代理构建

#IP代理的构建
'''
import urllib.request
ip="54.39.24.37:3128"
proxy=urllib.request.ProxyHandler({"http":ip})
opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
url="http://www.baidu.com"
data=urllib.request.urlopen(url).read().decode("utf-8","ignore")
print(len(data))
#注意下面
#出现UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 29542: illegal multibyte sequence
#在下面这条指令加上encoding="utf-8"
fh=open("C:\\Users\\gaoxingyuan\\Desktop\\baidu.html","w",encoding="utf-8")
fh.write(data)
fh.close()
'''

#ip代理池构建
#第一种方案(适合于代理IP稳定的情况)
'''
import random
import urllib.request

ippools=[
    "54.39.24.37:3128",
    "36.255.189.38:8080",
    "191.102.73.38:8080",
    ]
def ip(ippools):
    thisip=random.choice(ippools)
    print(thisip)
    proxy=urllib.request.ProxyHandler({"http":thisip})
    opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
    urllib.request.install_opener(opener)
for i in range(0,3):
    try:
        ip(ippools)
        url="http://www.baidu.com"
        data=urllib.request.urlopen(url).read().decode("utf-8","ignore")
        print(len(data))
        fh=open("C:\\Users\\gaoxingyuan\\Desktop\\baidu"+str(i+1)+".html","w",encoding="utf-8")
        fh.write(data)
        fh.close()
    except Exception as e:
        print(str(e))
'''

#第二种方案(接口调用法,这种方法更适合代理IP不稳定的情况)

import urllib.request

def ip():
    thisip=urllib.request.urlopen("API接口地址")
    print(thisip)
    proxy=urllib.request.ProxyHandler({"http":thisip})
    opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
    urllib.request.install_opener(opener)

for i in range(0,3):
    try:
        ip()
        url="http://www.baidu.com"
        data=urllib.request.urlopen(url).read().decode("utf-8","ignore")
        print(len(data))
        fh=open("C:\\Users\\gaoxingyuan\\Desktop\\baidu"+str(i+1)+".html","w",encoding="utf-8")
        fh.write(data)
        fh.close()
    except Exception as e:
        print(str(e))
'''
遍历列表
for item in 列表名
    print(item)
'''































        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值