网页抓取检查单词是否在字符串中

2024-10-01 09:21:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着从他们的产品名称中得到袋子的样式(例如,从“铂金人造漆皮手提包”中得到“手提包”)。这是我的密码:

    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)

虽然它应该给我没有为袋子,不是手提包和手提包的袋子是手提包,它给我所有的袋子手提包。你知道吗


Tags: namefromimportnoneidstylehtmlfind
1条回答
网友
1楼 · 发布于 2024-10-01 09:21:07

您应该使用:

 if "Tote" in Name:
     ...

而不是str.findstr.find将返回找到的索引,或者返回-1。不管怎样,任何不是0的数字都将计算为True,这就是错误的来源。你知道吗

相关问题 更多 >