我刚开始学习Python。我想写一个蜘蛛程序来在网上找些笑话。当我运行程序时它警告:'AttributeError:'Spider_Model'对象没有属性'pages'。而且我在网上找到的解决方案不起作用。这是代码。请看一下。
import urllib.request as request
import re
#import _thread
import time
class Spider_Model:
def __int__(self):
self.page = 1
self.pages = []
self.enable = False
# Download all the jokes and save it to the list
def GetPage(self, page):
myUrl = 'https://www.qiushibaike.com/hot/' + str(page)
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
headers = {'User_Agent' : user_agent}
req = request.Request(myUrl, headers = headers)
myResponse = request.urlopen(req)
myPage = myResponse.read()
# The role of decode is to convert other encoded strings into unicode string
unicodePage = myPage.decode('utf-8')
myItems = re.findall('<span>(.*?)</span>', unicodePage, re.s)
items = []
for item in myItems:
items.append(item[0].repalce("\n", ""), item[1].replace("\n", ""))
return items
# Download new jokes
def LoadPage(self):
# If users doesn't enter quit, keep download
while self.enable:
# If the pages array less than 2
if len(self.pages) < 2:
try:
myPage = self.GetPage(str(self.page))
self.page += 1
self.pages.append(myPage)
except:
print('Connection failed')
else:
time.sleep(1)
def ShowPage(self, nowPage, page):
for items in nowPage:
print('No.%d Page:'% page, items[0], items[1])
myInput = input()
if myInput == 'quit':
self.enable = False
break
def Start(self):
self.enable = True
page = 0
print('Loading...')
# _thread.start_new_thread(self.LoadPage(), ())
while self.enable:
if len(self.pages) > 0:
nowPage = self.pages[0]
page += 1
del self.pages[0]
self.ShowPage(nowPage, page)
print('Hit Enter:')
input(' ')
myModel = Spider_Model()
myModel.Start()