1

Hey I need to scrape an html for a specific shipping rate here's the HTML:

<ul class="methods" id="shipping-quotes" data-url="/en/webshipper/setshippingquote" data-shipping-fee="£ 12">
                                                                <li class="method-option method-option--shipping">
            <div class="method-option__header">
                <input
                    id="shippingrate-8"
                    name="shippingrate"
                    type="radio"
                    value="8"
                    class="method-option__input shipping-rate"
                    
                />
                <label for="shippingrate-8" class="method-option__label">
                    <span class="method-option__name">UPS Standard UK - Home Delivery (2-4 business days)</span>
                    <span class="method-option__fee">9 GBP</span>
                </label>
            </div>
                        </li>
                                                                <li class="method-option method-option--shipping">
            <div class="method-option__header">
                <input
                    id="shippingrate-9"
                    name="shippingrate"
                    type="radio"
                    value="9"
                    class="method-option__input shipping-rate"
                    
                />
                <label for="shippingrate-9" class="method-option__label">
                    <span class="method-option__name">UPS Standard UK - Pick-up Point (2-4 business days)</span>
                    <span class="method-option__fee">6 GBP</span>
                </label>
            </div>
                        </li>
                                                                <li class="method-option method-option--shipping">
            <div class="method-option__header">
                <input
                    id="shippingrate-15"
                    name="shippingrate"
                    type="radio"
                    value="15"
                    class="method-option__input shipping-rate"
                    
                />
                <label for="shippingrate-15" class="method-option__label">
                    <span class="method-option__name">UPS Express UK - Home Delivery (2-4 business days)</span>
                    <span class="method-option__fee">15 GBP</span>
                </label>
            </div>
                        </li>
                                                                <li class="method-option method-option--shipping">
            <div class="method-option__header">
                <input
                    id="shippingrate-29"
                    name="shippingrate"
                    type="radio"
                    value="29"
                    class="method-option__input shipping-rate"
                    checked
                />
                <label for="shippingrate-29" class="method-option__label">
                    <span class="method-option__name">UPS Express UK - Pick-up Point (1-3 business days)</span>
                    <span class="method-option__fee">12 GBP</span>
                </label>
            </div>
                                
<div class="drop-points" id="drop-points">
    <p class="drop-point drop-point--no-result">
        No pickup points found near your chosen location
    </p>
</div>
                        </li>
        </ul>

Specifically the home delivery option so in the HTML below I would need to get the value= 9 value so I would like to get the value of 9. How could I get the specific value for the home delivery option using bs4?

If I am unclear i mean the value option within this

<input
                    id="shippingrate-9"
                    name="shippingrate"
                    type="radio"
                    value="9"
                    class="method-option__input shipping-rate"

                />

For the home delivery 2-4 days option.

4
  • 1
    As discussed here, you should be able to use something like soup.find('input', {'name': 'shippingrate'}).attrs['value']. Commented Mar 8, 2021 at 17:13
  • that gets the first value, how do I get the shipping rate value for the "home delivery" option? Commented Mar 8, 2021 at 18:37
  • Use soup.find_all and a loop. You need to read the docs as you seem to lack the basics and are trying to run before you can walk. Commented Mar 8, 2021 at 18:41
  • i understand find all but how can I search for the value in between a class within a label? Commented Mar 8, 2021 at 18:45

2 Answers 2

1

If you want to get the input value of shipping method (let's say UPS Standard UK - Pick-up Point (2-4 business days) in your case), you can first find all divs then pick the one with this method. So something like this will do the job:

data = soup.find('ul', attrs={'id': 'shipping-quotes'}).find_all('div', class_='method-option__header')

values = [each.input['value'] for each in data if each.find('span', class_='method-option__name').text == 'UPS Standard UK - Pick-up Point (2-4 business days)']

values will be:

['9']
Sign up to request clarification or add additional context in comments.

2 Comments

hey how could i get the value of 9 not the shipping method for the value of 9. SO what I mean is I want to get the value number for the home delivery option
@MovieClips edited, check if this is what you want
1

You've not shown any effort at all but since I needed a break...

from bs4 import BeautifulSoup

data = '''\
<ul class="methods" id="shipping-quotes" data-url="/en/webshipper/setshippingquote" data-shipping-fee="£ 12">
  <li class="method-option method-option--shipping">
    <div class="method-option__header">
    <input id="shippingrate-8" name="shippingrate" type="radio" value="8" class="method-option__input shipping-rate" /> 
    <label for="shippingrate-8" class="method-option__label">
    <span class="method-option__name">UPS Standard UK - Home Delivery (2-4 business days)</span> 
    <span class="method-option__fee">9 GBP</span></label></div>
  </li>
  <li class="method-option method-option--shipping">
    <div class="method-option__header">
    <input id="shippingrate-9" name="shippingrate" type="radio" value="9" class="method-option__input shipping-rate" /> 
    <label for="shippingrate-9" class="method-option__label">
    <span class="method-option__name">UPS Standard UK - Pick-up Point (2-4 business days)</span> 
    <span class="method-option__fee">6 GBP</span></label></div>
  </li>
  <li class="method-option method-option--shipping">
    <div class="method-option__header">
    <input id="shippingrate-15" name="shippingrate" type="radio" value="15" class="method-option__input shipping-rate" /> 
    <label for="shippingrate-15" class="method-option__label">
    <span class="method-option__name">UPS Express UK - Home Delivery (2-4 business days)</span> 
    <span class="method-option__fee">15 GBP</span></label></div>
  </li>
  <li class="method-option method-option--shipping">
    <div class="method-option__header">
    <input id="shippingrate-29" name="shippingrate" type="radio" value="29" class="method-option__input shipping-rate"
    checked="checked" /> 
    <label for="shippingrate-29" class="method-option__label">
    <span class="method-option__name">UPS Express UK - Pick-up Point (1-3 business days)</span> 
    <span class="method-option__fee">12 GBP</span></label></div>
    <div class="drop-points" id="drop-points">
      <p class="drop-point drop-point--no-result">No pickup points found near your chosen location</p>
    </div>
  </li>
</ul>
'''

soup = BeautifulSoup(data, 'html.parser')
for d in soup.select('div.method-option__header'):
    rate = d.find('input', {'name': 'shippingrate'}).attrs['value']
    name = d.find('span', {'class': 'method-option__name'}).text
    fee = d.find('span', {'class': 'method-option__fee'}).text
    print(f"{rate}\t{name}\t{fee}")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.