ここで提供されているリンクの製品に関する基本情報(製品名、画像、評価、価格)を取得するために2日間苦労しているので、 。あなたは、内側li
要素を必要としながら、ul
要素 - - 各製品容器を表す私は新しいです、この私のコードとはAttributeError: 'NavigableString'オブジェクトに属性がありません。インデックスが範囲外です
import urllib.request
from bs4 import BeautifulSoup
from random import randint
from bs4.dammit import EncodingDetector
import re
import sys
url='https://fr.aliexpress.com/category/205000316/men-clothing-accessories.html'
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:48.0) Gecko/20100101 Firefox/48.0,Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0',Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.0) Gecko/20100101 Firefox/33.0',Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko',Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; EN; rv:11.0) like Gecko',Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)',Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/5.0)',Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A'"
req = urllib.request.Request(url, headers = headers)
html = urllib.request.urlopen(req).read()
soup = BeautifulSoup(html.decode('utf8', 'ignore'), "html.parser")
# retrive infos such product name, price , rating
Prod=soup.find_all('ul', class_='util-clearfix son-list')
for item in Prod:
print(item.contents[0].find_all("span",{"class:","star star-s "[0].text)
print(item.contents[0].find_all("span",{"class:","star star-s "[0].text)
print(item.contents[0].find_all("span",{"class:","star star-s "[0].text)
なぜですか同じ要素の3倍?おそらく最初の 'for'の中で2番目の' for'ループを使用します。 – furas
あなたは同じインデックスを何度も印刷しているようですが、おそらくコンテンツのような動的インデックスを使用するべきでしょう。 – vladko