【问题标题】:Print returns none even though there its not即使没有,打印也不返回
【发布时间】:2020-08-16 18:35:04
【问题描述】:

嘿,我正在尝试抓取一个网站进行定价。它返回 [],即使在搜索页面上它的价值是 79.99 美元。我只希望它从搜索页面中提取第一个价格。我似乎无法弄清楚我做错了什么。

bburl = "https://www.ebgames.ca/SearchResult/QuickSearch?q=animal+crossing"

def bestbuy():
    proxies = get_proxy()
    result = requests.get(bburl,headers=header,timeout=12,proxies=proxies)
    soup = BeautifulSoup(result.content, 'lxml')

    titles = soup.title
    price = soup.find_all('span',attrs={'class':'megaButton buyTier3 cartAddNoRadio'})

    print(titles)
    print(price)

它返回这个

<title>EB Games | The largest video game retailer in Canada. Play. Trade. Save.  - EBGames.ca </title>
[]

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    megaButton buyTier3 cartAddNoRadio 类位于a 标记中,而不是span。 要仅获取第一个元素,请使用 .find() 而不是 find_all()

    import requests
    from bs4 import BeautifulSoup
    
    bburl = "https://www.ebgames.ca/SearchResult/QuickSearch?q=animal+crossing"
    
    def bestbuy():
        result = requests.get(bburl)
        soup = BeautifulSoup(result.content, 'lxml')
    
        price = soup.find('a',attrs={'class': 'megaButton buyTier3 cartAddNoRadio'})
    
        print(price.get_text(strip=True, separator=' '))
    
    
    bestbuy()
    

    输出:

    New $79.99
    

    【讨论】:

      猜你喜欢
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 2013-01-04
      • 2012-09-09
      • 2019-12-12
      • 2018-05-11
      相关资源
      最近更新 更多