【问题标题】:Using Python to generate URLs with names from txt使用 Python 生成带有 txt 名称的 URL
【发布时间】:2014-05-14 06:24:02
【问题描述】:

我对 Python 还很陌生,所以我希望有人可以帮助我根据文本文件中的信息生成唯一 URL 列表。

示例:我有一个基本 URL,www.website.com/users/,以及一个包含用户名“frank”、“rachel”、“james”等的 txt 文件。我想用这些信息创建 URL ,并将其保存为 txt 文件,如下所示:

www.website.com/users/frank
www.website.com/users/rachel
www.website.com/users/james
etc.

我对数字做了类似的事情,例如

www.website.com/1
www.website.com/2
etc.

我为数字解决方案编写的代码粘贴在下面,以防它作为起点有所帮助。

import time

htmlTxt=""
pageNum=0
x="http://forum.com/eforum/forumdisplay.php?fid=13&page="
y=x+str(pageNum)

file = open("URLs.txt", "wb")
while True:
    try:
        time.sleep(0.001)  # do something here
        file.write(x +str(pageNum)+"\n")
    pageNum+=1

    except KeyboardInterrupt:
        print '\nPausing...  (Hit ENTER to continue, type quit to exit.)'
        try:
            response = raw_input()
            if response == 'quit':
                break
            print 'Resuming...'
        except KeyboardInterrupt:
            print 'Resuming...'
            continue
file.close()

(我在上面的例子中使用'time'的原因是因为我不知道如何让它停在某个数字,所以我只是让它运行了几秒钟并删除了超出的URL “最大”数。)

提前致谢!

【问题讨论】:

  • 你能简化你的问题吗? www.website.com 每次在预期输出中都是同一个站点吗?它从何而来?名称列表是什么样的?文件中每行一个?

标签: python url python-2.7


【解决方案1】:

打开包含名称的文件用于读取,另一个将包含输出 - 用于写入。逐行读取输入文件并写入附加名称的输出:

URL = "www.website.com/users/"

with open('input.txt', 'r') as input_file:
    with open('output.txt', 'w') as output_file:
        for line in input_file:
            output_file.write(URL + line)

对于包含以下内容的input.txt

frank
rachel
james

它产生以下output.txt

www.website.com/users/frank
www.website.com/users/rachel
www.website.com/users/james

【讨论】:

  • 感谢您的帮助!
【解决方案2】:
  • 使用要读取的用户名打开文件并输出要写入的文件。
  • 从用户名文件中读取一行,构造url并将其写入输出文件。

    with open('usernames', 'r') as input_file, open('output', 'a') as output_file:
        for line in input_file:
            url = "http://website.com/{}".format(line.strip())
            output_file.write(url)
    input_file.close()
    output_file.close()
    

【讨论】:

  • .format 更好(更pythonic?)而不是仅仅与+ 连接?
  • @Jasper 文档说它是格式化字符串的新方法。我只是出于可读性的原因更喜欢它。
  • 好的,大体上同意,但如果它位于字符串的末尾,我想我会使用+ 而不是{}
【解决方案3】:

关于“如何让它停在某个数字”: 您可以使用for 循环,该循环通常用于迭代列表:

for i in range(maxnumber):
    # this body is executed maxnumber times and i is 0, 1, ..., maxnumber - 1

【讨论】:

    【解决方案4】:

    这工作正常。

    URL = "www.website.com/users/"
    
    with open('input.txt', 'r') as input_file:
        with open('output.txt', 'w') as output_file:
            for line in input_file:
                output_file.write(URL + line)
    

    您能否更新以下代码。因为此代码输出将转到单独的行。

    URL = "www.website.com/users/"
    URI_PART = "/set/passwd"
    
    with open('input.txt', 'r') as input_file:
        with open('output.txt', 'w') as output_file:
            for line in input_file:
                new_url = URL + line + URI_PATH
                print(new_url)
    

    然后输出 URL 将分成 2 行。 你对此有什么想法吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      相关资源
      最近更新 更多