2017-05-24 29 views
0

我正在編寫一個獲取產品名稱,參數並將其保存到文本文件的程序。頁是http://www.euro.com.pl/pralki,strona-1.bhtml我寫了這個:Python:逐個保存參數和產品

`url = "http://www.euro.com.pl/pralki,strona-1.bhtml" 
source_code = requests.get(url) 
plain_text = source_code.text 
soup = BeautifulSoup(plain_text, "html.parser") 
for product in soup.find_all('div', {'class': 'product-main'}): 
    for prod in soup.find_all('h2', {'class': 'product-name'}): 
     temp.extend(prod.stripped_strings) 
     for param in soup.find_all('div', {'span': 'attribute-value'}): 
      temp.extend(param.stripped_strings)` 

但它給了我超過20萬行的文本文件!無法弄清楚如何修復或從頭開始寫入。

+0

你究竟想要什麼?你有什麼問題 ? –

+0

我只是想將產品名稱及其參數保存到該文件的文件中。 – Wakun

+0

難道不是這樣嗎? –

回答

0

從這:

>>> len(soup.find_all('div', {'class': 'product-main'})) 
30 
>>> len(soup.find_all('h2', {'class': 'product-name'})) 
30 

我得出這樣的結論是product 30個實例和prod所以這行

temp.extend(prod.stripped_strings) 

的30種情況下被執行900次。從你對問題的描述中,我認爲你期待它被執行30次。

+0

好吧,現在我看到我做錯了什麼,但仍然無法弄清楚如何正確地做到這一點。 – Wakun