python3.0学习七: 用代理访问网页

'''
备注:本程序是为了使用代理来访问网页

'''

import urllib.request
import random


#一个检测网站的ip地址
url='http://www.whatismyip.com.tw'

#可以添加一个ip列表,用于随机访问ip
iplist=['125.62.14.162:3128','111.243.68.23:8080','113.119.204.173:808']



#新建一个代理
proxy_support=urllib.request.ProxyHandler({'http':random.choice(iplist)})

#创建一个opener
opener=urllib.request.build_opener(proxy_support)
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36')]


urllib.request.install_opener(opener)

response=urllib.request.urlopen(url)

html=response.read().decode('utf8')

print(html)



#运行结果如下:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="description" content="我的IP查詢"/>
    <meta name="keywords" content="查ip,ip查詢,查我的ip,我的ip位址,我的ip位置,偵測我的ip,查詢我的ip,查看我的ip,顯示我的ip,what is my IP,whatismyip,my IP address,my IP proxy"/>
    <title>我的IP位址查詢</title>
  </head>
  <body>
<h1>IP位址</h1> <h2>113.119.204.149</h2><h1>真實IP</h1> <h2>220.178.4.222</h2>

<script type="text/javascript">
var sc_project=6392240; 
var sc_invisible=1; 
var sc_security="65d86b9d"; 
var scJsHost = (("https:" == document.location.protocol) ? "https://secure." : "http://www.");
document.write("<sc"+"ript type='text/javascript' src='" + scJsHost + "statcounter.com/counter/counter.js'></"+"script>");
</script>
<noscript><div class="statcounter"><a title="websitestatistics" href="http://statcounter.com/" target="_blank"><img class="statcounter"  src="http://c.statcounter.com/6392240/0/65d86b9d/1/" alt="website statistics"></a></div></noscript>

  </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值