2015-10-26 51 views
0

我希望有人能夠幫助我與urllib發佈。我的這個計劃的目標是發佈一個IP地址並獲得它的相對位置。我知道有很多API等等,但是我的學校並不太熱衷於以任何方式對他們的計算機進行任何修改(這是用於comp sci課程的)。因此,現在下面的代碼讓我知道我的位置,因爲我的計算機IP已經被網站感知了(我在猜測頭部?),但是我想要做的只是輸入IP並返回位置。 ipStr只是IP字符串(在這種情況下,它是紐約時代華納有線電視的IP)。我嘗試設置值並提交數據,但無論我將值設置爲什麼,它只是返回我自己的計算機位置。有任何想法嗎?Python 3 Urllib發佈問題

ipStr = "72.229.28.185" 


url = "https://www.iplocation.net/" 

values = {'value': ipStr} 

headers = {} 

headers ['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1' 

data = urllib.parse.urlencode(values) 

data = data.encode('utf-8') 

req = urllib.request.Request(url,data=data, headers = headers) 

resp = urllib.request.urlopen(req) 

page = str(resp.read()) 

npattern = "Google Map for" 

nfound = re.search(npattern,page) 

ns = nfound.start() 
ne = nfound.end() 

location = "" 

while page[ne:ne +1] != "(": 

    location += page[ne:ne+1] 

    ne += 1 

回答

0

你只需要參數名稱更改從valuequery,例如:

values = {'query': ipStr} 

如果你看看輸入字段的名稱的頁面(https://www.iplocation.net/)上,你會看到該字段的名稱是query

+0

哦,這是有道理的!謝謝! – Benji