【问题标题】:Scraping dropdown menu values by Python BeautifulSoup通过 Python BeautifulSoup 抓取下拉菜单值
【发布时间】:2021-09-13 02:00:07
【问题描述】:

我查看了大部分帖子,但没有找到我的小问题的回复。

这是我要抓取的下拉菜单:

<div class="input-box">
    <select name="super_attribute[138]" id="attribute138" class="required-entry super-attribute select form-control" onchange="notifyMe(this.value, this.options[this.selectedIndex].innerHTML);">
        <option value="">Choose an Option...</option>
        <option value="17" price="0">M (in stock) </option>
        <option value="18" price="0">L (out of stock) </option>
        <option value="15" price="0">XL (in stock) </option>
        <option value="52" price="0">XXL (in stock) </option>
    </select>
</div>

我的 Python 代码是:

items = soup.select('option[value]')
values = [item.get('value') for item in items]
textvalues = [item.text for item in items]

print(textvalues)

输出是: ['select', '(In-Stock)', '(Out-Stock)', '(In-Stock)', '(In-Stock)']

我的要求是我还需要其他值(SizeValue 和 SizeName): 17 & M / 18 & L / 15 & XL / 52 & XXL

如果我删除了 .text ,我有这个输出:

   <option value="">select</option>, <option value="200@#-(In-Stock)@#-https://store.alsabihmarine.com/index.php/diving-equipments/wetsuits/camouflage-hooded-suits-220.html@#-">(In-Stock)</option>, <option value="201@#-(Out-Stock)@#-https://store.alsabihmarine.com/index.php/diving-equipments/wetsuits/camouflage-hooded-suits-220.html@#-">(Out-Stock)</option>, <option value="202@#-(In-Stock)@#-https://store.alsabihmarine.com/index.php/diving-equipments/wetsuits/camouflage-hooded-suits-220.html@#-">(In-Stock)</option>, <option value="203@#-(In-Stock)@#-https://store.alsabihmarine.com/index.php/diving-equipments/wetsuits/camouflage-hooded-suits-220.html@#-">(In-Stock)</option>

提前感谢您的帮助。

【问题讨论】:

    标签: python beautifulsoup combobox dropdown screen-scraping


    【解决方案1】:

    这很简单,只需在您的列表理解中添加一个+ 并调用item.text。

    代替:

    values = [item.get('value') for item in items]
    

    使用:

    values = [item.get('value') + item.get_text(strip=True) for item in items[1:]]
    print(values)
    

    编辑:数据是动态加载的,所以requests 不支持。但是网站上的数据以 JSON 格式提供。您可以使用 re 模块通过正则表达式提取它:

    import json
    import re
    import requests
    
    
    url = "https://store.alsabihmarine.com/index.php/diving-equipments/wetsuits/camouflage-hooded-suits-220.html"
    response = requests.get(url).content
    
    regex_pattern = re.compile(r"Product\.Config\(({.*?})\);")
    data = json.loads(regex_pattern.search(str(response)).group(1))
    
    print(
        [
            product["id"] + product["label"]
            for product in data["attributes"]["138"]["options"]
        ]
    )
    

    输出:

    ['17M (in stock) ', '18L (out of stock) ', '15XL (in stock) ', '52XXL (in stock) ']
    

    【讨论】:

    • 感谢您的回复。是的,我试过了,但输出的问题如下:
    • @TalalIsmail URL 是什么?也不要发布诸如答案之类的内容,而是作为评论发布。 (就像你刚刚做的那样)
    • 好吧对不起,这是我第一次来堆栈溢出。
    • 非常感谢,我现在明白它是动态值。效果很好。
    【解决方案2】:

    感谢您的回复。 是的,我试过了,但输出的问题如下:

    ['200@#-(In-Stock)@#-https://store.alsabihmarine.com/index.php/diving-equipments/wetsuits/camouflage-hooded-suits-220.html@#-(In-Stock)', '201@#-(Out-Stock)@#-https://store.alsabihmarine.com/index.php/diving-equipments/wetsuits/camouflage-hooded-suits-220.html@#-(Out-Stock)', '202@#-(In-Stock)@#-https://store.alsabihmarine.com/index.php/diving-equipments/wetsuits/camouflage-hooded-suits-220.html@#-(In-Stock)', '203@#-(In-Stock)@#-https://store.alsabihmarine.com/index.php/diving-equipments/wetsuits/camouflage-hooded-suits-220.html@#-(In-Stock)']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2020-02-06
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      相关资源
      最近更新 更多