我正在试图 grab 这个页面https://www.anesishome.gr/%CE%B2%CF%81%CE%B5%CF%86%CE%B9%CE%BA%CE%AC-159#!/我需要前5页每个产品的名称和价格 . 问题是我的代码给出了第一页的结果5次 . 好像我没有改变下一页的网址 . 我究竟做错了什么?谢谢!
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")
2 回答
我为你做了一个演示,希望这会有所帮助:
出:
此页面使用JavaScript / AJAX加载下一页 - 而这个AJAX使用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
是页码 .结果是JSON字符串
但也许您可以使用字符串切片来使用JSON模块/解析器来获取HTML .