0

i write some code that returns two outputs. The error appears. What is the main problem of my code???????

I use from python3.x - beautifulsoup4-4.6.3

import codecs
from urllib.request import urlopen as uReq

from bs4 import BeautifulSoup as soup

for i in range(3): #electronic

my_url = "https://www.bamilo.com/mobile_phones/?facet_is_mpg_child=0&viewType=gridView&page="

uClient = uReq(my_url + str(i))

page_html = uClient.read()

uClient.close()

page_soup = soup(page_html, "html.parser")

containers = page_soup.findAll("div" , {"class" : "sku -gallery" })

filename = "mobile.csv"
f = codecs.open(filename, "a" , "utf-8-sig")
headers = "name, Brand, price_one, price_two, ranking\n"
f.write(headers)


for container in containers:

    name = container.img["alt"]

    title_container = container.findAll("span", {"class" : "brand"})

    Brand = title_container[0].text

    price = container.findAll("span",{"class" : "price"} )

    price_one = price[0].text.strip()

    price_old = container.findAll("span",{"class" : "price -old "})
    price_two = 0
    if len(price_old) > 0:
        price_two = price_old[0].text.strip()

    rank = container.findAll("span",{"class" : "rating-aggregate"})
    if len(rank) > 0:
        ranking = rank[0].text.strip()

    print("name " + name)
    print("Brand "+ Brand)
    print("price_one " + price_one)
    print("price_two {}".format(price_two))
    print("ranking " + ranking)

    f.write(name + "," + Brand.replace(",", "|") + "," + price_one.replace(",", "") + "," + price_two.replace(",", "") + "," + ranking + "\n")

f.close()

Traceback (most recent call last): name Galaxy J7 Pro SM-J730FD Dual 64GB Gold File "C:/Users/...../.PyCharm2018.2/config/scratches/scratch.py", line 53, in Brand Samsung  f.write(name + "," + Brand.replace(",", "|") + "," + price_one.replace(",", "") + "," + price_two.replace(",", "") + "," + ranking + "\n") AttributeError: 'int' object has no attribute 'replace' price_one 32,050,000ریال price_two 0 ranking 1

Error :::::

f.write(name + "," + Brand.replace(",", "|") + "," + 
price_one.replace(",", "") + "," + price_two.replace(",", "") + "," + 
ranking + "\n")

AttributeError: 'int' object has no attribute 'replace'

What changes should be applied? Is there any solution?

0

1 Answer 1

2

You are getting the error because of this line price_two = 0 if the condition you set is not met then the value of price_two is a integer. And you cannot use str.replace on it.

to fix problem replace

price_two = 0

to

price_two = '0'  #To string
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.