用python进行bs4过滤

用python进行bs4过滤,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我正在尝试编写一个脚本来检查steam store,但是我在过滤掉所有代码中没有折扣的列表时遇到了问题。我只想保留带有span标记和-percentage的列表,而不是没有span标记的列表。这是我的密码: from urllib.request import urlopen from datetime import date import requests as rq inp = str(input('what would you like to search up?')) w = ('ht

我正在尝试编写一个脚本来检查steam store,但是我在过滤掉所有代码中没有折扣的列表时遇到了问题。我只想保留带有span标记和
-percentage
的列表,而不是没有span标记的列表。这是我的密码:

from urllib.request import urlopen
from datetime import date
import requests as rq

inp = str(input('what would you like to search up?'))
w = ('https://store.steampowered.com/search/?term=' + inp)
page = rq.get(w)
soup = bsoup(page.content, 'html.parser')
soup.prettify()
sales = soup.find_all('div', class_="responsive_search_name_combined")

for sale in sales:
    p = soup.find('div', class_="col search_price responsive_secondrow")
    d = soup.find_all('div', class_="col search_discount responsive_secondrow")
    n = soup.find('span', class_="title")

    if None in (d, n, p):
        continue
    print(d)

以及输出(包含我想要过滤掉的东西/我想要保留的东西)

-16%
, 
, 
-19%
, 
, 
, 
, 
, 
, 
, 
, 
, 
, 
, 
, 
, 
, 
, 
等等。 我试着用
d=soup.find_all('div',class=“col search_折扣响应的第二行”)
替换
d=soup.find_all('span',string=“-16%”)
,看看这是否可行,但没有成功。 我想保留span标记,但不保留div标记
有人能帮忙吗?

您只需在最后一个
for
循环中添加一个
try except
块,即可解决您的问题。以下是完整的代码:

from urllib.request import urlopen
from datetime import date
import requests as rq
from bs4 import BeautifulSoup as bsoup
inp = str(input('what would you like to search up?'))
w = ('https://store.steampowered.com/search/?term=' + inp)
page = rq.get(w)
soup = bsoup(page.content, 'html.parser')
soup.prettify()
sales = soup.find_all('div', class_="responsive_search_name_combined")

final = []

for sale in sales:
    p = soup.find('div', class_="col search_price responsive_secondrow")
    d = soup.find_all('div', class_="col search_discount responsive_secondrow")
    n = soup.find('span', class_="title")

    try:
        for element in d:
            span = element.span
            if span:
                final.append(span.text)
    except:
        pass
print(final)
输出:

what would you like to search up?>? among us
['-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%']

你的产出是多少?问题是什么?这并不是说它不起作用,而是当我在最后运行print(d)时,我得到的结果是,-80%,,,,我想过滤结果,这样它只显示这个div中的span标记。我尝试使用其他变量来更具体些,或者尝试直接访问span标记,但没有成功。我可能做错了,但即使在搜索了一段时间后我也不知道该怎么做,因为不清楚您期望的输出和您现在得到的输出。这不是代码错误,但我不知道如何实现某些东西,我所做的研究到目前为止对我没有帮助。我想用一个百分比过滤掉所有不包含span标记的div标记,这样我只剩下一个百分比。我刚刚尝试过,但我似乎仍然收到很多没有span包含百分比的空div标记。我想这可能是因为我在做soup.find_all,但我不确定我是否可以使用soup.find而不是soup.find_all,如果没有包含百分比的span标记,请跳到下一个列表,但我不确定我会怎么做这看起来很奇怪。我仍然在我的控制台上打印了很多空标签我想可能是d在查找所有这些标签,(因为我使用了soup.find_all),然后当它附加时,一旦找到一个span标签,它也会附加其中的所有空标签。哦…顺便说一句,你只是想将span标签附加到最终列表中吗?
what would you like to search up?>? among us
['-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%', '-10%', '-25%']