循环访问URL列表并另存为txt文件

时间:2019-01-31 08:35:29

标签: python

我的网址如下:https://www.vq.com/36851082/?p=1。我想创建一个名为list_of_urls.txt的文件,其中包含从p=1p=20的url链接,用空格分隔每个链接,并将其另存为txt文件。

这是我尝试过的方法,但只显示最后一个:

url = "https://www.vq.com/36851082/?p="
list_of_urls = []
for page in range(20):
    list_of_urls = url + str(page)
print(list_of_urls)

其中的预期txt文件如下所示: enter image description here

5 个答案:

答案 0 :(得分:2)

这是使用 f-strings 的机会,该字符串自Python 3.6起可用,并在PEP 498 -- Literal String Interpolation中进行了详细说明。

url_base = "https://www.vq.com/36851082/?p="

with open('your.txt', 'w') as f:
    for page in range(1, 20 + 1):
        f.write(f'{url_base}{page} ')
       #f.write('{}{} '.format(url_base, page))
       #f.write('{0}{1} '.format(url_base, page))
       #f.write('{u}{p} '.format(u=url_base, p=page))
       #f.write('{u}{p} '.format(**{'u':url_base, 'p':page}))
       #f.write('%s%s '%(url_base, page))

在每个格式表达式的末尾注意空格字符

答案 1 :(得分:1)

尝试一下:)

url = "https://www.vq.com/36851082/?p="
list_of_urls = ""
for page in range(20):
    list_of_urls = list_of_urls + url + str(page) + " "
print(list_of_urls)

答案 2 :(得分:1)

不确定是否要在文件中插入一行,但如果这样:

url = "https://www.vq.com/36851082/?p=%i"
with open("expected.txt", "w") as f:
    f.write(' '.join([url %i for i in range(1,21)]))

输出:

https://www.vq.com/36851082/?p=1 https://www.vq.com/36851082/?p=2 https://www.vq.com/36851082/?p=3 https://www.vq.com/36851082/?p=4 https://www.vq.com/36851082/?p=5 https://www.vq.com/36851082/?p=6 https://www.vq.com/36851082/?p=7 https://www.vq.com/36851082/?p=8 https://www.vq.com/36851082/?p=9 https://www.vq.com/36851082/?p=10 https://www.vq.com/36851082/?p=11 https://www.vq.com/36851082/?p=12 https://www.vq.com/36851082/?p=13 https://www.vq.com/36851082/?p=14 https://www.vq.com/36851082/?p=15 https://www.vq.com/36851082/?p=16 https://www.vq.com/36851082/?p=17 https://www.vq.com/36851082/?p=18 https://www.vq.com/36851082/?p=19 https://www.vq.com/36851082/?p=20

答案 3 :(得分:1)

请注意range-默认情况下它从0开始,并且不包括范围的最后一个数字。因此,如果您想要数字1 - 20,则需要使用range(1, 21)

url_template = "https://www.vq.com/36851082/?p={page}"
urls = [url_template.format(page=page) for page in range(1, 21)]

with open("/tmp/urls.txt", "w") as f:
    f.write(" ".join(urls))

答案 4 :(得分:0)

感谢我的同事,这也可以!

url = "https://www.vq.com/36851082/?p=%d"
result = " ".join([ url % (x + 1) for x in range(20)])
with open("list_of_urls.txt", "w") as f:
    f.write(result)