简体   繁体   English

将文件中的参数添加到python中的给定URL

[英]Add params from file to given URL in python

I would like to generate a series of urls using given urls plus some params in a txt file. 我想使用给定的URL加上txt文件中的一些参数来生成一系列URL。

For example, the given urls are: 例如,给定的URL为:

http://stackoverflow.com/questions/ask1
http://stackoverflow.com/questions/ask2

They are stored in A1, A2, respectively, in Sheet1 in url.xlsx file. 它们分别存储在url.xlsx文件的Sheet1中的A1,A2中。

The params are stored in params.txt file, with following content: 这些参数存储在params.txt文件中,其内容如下:

w3e
1
123456
fy

I would like to generate urls like following: 我想生成如下网址:

http://stackoverflow.com/questions/ask1/param.x
http://stackoverflow.com/questions/ask2/param.x

This means I will get 2x4=8 urls. 这意味着我将获得2x4 = 8个网址。

Any idea how to make this work? 任何想法如何使这项工作? Many thanks! 非常感谢!

Read both files into lists, then use itertools.product() to combine them: 将两个文件读入列表,然后使用itertools.product()组合它们:

from itertools import product

with open('urls.txt') as urlfile:
    urls = [line.strip() for line in urlfile]

with open('params.txt') as paramfile: parameters = [line.strip() for line in paramfile] 使用open('params.txt')作为paramfile:parameters = [line.strip()for paramfile中的行]

for url, param in product(urls, parameters):
    print url + param

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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