2014-06-10 26 views
1

由於某種原因,我不斷收到這個錯誤,我找不到原因。我對Python很新,所以請耐心等待。 我得到的錯誤是:AttributeError: 'str' object has no attribute 'read'AttributeError:'str'對象沒有屬性'read'Python urllib2

這是我的代碼:

import re, urllib2 
for i in range(0, 6): 
    url = "http://www.gametracker.com/search/garrysmod/?searchipp=50&searchpge= {0}#search".format(i) 
    print url 
    urlr = url.read() 
    ip = re.compile(r"\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?):\d{1,6}\b") 
    print ip.findall(urlr) 

我希望有人能幫助我。

回答

3

你忘了使用請求urlopen()進行:

urlr = urllib2.urlopen(url).read() 

此外,爲了避免Bad request錯誤,消除來自url多餘的空格:

url = "http://www.gametracker.com/search/garrysmod/?searchipp=50&searchpge= {0}#search".format(i) 
#                  ^