Ce que je veux, c'est juste gratter tous les produits. Pourquoi je ne peux pas non plus utiliser containers.div? Je suis vraiment confus quand il y a <div><\div><div>
tandis que mon tutoriel est juste <div></div>
.Pourquoi je ne peux pas appeler container.findAll ("h3", {"class": "name"})?
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://hbx.com/categories/sneakers'
# membuka koneksi, mengambil halaman
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
# html parsing
page_soup = soup(page_html, "html.parser")
# mengambil masing2 produk
containers = page_soup.findAll("div",{"class":"product-wrapper col-xs-6 col-sm-4"})
filename = "kontol.csv"
f = open(filename, "w")
headers = "judul, brand, harga\n"
f.write(headers)
for container in containers:
title_container = container.findAll("h3", {"class":"name"})
judul = title_container[0].text
brand_container = container.findAll("h4", {"class":"brand"})
brand = brand_container[0].text
price_container = container.findAll("span", {"class":"regular-price"})
harga = price_container[0].text
print("judul: " + judul)
print("brand: " + brand)
print("harga: " + harga)
f.write(judul + "," + brand + "," + harga + "\n")
f.close()
Quand j'essayer appel à l'aide container.findAll ("h3", { "classe": "nom"}) Je suis arrivé cette erreur
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python36\lib\site-packages\bs4\element.py", line 1807, in __getattr__
"ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'findAll'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
Après avoir exécuté ce code sur mon ordinateur, il semble que vous allez avoir quelques problèmes raclant ces données de ce site en utilisant urllib. Il semble que la plus grande partie du contenu est rendue en utilisant javascript, ce qui rendra impossible de l'utiliser avec urllib. Je suggérerais d'utiliser Selenium pour résoudre ce problème: http://selenium-python.readthedocs.io/. –