美文网首页Python三期爬虫作业
【Python爬虫】-【第二周】01-作业

【Python爬虫】-【第二周】01-作业

作者: 奔跑的Kay | 来源:发表于2017-07-18 17:02 被阅读67次
    1. 习题13 参数、解包、变量
      代码:
    # 习题13 参数、解包、变量
    from sys import argv # 从sys模块/模组/库中导入argv函数(参数变量)
    # 将argv解包,与其将所有参数放到同一个变量下面,并将每一个参数赋予一个变量名,script
    script, first, second, third = argv # 将argv中的数据解包,将所有的参数依次赋予左边的变量
    # 命令行运行是,以下列参数运行 python ex13.py first 2nd 3rd,脚本的名称总是以ex13.py为sys.argv列表的第一个参数。
    # 打印各个参数
    print("The script is called:", script)
    print("Your first variable is:", first)
    print("Your second variable is:", second)
    print("Your third variable is:", third)
    

    加分习题:
    13.3 将raw_input和argv一起使用,让你的脚本从用户的手上得到更多的输入。
    代码:

    from sys import argv # 从sys模块/模组/库中导入argv函数(参数变量)
    script_name, name, gender, age, location = argv # 将argv中的数据解包,将所有的参数依次赋予左边的变量
    prompt = "Please input your information:\n" # 输入提示1
    name = input(prompt + "your name: ") # 用户输入1
    gender = input(prompt + "your gender: ") # 用户输入2
    age = input(prompt + "your age: ") # 用户输入3
    location = input(prompt + "your location: ") ##用户输入4
    print("The script is called:", script_name) # 打印内容
    print("Your name is:", name)
    print("Your gender is:", gender)
    print("Your age is:", age)
    print("Your location is:",location)
    

    心得:
    ①在提供给参数过后,argv其实是一个非空列表,用两种方法可以得到验证:

    for data in argv: # 以遍历列表的方式打印argv的各元素
          print(data)
    
    print(argv[0]) # 读取列表
    

    ②既然是列表,那么计数也会从0开始。所以脚本的名称总是以ex13.py为sys.argv列表的第一个参数也是比较容易接受和理解的。
    ③在想要执行的python程序当前文件夹,ctrl + 鼠标右键,可以找到当前目录运行命令行的快捷方式。

    1. 习题14 提示和传递
      代码
    from sys import argv # 从sys模块/模组/库中导入argv函数(参数变量)
    script, user_name, favorite_color = argv # 将argv中的数据解包,将所有的参数依次赋予左边的变量
    prompt = "> " # 输入提示
    print("Hi, %s, I'm the %s script.%s is my favorite color." % (user_name, script, favorite_color))
    print("I'd like to ask you a few questions.")
    print("Do you like me %s?" % user_name)
    likes = input(prompt) # python3.6,input
    print("Where do you live %s?" % user_name)
    lives = input(prompt)
    print("What kind of computer do you have?")
    computer =input(prompt)
    print("Do you love %s?" % favorite_color)
    answer = input(prompt + "(Yes/NO)")
    print(
        '''
        Alright, so you said %r about liking me. 
        You live in %r. Not sure where that is.
        And you have a %r computer. Nice.
        Your favorite color is %s, too.
        '''% (likes, lives, computer, favorite_color) # 多参数格式化字符串
    )
    

    加分习题:
    14.3 给你的脚本再添加一个参数,让你的程序用到这个参数。
    已经在上面的代码行添加了favorite_color这个参数。

    1. 习题15 读取文件
      代码
    from sys import argv # 从sys模块/模组/库中导入argv函数
    script, filename = argv # 将argv中的数据解包,将所有的参数依次赋予左边的变量,默认参数script,需要提供filename参数
    txt = open(filename) # open()函数打开文件,返回一个表示文件的对象,并将对象储存在txt变量
    print("Here's your file %r:" % filename)
    print(txt.read()) # read()函数读取存储在txt变量中的文件对象的全部内容,然后打印
    print("Type the filename again:")
    file_again = input("> ") # 提示用户输入文件名
    txt_again = open(file_again) # open()函数打开文件,返回一个表示文件的对象,并将对象储存在txt变量
    print(txt_again.read()) # read()函数读取存储在txt变量中的文件对象的全部内容,然后打印
    

    备注:
    ①被打开文件必须与ex15.py文件在同一个文件夹,否则需在指定文件文件位置。

    1. 习题16 读写文件
    from sys import argv # 从sys模块/模组/库中导入argv函数(参数变量)
    script,filename = argv # 将argv中的数据解包,将所有的参数依次赋予左边的变量
    print("We're going to erase %r." % filename) # 格式化字符串
    print("If you don't want that, hit CTRL-C (^C).")
    print("If you do want that, hit RETURN.")
    input("?")
    print("Opening the file...")
    target = open(filename,'w') # 以读写模式打开文件,并将打开的对象储存在target变量中
    print("Truncating the file. Goodbye!")
    target.truncate() # 清空文件内容
    print("Now I'm going to ask you for three lines.")
    # 输入三行内容
    line1 = input("line 1: ")
    line2 = input("line 2: ")
    line3 = input("line 3: ")
    print("I'm going to write these to the file.")
    target.write(line1) # 将line1内容写入对象target中
    target.write("\n") # 换行输入
    target.write(line2)
    target.write("\n")
    target.write(line3)
    target.write("\n")
    print("And finally, we close it.")
    target.close()  # 关闭文件对象
    

    备注:
    ①代码中用input()代替了raw_input()
    加分习题:
    16.3 文件中重复的地方太多了。试着用一
    个 target.write() 将 line1, line2, line3 打印出来,你可以使用字符串、格式化字符、以及转义字符。

    # 简化代码
    target.write(line1 +"\n" + line2 +"\n" + lin3 +"\n")
    # 或者
    target.write("%s\n%s\n%s" % (line1,line2,line3))
    
    1. 习题17 更多文件操作
      代码:
    from sys import argv # 从sys模组导入argv函数
    from os.path import exists # 从os.path模块导入exists()函数,检查文件路径是否真实存在,即文件是否存在,返回True或False
    script, from_file, to_file = argv # 将argv中的数据解包,将所有的参数依次赋予左边的变量
    print("Coping for %s to %s" % (from_file, to_file)) # 格式化字符串
    # 一行实现: indata = open(from_file).read()
    data_from = open(from_file) # input在python3.6中有重要含义,不宜作变量名,改为data_from.打开文件,并将读取到的对象粗春在data_from
    indata = data_from.read() # 读取文件对象内容,并将内容赋值给indata
    print("The input is %d bytes long" % len(indata)) # 格式化字符串
    print("Does the output file exist? %r" % exists(to_file)) # 格式化字符串 exists(to_file) 返回的是True或者False
    print("Ready, hit RETURM to continue, CRTL-C to abort.")
    # indata_1=input("Please input:\n>") # 如果用raw_input(),会报错,没有传递数据到下面,在python3.6中使用input
    data_to = open(to_file, 'w') # 以写入模式打开to_file,并将对象存储于data_to
    data_to.write(indata) # 将indata写入data_to
    print("Alright, all done.")
    data_to.close() # 关闭文件
    data_from.close() # 关闭文件
    

    备注:
    ①原代码17行raw_input()运行时候出现报错,可能是python版本问题。一开始我换成了input(),但是仔细想想这个程序的功能是将一个文件的内容copy到另外一个文件,文件读取内容已经存到indata,现在只剩下写入的操作,因此我删除了raw_input()这一行代码。
    ②input是python3.6中的重要函数名,因此书中第9~11行代码用input作为变量名称是有误的。
    ③from os.path import exists # 从os.path模块导入exists()函数,检查文件路径是否真实存在,即文件是否存在,返回True或False.这就是为什么第三行输出后面会有一个False.
    加分习题:
    17.2/17.3 简化代码,写成一行。

    from sys import argv # 从sys模组导入argv函数
    from os.path import exists # 从os.path模块导入exists()函数,检查文件路径是否真实存在,即文件是否存在,返回True或False
    script, from_file, to_file = argv # 将argv中的数据解包,将所有的参数依次赋予左边的变量
    open(to_file, 'w').write(open(from_file).read()) # 将indata写入data_to
    

    写成一行。。。导入函数模块要怎么省略
    17.6 找出为什么你需要在代码中写output.close()
    未妥善地关闭文件可能会导致数据丢失或受损。并非在任何情况下都能轻松确定关闭文件的恰当时机。如果在程序中过早地调用close() ,你会发现需要使用文件时它已关闭 (无法访问),这会导致更多的错误。


    Week2 -01 作业 心得体会

    • 在习题13的时候,在命令行中运行python程序这个节点卡了好久。幸亏群里的几位老师的指导,我并没有在这个关口停留太久。这也是我参加爬虫训练营的原因之一:一个人走的很快,一群人走得更远。有人指导,有人讨论,很多知识点都加深了理解。

    相关文章

      网友评论

      本文标题:【Python爬虫】-【第二周】01-作业

      本文链接:https://www.haomeiwen.com/subject/vyshkxtx.html