python学习,代理服务器的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/VABTC/article/details/84789538
import urllib.request
def use_proxy(url,proxy_addr):
    proxy=urllib.request.ProxyHandler({"http":proxy_addr})#使用代理服务器
    opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)#给opener
    urllib.request.install_opener(opener)#添加为全局
    data=urllib.request.urlopen(url).read().decode("utf-8","ignore")
    return data

proxy_addr="58.218.201.188:58093"#代理网址,这里可以使用地址池,用for循环调用
url="http://www.baidu.com"#待爬url

data=use_proxy(url,proxy_addr)
print(len(data))

猜你喜欢

转载自blog.csdn.net/VABTC/article/details/84789538