Python enumerate for statement question

Asked 2 years ago, Updated 2 years ago, 17 views

Hello, I'm parsing NAVER shopping products When I searched for the product first, I have parsed the number of pages and the name of the url shopping item And I wanted to see what rank I was in I used enumerate to open it up Like that, until the last product on page 1, you should list the ranking well But if you move on to the next page, you'll start again by going straight away Is there a way to continue the number without starting with 1?

 for i in range(1,len(Pageno)+1):
    url = f'https://ad.search.naver.com/search.naver?where=ad&query= 
   {searchName}&x=0&y=0&pagingIndex={i}'
    response = requests.get(url)
    html = response.text
    soup = BeautifulSoup(html, 'html.parser')
    urllist = soup.findAll('a', {"class": "url"})
    namelist = soup.findAll('a', {"class": "lnk_tit"})
    for i,(a,b) in enumerate(zip(urllist,namelist), start = 1):
      print(i,a.get_text()+" / ",b.get_text())



       It's too long if I start from number 1, so I'll start from number 22 
       >>> Results
        22 http://www.officehana.com / No. 1 Office Hana General Shopping Mall
        23 http://ecogift.co.kr / Promotional Souvenir Discount Eco Gift
        24 http://www.attyfactory.co.kr / Super Special Price Production Artifactory
        25 http://www.giftms.com / File Specialized Company MSGift
        1 http://www.navimro.com / Business file Navi MRO <<< initialize to 1 when crossing the page 
        2 http://www.hit87.com / File! Hit promotional material
        3 http://www.gmarket.co.kr / Gmarket File
        4 http://giftoutlet.kr / Gift outlet specializing in production of promotional materials


        >>> Results You Want 
        22 http://www.officehana.com / No. 1 Office Hana General Shopping Mall
        23 http://ecogift.co.kr / Promotional Souvenir Discount Eco Gift
        24 http://www.attyfactory.co.kr / Super Special Price Production Artifactory
        25 http://www.giftms.com / File Specialized Company MSGift
        26 http://www.navimro.com / Business file Navi MRO
        27 http://www.hit87.com / File! Hit Promotion
        28 http://www.gmarket.co.kr / Gmarket File
        29 http://giftoutlet.kr / Gift Outlet specializing in production of promotional materials

Thank you for your answer

python

2022-09-20 11:06

1 Answers

The name list is initialized per loop. Like this

# Loop 1
namelist = [Shopping mall 1, shopping mall 2,...]

# Loop 2
namelist = [Shopping mall 12, shopping mall 13,...]

If the order is simply a problem, you can print it out at once after the collection.

list_name = []
list_url = []
For i in loop:
    namelist = collection code
    list_name += namelist
    lit_url += urllist

for i, (a, b) in enumerate(zip(list_url, list_name), start = 1):
    print(i, a, b)


2022-09-20 11:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.