用AI写代码 -- Github Copilot测试

在这里插入图片描述
截图为copilot官网

要使用copilot,首先要安装vscode。下载copilot插件。下载完了以后,并不能马上使用。还需要申请内测账号,前几天,我终于收到内测账号通过的邮件。趁着今天在家,连忙打开vscode测试一下。

爬图片

我们的第一个实验是爬csdn的logo。

于是,我新建了一个文件,我开始写注释。当我写道retri的时候,copilot已经知道我要说什么了,直接把一句话补全了。牛!

在这里插入图片描述
我于是打了一下tab,这句话就补全了。

接着,我打了import,copilot已经开始建议我导入urllib了。牛!

在这里插入图片描述

打完def三个字,整个函数的建议都有了。牛!

在这里插入图片描述
接着,打if:
在这里插入图片描述
于是,满怀期待的,运行了程序,结果,悲剧了:

AttributeError: module ‘urllib’ has no attribute ‘urlopen’

我还是比较熟悉requests模块,于是,我把第一句代码改成了import requests,重新生成代码。如下:

# find logo in csdn.net
# retrieve the logo and save it in the same directory

import requests
from bs4 import BeautifulSoup
import os

def get_logo(url):
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')
    logo = soup.find('link', rel='shortcut icon')
    if logo:
        logo_url = logo['href']
        r = requests.get(logo_url)
        if r.status_code == 200:
            with open(os.path.basename(logo_url), 'wb') as f:
                f.write(r.content)
                print('logo saved')
        else:
            print('logo not found')
    else:
        print('logo not found')
        

if __name__ == "__main__":
    get_logo('http://www.csdn.net')

运行完了,发现不是logo,是favicon。😦

算了,算了,Copilot找不到logo,还是我自己找吧。

于是,我把注释改成了

# download and save https://img-home.csdnimg.cn/images/20201124032511.png
# to the same folder

生成的代码如下:

# download and save https://img-home.csdnimg.cn/images/20201124032511.png
# to the same folder

import requests
from bs4 import BeautifulSoup
import os

def download():
    url = 'https://img-home.csdnimg.cn/images/20201124032511.png'
    root = './img'
    path = root + url.split('/')[-1]
    try:
        if not os.path.exists(root):
            os.mkdir(root)
        if not os.path.exists(path)
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

织网者Eric

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值