I'm new to Python, and just started to learn the principles of Object Oriented Programming, this does not judge strictly.
This code is fully working but confused some elements:
- I don't understand what the
__init__
method needs to return. - I don't like
self.
inget_parse
that much.
Initially, I did it in order to be able to easily cause Parse.id
, or you want me to attribute.
But now, I need to first create an instance of the class, and then call the function get_parse
while accepting self
and the number of lines (k
), and only then, I will be available for items such as Parse.title
.
I tried to do the same when declaring __init__
, but nothing came of it, because at this stage the function may not be available (this is especially the language, is not it? In IPython not exist, or am I mistaken too?)
from bs4 import BeautifulSoup
import urllib.request
import csv
class Parse:
k = 1
def __init__(self,k):
pass
def read_csv(k):
with open('/home/narnikgamarnik/PycharmProjects/my_phyton3_projects/products_links2.csv') as f:
r = csv.reader(f)
cont = [row for row in r]
d = (cont[k])[0]
return d
def get_url(d):
try:
url = urllib.request.urlopen(d)
except urllib.error.HTTPError as err:
if err.code == 404:
return False
else:
raise
return url
def get_title(url):
try:
soup = BeautifulSoup(url, 'html.parser')
ol = soup.find('ol', 'breadcrumb')
title = ol.find_all('li')[-1].string
except AttributeError:
return False
return title
def get_gender(url):
try:
soup = BeautifulSoup(url, 'html.parser')
ol = soup.find('ol', 'breadcrumb')
gender = ol.find_all('a')[0].string
except AttributeError:
return False
return gender
def get_category(url):
try:
soup = BeautifulSoup(url, 'html.parser')
ol = soup.find('ol', 'breadcrumb')
brand = ol.find_all('a').get_text[1].string
except AttributeError:
return False
return brand
def get_model(url):
try:
soup = BeautifulSoup(url, 'html.parser')
ol = soup.find('ol', 'breadcrumb')
model = ol.find_all('a')[2].string
except AttributeError:
return False
return model
def get_article(url):
try:
soup = BeautifulSoup(url, 'html.parser')
product_code = soup.find('p', 'product__code')
article = product_code.find_all('span')[0].string
except AttributeError:
return False
return article
def get_article_2(url):
try:
soup = BeautifulSoup(url, 'html.parser')
geth1 = soup.find('h1')
article_2 = geth1.find_all('span')[0].string
except AttributeError:
return False
return article_2
def get_prices(url):
try:
soup = BeautifulSoup(url, 'html.parser')
product_price = soup.find_all('span', 'select_currency currency hide')
except AttributeError:
return False
return product_price
def get_img(url):
try:
soup = BeautifulSoup(url, 'html.parser')
div = soup.find_all('div', 'fotorama fotorama-primary')
for a in div:
b = a.find_all('a')
images = []
for c in b:
d = c['data-full']
images.append(d)
except AttributeError:
return False
return images
def get_parse(self,k):
self.d = self.read_csv(k)
self.url = self.get_url(self.d)
self.title = self.get_title(self.url)
self.url = self.get_url(self.d)
self.gender = self.get_gender(self.url)
self.url = self.get_url(self.d)
self.category = self.get_category(self.url)
self.url = self.get_url(self.d)
self.model = self.get_model(self.url)
self.url = self.get_url(self.d)
self.article = self.get_article(self.url)
self.url = self.get_url(self.d)
self.article_2 = self.get_article_2(self.url)
self.url = self.get_url(self.d)
self.prices = self.get_prices(self.url)
self.price_pln = self.prices[0].string[3:6]
self.price_usd = self.prices[1].string[3:6]
self.price_eur = self.prices[2].string[3:6]
self.price_gbp = self.prices[3].string[3:6]
self.price_rub = self.prices[4].string[3:7]
self.url = self.get_url(self.d)
self.images = self.get_img(self.url)
return self.d, self.title, self.gender, self.category, self.model, self.article, self.article_2, self.images, self.price_pln, self.price_usd, self.price_eur, self.price_gbp, self.price_rub, self.images
Usage is as follows:
from parse import parse
parse = parse.Parse
k = 1
parse.get_parse(parse,k)
Tell me how to make a code to be beautiful? I'm a novice at programming, but the wording of code seems cumbersome and ugly, although I understand that the syntax of the language, and so makes coding more enjoyable than js
self.read_csv(k)
does not match with the definition of the functiondef read_csv(k):
. Same for pretty much all your "methods". Can you provide the calling code, because I don't see how it could work with only this part? \$\endgroup\$from parse import parse parse = parse.Parse k = 1 parse.get_parse(parse,k)
\$\endgroup\$self.url = self.get_url (self.d)
. Is there a way to get rid of it? \$\endgroup\$