【问题标题】:if statement not working with scraped web textif 语句不适用于抓取的网络文本
【发布时间】:2021-04-25 20:23:09
【问题描述】:

我知道这是一个相当普遍的问题,但是这种情况使它变得令人困惑。

我正在使用beautifulsoup 从网站上抓取某些数据,我也在使用它来检查我当前正在抓取的页面上的“下一页”链接,看看我是否可以抓取另一个。

next_page_button_finder = soup.find('ul', class_='navnext').text

为了检查结果如何,我使用以下方法打印它:

print(next_page_button_finder)

输出是:

Next >>

然而,这是奇怪的部分,当我尝试通过创建简单的 if 语句来验证这一点时:

    if next_page_button_finder == "Next >>":
        print("yes")
    else:
        print("no")

no”被打印出来。

任何帮助将不胜感激。

这是您可以用来复制问题的代码(spareroom.com 的任何链接都可以使用,但是,为方便起见,您可以使用此链接https://www.spareroom.co.uk/flatshare/?search_id=1034984872&):

from bs4 import BeautifulSoup
import requests

html_address = input("Paste page the address here:")

html_text = requests.get(html_address).text

soup = BeautifulSoup(html_text, 'lxml')
prices = soup.find_all('strong', class_='listingPrice')

next_page_button_finder = soup.find('ul', class_='navnext').text

print(next_page_button_finder)

if next_page_button_finder == "Next >>":
    print("yes")
else:
    print("no")

【问题讨论】:

  • 我的猜测是您可能在某处遗漏了一个空格,您可以尝试使用list(next_page_button_finder) 并打印出来
  • 那里有更多字符,如果您在打印中输入 *,您会看到。现在你可以去掉额外的东西了。试试这个print("*",next_page_button_finder,"*") if next_page_button_finder.strip() == "Next >>":

标签: python string if-statement beautifulsoup


【解决方案1】:

如果您在 if 语句处进行调试并停止,您将看到文本实际上包含前后换行符。您可以使用该字符串,也可以使用 string.strip() 预先删除您的字符串

【讨论】:

  • 你的意思是.strip() 我认为这里会更好
【解决方案2】:

更好的方法可能是简单地消除文本变化的可能性,并使用它的 id(实际元素而不是它的父元素 - 您当前的目标)简单地测试“按钮”是否存在于 html 中。 id 也将允许更快的匹配,这个测试应该更可靠。

import requests
from bs4 import BeautifulSoup as bs
  
links = ['https://www.spareroom.co.uk/flatshare/?search_id=1034984872&',
         'https://www.spareroom.co.uk/flatshare/?offset=10&search_id=1034984872&sort_by=age&mode=list',
         'https://www.spareroom.co.uk/flatshare/?offset=410&search_id=1034984872&sort_by=age&mode=list']

with requests.Session() as s:
    for link in links:
        r = s.get(link)
        soup = bs(r.content, 'lxml')
        next_page = soup.select_one('#paginationNextPageLink')
        if not next_page is None:
            print('Yes')
        else:
            print('No. Last page.')
    

【讨论】:

    【解决方案3】:

    我认为问题出在尾随空格。换句话说,在字符串的开头或在本例中为结尾有一个空格字符。由于这个空间,字符串不一样,比较时会产生False 语句(因为它们不相等)。

    你要比较变量的字符串是"Next >>",但是你说变量存储的字符串是"Next >> "(这个字符串的末尾有一个空格)。因此,它们不一样,会产生False

    你如何解决这个问题?如果您使用.strip() 方法,那么它会删除字符串周围的空格。如果您将代码更新为以下内容,它将起作用:

    if next_page_button_finder.strip() == "Next >>":
        print("yes")
    else:
        print("no")
    

    【讨论】:

    • 他提供了 2 个链接供使用
    • @Matiiss 谢谢你的收获。我还编辑了我的答案以包含更多信息。
    【解决方案4】:

    使用:

    print(list(next_page_button_finder))
    

    我能够看到“next_page_button_finder”str 实际上在文本前后都有换行符:

    ['\n', 'N', 'e', 'x', 't', ' ', '>', '>', '\n']
    

    所以我将 if 语句更改为:

    if next_page_button_finder == "\nNext >>\n":
        print("yes")
    else:
        print("no")
    

    现在它打印出来了:

    yes
    

    或者,我可以写一行代码:

    next_page_button_finder = next_page_button_finder.strip()
    

    去掉换行符和原始代码就可以了。

    【讨论】:

    • 使用.strip() 会更容易
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多