Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关键字/属性的漂亮汤过滤(python)_Python_Web Scraping_Beautifulsoup_Filtering - Fatal编程技术网

关键字/属性的漂亮汤过滤(python)

关键字/属性的漂亮汤过滤(python),python,web-scraping,beautifulsoup,filtering,Python,Web Scraping,Beautifulsoup,Filtering,我想用漂亮的汤和请求刮取网站的数据,我几乎得到了我想要的,但我找不到过滤最后步骤的方法: 这是我的密码: variants = soup.find('div', class_='product-configure') print(variants) 这将打印以下内容: <div class="product-configure"> <select id="custom-variants"> <option disabled="disabled" selected=

我想用漂亮的汤和请求刮取网站的数据,我几乎得到了我想要的,但我找不到过滤最后步骤的方法:

这是我的密码:

variants = soup.find('div', class_='product-configure')
print(variants)
这将打印以下内容:

<div class="product-configure">
<select id="custom-variants">
<option disabled="disabled" selected="selected">Maak een keuze</option>
<option data-status="available" data-value="177379037">Size : EU 40.5 (US 7) 
</option>
<option data-status="available" data-value="177379043">Size : EU 41.5 (US 8) 
</option>
<option data-status="available" data-value="177379223">Size : EU 42.5 (US 9) 
</option>
</select>
</div>

马钦久泽
规模:欧盟40.5(美国7)
规模:欧盟41.5(美国8)
规模:欧盟42.5(美国9)
我如何过滤它,使其只打印“数据值”中的所有数字? (例如“177379037”作为第一行的输出)

以下是HTML:

          <div class="gui-select">
                  <div class="product-configure">
                        <select id="custom-variants">
            <option selected="selected" disabled="disabled">Select an option</option>
                            <option data-value="177379037" data-status="available">Size : EU 40.5 (US 7)</option>
                            <option data-value="177379043" data-status="available">Size : EU 41.5 (US 8)</option>
                            <option data-value="177379223" data-status="available">Size : EU 42.5 (US 9)</option>


        </div>

选择一个选项
规模:欧盟40.5(美国7)
规模:欧盟41.5(美国8)
规模:欧盟42.5(美国9)
您可以使用soup.find_all()并使用带有属性的dict

options = soup.find_all("option", {"data-value": True})
结果:

for o in options:
    print(o.attrs["data-value"])


使用
find_all
获取所有
标记,然后打印
数据值
属性。请注意,并非所有的
选项
标记都具有该属性,因此我在其中放入了一个
try
,以便在没有找到
数据值时继续

html = '''         <div class="gui-select">
                  <div class="product-configure">
                        <select id="custom-variants">
            <option selected="selected" disabled="disabled">Select an option</option>
                            <option data-value="177379037" data-status="available">Size : EU 40.5 (US 7)</option>
                            <option data-value="177379043" data-status="available">Size : EU 41.5 (US 8)</option>
                            <option data-value="177379223" data-status="available">Size : EU 42.5 (US 9)</option>


        </div>'''

import bs4

soup = bs4.BeautifulSoup(html, 'html.parser')

elem = soup.find_all('option')
for each in elem:
    try:
        print (each['data-value'])
    except:
        continue

您可以将类与属性css选择器一起使用

items = [item['data-value'] for item in soup.select('.product-configure [data-value]')]

如果只有一个类要考虑,可以使用ID作为选择< /p>

items = [item['data-value'] for item in soup.select('#custom-variants [data-value]')]

可能是我要找的东西的复制品,谢谢。你也知道我如何过滤掉以“Size:”开头的纯文本吗?因为它不是一个属性,所以我使用XPATH,在美化组对象之前必须使用,请查看一些示例。我为您的问题提供了一个xpath示例:xpath_optionns://options[@data value and contains(text(),'Size:')”
items = [item['data-value'] for item in soup.select('.product-configure [data-value]')]
items = [item['data-value'] for item in soup.select('#custom-variants [data-value]')]