Reputation: 46
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??
Upvotes: 0
Views: 1222
Reputation: 11550
try:
for app in crawler.apps:
print str(app)
or implement
__repr__
instead of __str__
Upvotes: 1