实用爬虫-02-爬虫真正使用代理 ip

# coding:utf-8
# 爬虫使用代理IP

from urllib import request,error

if __name__ == '__main__':

    # 0.设置测 ip 的地址
    url = "http://2018.ip138.com/ic.asp"
    # 1.设置代理 ip,获取方法参照:https://www.cnblogs.com/xpwi/p/9600727.html
    proxy = {'http':'189.201.142.129:57815'}
    # 2.创建ProxyHandler
    proxy_handler = request.ProxyHandler(proxy)
    # 3.创建Opener
    opener = request.build_opener(proxy_handler)
    # 4.安装Opener
    request.install_opener(opener)

    # 下面再进行访问url就会使用代理服务器
    try:
        rsp = request.urlopen(url)
        html = rsp.read().decode('GBK')
        print(html)

    except error.HTTPError as e:
        print(e)
    except Exception as e:
        print(e)

运行结果

实用爬虫-02-爬虫真正使用代理 ip

  • 什么墨西哥,我在天津

更多文章链接:实用爬虫


- 本笔记不允许任何个人和组织转载

相关文章:

  • 2021-12-18
  • 2021-12-12
  • 2021-04-12
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2021-11-26
猜你喜欢
  • 2021-09-04
  • 2022-02-24
  • 2021-04-09
  • 2022-12-23
  • 2021-12-11
相关资源
相似解决方案