如何将for循环中的url保存到单个变量中?

问题描述 投票:1回答:1

我想将多个网址存储到单个变量“网址”中。这些网址由“urlp1”,“n”和“urlp2”三部分组成,您可以在下面的代码中看到这些部分。

urlp1 = "https://www.proteinatlas.org/" 

URLs = []
for cancer in cancer_list:

    urlp2 = "/pathology/tissue/" + cancer[1]
    f = cancer[0]

    try:
        df1 = pd.read_csv(rootPath + f + ".csv", header=0);
    except Exception as e:
        print("File " + f + " doesn't exist")
        print(str(e))
        sys.exit()

    gene_matrix = df1.as_matrix()
    number_matrix = gene_matrix[:,0]
    gene_cancer = number_matrix.tolist()
    # print(gene_cancer)
    np_clean = [x for x in gene_cancer if str(x) != 'nan']


    for n in np_clean:

        URL = urlp1 + n + urlp2
        URLs.append(URL)
        #print(URLs)

那么如何将所有输出存储到URL = [“URL1”,“URL2”,“URL3”,...]?

感谢帮助。

python for-loop web-scraping beautifulsoup
1个回答
0
投票

对不起,小错误。只需调用外面的print(URL)for循环即可解决。

© www.soinside.com 2019 - 2024. All rights reserved.