我试着从他们的产品名称中得到袋子的样式(例如,从“铂金人造漆皮手提包”中得到“手提包”)。这是我的密码:
from urllib.request import urlopen
from bs4 import BeautifulSoup
saksurl="http://www.saksfifthavenue.com/Handbags/shop/_/N-52jzot/Ne- 6lvnb5?FOLDER%3C%3Efolder_id=2534374306622829"
html = urlopen(saksurl)
bsObj = BeautifulSoup(html.read(),"html.parser")
for product in bsObj.select("#product-container [id^=product-]"):
Style="None"
Name=product.find("p",{"class":"product-description"}).get_text()
print(Name)
if Name.find("Tote"):
Style="Tote"
else:
Style="None"
print(Style)
虽然它应该给我没有为袋子,不是手提包和手提包的袋子是手提包,它给我所有的袋子手提包。你知道吗
您应该使用:
而不是
str.find
。str.find
将返回找到的索引,或者返回-1
。不管怎样,任何不是0的数字都将计算为True
,这就是错误的来源。你知道吗相关问题 更多 >
编程相关推荐