I am trying to scrape this page https://www.anesishome.gr/%CE%B2%CF%81%CE%B5%CF%86%CE%B9%CE%BA%CE%AC-159#!/ I need the name and price of each product for the first 5 pages. The problem is tha my code gives the results of the first page 5 times. As if I dont't change the url for the next pages. What am I doing wrong? Thank you!
from urllib.request import urlopen
from bs4 import BeautifulSoup
for i in range(5):
page="https://www.anesishome.gr/%CE%B2%CF%81%CE%B5%CF%86%CE%B9%CE%BA%CE%AC-159#!/page-{}".format(i)
html = urlopen(page)
soup=BeautifulSoup(html, "html.parser")
pin=[None]*240
puk=[None]*240
k=soup.find("ul", class_="product-grid row")
titles=k.find_all("a", class_="product_image")
i=0
for title in titles:
pin[i]=title.get("title")
i=i+1
t=soup.find("ul", class_="product-grid row")
prices=t.find_all("span", class_="price")
i=0
for price in prices:
puk[i]=price.get_text()
i=i+1
x=0
with open('vrefika.txt', 'w') as f:
for x in range(0,i):
print(pin[x])
print("price=",puk[x])
string=pin[x]
f.write(string+"\n")
string=puk[x]
f.write(string+"\n")
This page uses JavaScript/AJAX to load next pages - and this AJAX uses url
https://www.anesishome.gr/modules/blocklayered/blocklayered-ajax.php?id_category_layered=159&layered_price_slider=2_350&orderby=price&orderway=asc&n=48&p=2&_=1486296430052
p=2
is page number.Result is JSON string
but maybe you you could use string slicing to get HTML with using JSON module/parser.
I made a demo for you, hope this will help:
out: