简体   繁体   中英

Loop URL list and save as txt file

I have a URL as follows: https://www.vq.com/36851082/?p=1 . I want to create a file named list_of_urls.txt which contains url links from p=1 to p=20 , seperate each with space, and save it as a txt file.

Here is what I have tried, but it only prints the last one:

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)

The expected txt file inside would be like this: 在此处输入图片说明

It is the occasion to use f-strings , usable since Python 3.6, and fully described in 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))

Notice the space character at the end of each formatting expression.

Try this :)

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)

Not sure if you want one line inside your file but if so:

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)]))

Output:

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

Be careful with range - it starts from from 0 by default and the last number of the range is not included. Hence, if you want numbers 1 - 20 you need to use 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))

This one also work, thanks to my colleague!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM