資源簡介
原理利用網頁 www.ip138.com 可以查詢ip地理位置的功能,
利用爬蟲從中獲取ip地理位置
ip地理位置存在 標簽中
代碼片段和文件信息
#!?/usr/bin/env?python
#coding=utf-8
#原理利用網頁?www.ip138.com??可以查詢ip地理位置的功能,
#利用爬蟲從中獲取ip地理位置
#ip地理位置存在??標簽中
import?urllib2
from?bs4?import?BeautifulSoup
def?get_http_content(url):
????try:
????????user_agent?=?‘Mozilla/4.0?(compatible;?MSIE?5.5;?Windows?NT)‘
????????headers?=?{?‘User-Agent‘?:?user_agent?}
????????html?=?urllib2.Request(url?headers?=?headers)
????????myResponse?=?urllib2.urlopen(html)
????????myPage?=?myResponse.read()
????except?urllib2.URLError?as?e:
????????return?None
????try:
????????bsObj?=?BeautifulSoup(myPage‘html5l
評論
共有 條評論