1 #coding=gb2312 2 import urllib2; 3 proxy_handler = urllib2.ProxyHandler({‘http‘:‘61.184.192.42:80‘}); 4 #此处替换代理IP和端口 5 opener = urllib2.build_opener(proxy_handler) ; 6 f=opener.open(‘http://1111.ip138.com/ic.asp‘); 7 con=f.read(); 8 print con; 9 start=con.find(‘您的IP是‘); 10 end=con.find(‘</center>‘); 11 print con[start:end]; 12 f.close();
urllib2库是http编程中经常使用的一个库,python内置的,直接导入就行。
首行#coding=gb2312原因是IP查询网址(http://1111.ip138.com/ic.asp)使用了gb2312编码;
原文:http://www.cnblogs.com/jerory/p/4363869.html