【问题标题】:Curling in Python and Outputting into Individual Text Files在 Python 中卷曲并输出到单个文本文件中
【发布时间】:2017-02-23 21:31:50
【问题描述】:

您如何从 .t​​xt 文件中读取包含 3 个 URL 的多行,“卷曲”它们以提取 HTML,并将它们全部输出到 python 中它们自己的单独文件中? 我试过了-

    import commands
    import os
    import json

    # RAW DATA
    input = open('uri.txt', 'r')
    t_1 = open('command', 'w')
    counter_1 = 0

    for line in input:
          counter_1 += 1
    if counter_1 < 3:
          filename = str(counter_1)
          print str(line)
    filename= str(count)
    command ='curl ' + '"' + str(line).rstrip('\n') + '"'+ '> ./rawData/' + filename

    output_1 = commands.getoutput(command)
    input.close()

当我在 PyCharm 中运行我的代码时,它会在我的目录中创建一个名为“filename.txt”的空白文件

【问题讨论】:

  • StackOverflow 不是雇佣编码员服务。你试过什么了?你对你的问题做过任何研究吗?您是否遇到过特定的障碍?请将所有这些信息添加到您的问题中。
  • @KernelPanic 我已经尝试了一些东西,我在上一个问题中发布了我的代码。我会在这里添加它
  • 雇佣编码员哈哈
  • 请确保在包含代码时明确说明您遇到问题的确切位置。 “这是一段代码,由于某种原因它不起作用”似乎总是一个好问题,但阅读它的人不知道在您的代码上下文中“它不起作用”是什么意思。
  • @KernelPanic 我已经包含了我的代码。现在指定问题。

标签: python html url curl


【解决方案1】:

您应该在 python 中使用 requests 而不是 cURL,因为这将使处理响应更加容易。此外,处理文件的规范方法是使用上下文管理器:with open(filename) as f

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    相关资源
    最近更新 更多