2017-02-16 187 views
-2

我不知道是什麼問題,但它多次保持打印同一個東西。如果有人能幫助我,將不勝感激。謝謝多次打印相同的東西

from bs4 import BeautifulSoup 
from urllib.parse import urlparse 
import urllib.request 


req = urllib.request.Request('http://shopnicekicks.com/products/a-ma-maniere-x-diadora-n9000-mens-brown-sugar.xml') 
res = urllib.request.urlopen(req) 
end = res.geturl() 
soup = BeautifulSoup(res, 'lxml') 
parsed = urlparse(end) 


for variant in soup.variants.find_all("id", {"type": "integer"}): 
    cart = 'http://'+parsed.netloc+'/cart/' +variant.text+':1' 
    for size in soup.variants.find_all('title'): 
     print(cart, size.text) 
+0

應該打印什麼?那麼這是如何從輸入中推導出來的呢?它實際上打印了什麼? – interjay

+0

它假設打印像這樣: http://shopnicekicks.com/cart/23839239105:1 8 http://shopnicekicks.com/cart/23839239169:1 8.5 http://shopnicekicks.com/cart/23839239233 :1 9 http://shopnicekicks.com/cart/23839239297:1 9.5 http://shopnicekicks.com/cart/23839239361:1 10 http://shopnicekicks.com/cart/23839239425:1 10.5 http ://shopnicekicks.com/cart/23839239489:1 11 http://shopnicekicks.com/cart/23839239553:1 12 http://shopnicekicks.com/cart/23839239617:1 13 – EdBiz

回答

0

你的發現太高了,所以你最終得到文檔中每個標識的所有標題。在每個變體內部找到發現。

from bs4 import BeautifulSoup 
from urllib.parse import urlparse 
import urllib.request 


req = urllib.request.Request('http://shopnicekicks.com/products/a-ma-maniere-x-diadora-n9000-mens-brown-sugar.xml') 
res = urllib.request.urlopen(req) 
end = res.geturl() 
soup = BeautifulSoup(res, 'lxml') 
parsed = urlparse(end) 

for variant in soup.variants.find_all("variant"): 
    cart = 'http://'+parsed.netloc+'/cart/' +variant.id.text+':1' 
    size = variant.find("title") 
    print(cart, size.text) 
+0

的鏈接非常感謝。 – EdBiz

-1

如果您的print(cart, size.text)print(size, size.text)

+0

不,因爲我想打印購物車 – EdBiz