Home > Net >  How to iterate a list and replace variable with string?
How to iterate a list and replace variable with string?

Time:08-30

I am having trouble with looping through a list and replacing a variable with the string values in the list.

cate_list = [
    "https://myaeon2go.com/products/category/1208101/fresh-foods",
    "https://myaeon2go.com/products/category/8630656/ready-to-eat",
    "https://myaeon2go.com/products/category/6528959/grocery",
    "https://myaeon2go.com/products/category/6758871/snacks",
    "https://myaeon2go.com/products/category/8124135/chill-&-frozen",
    "https://myaeon2go.com/products/category/4995043/beverage",
    "https://myaeon2go.com/products/category/3405538/household",
    "https://myaeon2go.com/products/category/493239/baby-&-kids",
    "https://myaeon2go.com/products/category/8480430/health-&-wellness",
    "https://myaeon2go.com/products/category/528110/non-halal",
    "https://myaeon2go.com/products/category/6168158/personal-care",
    "https://myaeon2go.com/products/category/10000016/beauty",
    "https://myaeon2go.com/products/category/5741281/pets",
    "https://myaeon2go.com/products/category/6040602/festive",
    "https://myaeon2go.com/products/category/6820054/others",
]

for x in range(len(cate_list)):

    url = cate_list

    cookies = {
        "hideLocationOverlay": "true",
        "selectedShippingState": "Pulau Pinang",
        "selectedPostalCode": "14000",
    }

    # convert soup to readable html
    result = requests.get(url, cookies=cookies)
    doc = BeautifulSoup(result.text, "html.parser")

I am trying to replace url with the list in the form of a string every time it loops.

url is sepose to be:

url = "https://myaeon2go.com/products/category/6820054/others"

the issue is that "result" is unable to read the url.

CodePudding user response:

In the line url = cate_list you forgot to include the position of the element you want... so it is running the full list all the time, you can fix it by doing:

url = cate_list[x]

CodePudding user response:

I concur with @guin0x answer. I also think that you could alter your code to look like this to improve on its simplicity and efficiency

cate_list = [
    "https://myaeon2go.com/products/category/1208101/fresh-foods",
    "https://myaeon2go.com/products/category/8630656/ready-to-eat",
    "https://myaeon2go.com/products/category/6528959/grocery",
    "https://myaeon2go.com/products/category/6758871/snacks",
    "https://myaeon2go.com/products/category/8124135/chill-&-frozen",
    "https://myaeon2go.com/products/category/4995043/beverage",
    "https://myaeon2go.com/products/category/3405538/household",
    "https://myaeon2go.com/products/category/493239/baby-&-kids",
    "https://myaeon2go.com/products/category/8480430/health-&-wellness",
    "https://myaeon2go.com/products/category/528110/non-halal",
    "https://myaeon2go.com/products/category/6168158/personal-care",
    "https://myaeon2go.com/products/category/10000016/beauty",
    "https://myaeon2go.com/products/category/5741281/pets",
    "https://myaeon2go.com/products/category/6040602/festive",
    "https://myaeon2go.com/products/category/6820054/others",
]

cookies = {
        "hideLocationOverlay": "true",
        "selectedShippingState": "Pulau Pinang",
        "selectedPostalCode": "14000",
    }

for url in cate_list:
    # convert soup to readable html
    result = requests.get(url, cookies=cookies)
    doc = BeautifulSoup(result.text, "html.parser")

  • Related