如何创建下面这样的列表?

2024-05-12 21:37:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个URL的系列,我需要复制如下

https://wipp.edmundsassoc.com/Wipp/?wippid=*1205*

1205是可变的最终输出需要看起来像

"https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage1"
................................................#taxpage2"
................................................#taxpage3
................................................#taxpage4

等等。我有一个没有"#taxpage"部分的URL列表,以及每个页面应该有多少个税页的列表。我想为每个URL生成所有可能页面的列表。感谢您的帮助…对编码完全陌生,非常感谢您的帮助


Tags: httpscomurl列表页面wippedmundsassocwippid
2条回答

您可以使用列表理解:

In [1]: urls = ['https://wipp.edmundsassoc.com/Wipp/?wippid=1205',
                'https://wipp.edmundsassoc.com/Wipp/?wippid=1206']

In [2]: ["{}#taxpage{}".format(url, page_num) for page_num in xrange(1, 4) for url in urls]
Out[2]: 
['https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage1',
 'https://wipp.edmundsassoc.com/Wipp/?wippid=1206#taxpage1',
 'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage2',
 'https://wipp.edmundsassoc.com/Wipp/?wippid=1206#taxpage2',
 'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage3',
 'https://wipp.edmundsassoc.com/Wipp/?wippid=1206#taxpage3']

您可以使用str.format在列表中添加#taxpage数字

>>> s = r'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage{}'
>>> [s.format(i) for i in range(1, 5)]
    ['https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage1',
     'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage2',
     'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage3',
     'https://wipp.edmundsassoc.com/Wipp/?wippid=1205#taxpage4']

相关问题 更多 >