如何将列表中的每个值附加到 url 并每次生成新的 url

我正在尝试调用列表中的每个值并将其附加到 url 并每次为附加值生成 url


这是我的代码


    myList = ['10026','10067','10093','10117','10132','10133','10464','10524','10654','10657','10658','10701','10809','10966','11153','11173','11327','11453','11470','11478','11488','11490','12733','12750','12754','12785','13053','13683','13895','14347','14420','14438','14452','14453','14457','14460','14755','16913','17460','17497','17500','17502','18007','18013','18058','18265','18711','18778','18779','18782','18913','19790','20105','20363','20609','20766','20770','21212','21216','21262','21471','21520','21555','21833','21946','21961','22115','22203','22994','23457','23800','24260','24590','25155','25649','25749','26021','26022']


    for i in myList:

        unspsc_link = f"https://order.besse.com/Orders/Search/ProductSearch?query={i}"

它只取最后一个值


      unspsc_link

    'https://order.besse.com/Orders/Search/ProductSearch?query=26022'


慕运维8079593
浏览 95回答 2
2回答

缥缈止盈

你可以这样做my_list = ['10026','10067','10093','10117','10132','10133','10464','10524','10654','10657','10658','10701','10809','10966','11153','11173','11327','11453','11470','11478','11488','11490','12733','12750','12754','12785','13053','13683','13895','14347','14420','14438','14452','14453','14457','14460','14755','16913','17460','17497','17500','17502','18007','18013','18058','18265','18711','18778','18779','18782','18913','19790','20105','20363','20609','20766','20770','21212','21216','21262','21471','21520','21555','21833','21946','21961','22115','22203','22994','23457','23800','24260','24590','25155','25649','25749','26021','26022']unspsc_links = [f"https://order.besse.com/Orders/Search/ProductSearch?query={i}" for i in my_list]

慕哥6287543

由于您只有一个名为 的变量,unspsc_link因此对于循环的每次迭代,它都会被覆盖。相反,您可以使用生成的 URL 创建一个新列表:myList = ['10026','10067','10093','10117','10132','10133','10464','10524','10654','10657','10658','10701','10809','10966','11153','11173','11327','11453','11470','11478','11488','11490','12733','12750','12754','12785','13053','13683','13895','14347','14420','14438','14452','14453','14457','14460','14755','16913','17460','17497','17500','17502','18007','18013','18058','18265','18711','18778','18779','18782','18913','19790','20105','20363','20609','20766','20770','21212','21216','21262','21471','21520','21555','21833','21946','21961','22115','22203','22994','23457','23800','24260','24590','25155','25649','25749','26021','26022']unspsc_link = []for i in myList:    unspsc_link.append(f"https://order.besse.com/Orders/Search/ProductSearch?query={i}")这将生成一个包含所有所需 URL 的列表。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python