[英]python: Error:[<__main__.class instance at 0x>]
I wanna scrabble the APP's name on the app's website of Apple store and print it out on the terminal. 我想在苹果商店的应用程序网站上拼写该应用程序的名称,然后在终端上打印出来。
Here is my code: 这是我的代码:
from lxml import html
import requests
class AppCrawler:
def __init__(self,starting_url,depth):
self.starting_url = starting_url
self.depth = depth
self.apps = []
def crawl(self):
self.get_app_from_link(self.starting_url)
def get_app_from_link(self,link):
start_page = requests.get(link)
tree = html.fromstring(start_page.text)
name = tree.xpath('//h1[@itemprop="name"]/text()')[0]
app = App(name)
self.apps.append(app)
class App:
def __init__(self,name):
self.name=name
def __str__(self):
return ("Name:" + self.name)
crawler = AppCrawler('https://itunes.apple.com/us/app/candy-crush-saga/id553834731',0)
crawler.crawl()
################ print the list ##################################
print crawler.apps
################ print the element in the list ###################
for app in crawler.apps:
print app
Here is what I get in the terminal: 这是我在终端中得到的:
[<__main__.App instance at 0x029C3EE0>]
AppName:Candy Crush Saga
My question is: 我的问题是:
why the list print is [< main .App instance at 0x029C3EE0>] and use the " for in " loop to print the element in the list is exactly right?? 为什么列表打印为[< main .App instance at 0x029C3EE0>],并使用“ for in ”循环打印列表中的元素完全正确?
try: 尝试:
for app in crawler.apps:
print str(app)
or implement 或实施
__repr__
instead of __str__
__repr__
代替__str__
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.