【问题标题】:getting exception when running soup findall loop运行汤 findall 循环时出现异常
【发布时间】:2020-02-16 07:19:00
【问题描述】:

我正在尝试查看特定字符串是否在表中。所以我试图用soup.find all 找到它。 我的代码是:

for i in soup:
    tmp = soup.findAll("td", {"class": "spec-name"})
    tmp2 = soup.findAll("td", {"class": "spec-value"})
    str1 = tmp[i]
    str2 = tmp[i+1]
    if ('Touch' in str1 and 'yes' in str2 or 'Screen' in str1 and 'YES' in str2):
        IsTouch = "touch screen, "
        FIsTouch = 'yes'
    if ('Touch' in str1 and 'NO' in str2 or 'Screen' in str1 and 'No' in str2):
        IsTouch = "not screen touch"
        FIsTouch = 'no'
        break

我想看看“是”是否与“触摸”类别内联

但是 - 我得到了除了。

我做错了什么?

【问题讨论】:

  • 有什么异常?

标签: python beautifulsoup web-crawler findall


【解决方案1】:

我设法找到了一个带有 2 个 for 循环的解决方案:

def get_touch(short_desc,tempCat,soup):  
for i in soup.findAll("td", {"class": "spec-name"}):
    tmp=str(i)
    if ('Touch' in tmp or 'touch' in tmp):
        for z in soup.findAll("td", {"class": "spec-value"}):
            tmp2=str(z)
            if('NO' in tmp2 or 'no' in tmp2 or 'No' in tmp2 or 'no' in tmp2):
                IsTouch = "not touch screen, "
                FIsTouch = 'no'
                break
            elif ('YES' in tmp2 or 'yes' in tmp2 or 'Yes' in tmp2):
                IsTouch = "touch screen, "
                FIsTouch = 'yes'
                break
        else:
            continue

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 2018-07-13
    • 2023-03-06
    • 2019-09-21
    • 1970-01-01
    相关资源
    最近更新 更多