我已經(jīng)有了這段代碼,之前有一位朋友幫助過。我已經(jīng)獲得了網(wǎng)站上的所有鏈接。我想獲取名稱,名稱,價格,圖片,產(chǎn)品說明以及產(chǎn)品鏈接。僅當我們單擊產(chǎn)品時,描述的產(chǎn)品才會出現(xiàn)。我是Python的初學(xué)者。from bs4 import BeautifulSoupimport urllib.requestcount = 1url = "https://www.sociolla.com/155-foundation?p=%d"def get_url(url): req = urllib.request.Request(url) return urllib.request.urlopen(req)expected_url = url % countresponse = get_url(expected_url)link = []name = []merk = []price = []pic = []description = []while (response.url == expected_url): #print("GET {0}".format(expected_url)) soup = BeautifulSoup(response.read(), "html.parser") products = soup.find("div",{"id":"product-list-grid"}) for i in products: data = products.findAll("div",{"class":"product-item"}) for j in range(0, len(data)): link.append(data[j]["data-eec-href"]) count += 1 expected_url = url % count response = get_url(expected_url)print(len(link))"""import csvdataset=zip(link, merk, name, pic, price, description) with open("foundation_sociolla.csv","w", newline='') as csvfile: writer=csv.writer(csvfile) header=['link', 'merk', 'name', 'pic', 'price', 'description'] writer.writerow(header) writer.writerows(dataset)"""
添加回答
舉報
0/150
提交
取消