当前位置:   article > 正文

嘿ChatGPT,来帮我写代码

chatgpt写代码

点击上方“小白学视觉”,选择加"星标"或“置顶

重磅干货,第一时间送达

最近 ChatGPT 发行了,这是由 OpenAI 开发的AI聊天机器人,专门研究对话。它的目标是使AI系统更自然地与之互动,但是在编写代码时也可以为您提供帮助。

您可以让 ChatGPT 做你的编程助理,甚至更多!

在过去的几天里,我一直在玩 ChatGPT,并使用它完成了一些小任务。

任务1:绘制线性回归

如果要绘制线性回归,可以告诉 ChatGPT:

使用 Python 的 matplotlib 库绘制线性回归

84caa438e52c0cefcae066b45156355d.png

我们可以通过告诉ChatGPT应该使用的具体库。

这是 ChatGPT 创建的 Python 代码(包括注释!)

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # Generate some random data
  4. x = np.random.rand(100)
  5. y = x + np.random.normal(0, 0.1, size=100)
  6. # Fit a linear regression to the data
  7. m, b = np.polyfit(x, y, 1)
  8. # Plot the data and the fitted line
  9. plt.plot(x, y, 'o')
  10. plt.plot(x, m*x + b, '-')
  11. plt.show()

以下是输出:

7d455932c1016584a654853060e32b2d.png

这很简单,所以让我们尝试一些更困难的事情。

任务2:与 PyWhatKit 上的 WhatsApp 发送消息

我很想知道 ChatGPT 是否可以帮助我们使用 PyWheKit 等第三方库。该库允许我们使用 WhatsApp 发送消息。

使用 Python和 pywhkit 在 whatsapp 上发送消息

a67669a91ff8fd1ca7bcbe367a9d7409.png

为了本文的目的,我没有告诉 ChatGPT 我的电话号码或我想发送的消息来(虽然我为Task3做了类似的告知)。

这是ChatGPT创建的Python代码。

  1. import pywhatkit
  2. # Set the time in 24-hour format (HH:MM:SS) when you want the message to be sent
  3. time = "HH:MM:SS"
  4. # Set the phone number that you want to send the message to
  5. phone_number = "1234567890"
  6. # Set the message that you want to send
  7. message = "Your message goes here"
  8. # Use the sendwhatmsg method to send the message at the specified time
  9. pywhatkit.sendwhatmsg(time, phone_number, message)

除了ChatGPT生成的代码外,您还必须在浏览器中打开 WhatsApp 会话,并安装 PyWhekit

任务3:使用 Python 发送电子邮件

互联网上有数百个代码段向您展示如何使用 Python 一起发送电子邮件。您也可以使用 ChatGPT 做到:

将来自“ email_1”的电子邮件发送到“ email_2”,并带有“ ChatGPT发送的电子邮件”和“ ChatGPT rocks!”的主题。

31eb11c00f3fce24b1e2f1c896fe0eaa.png

以下是代码:

  1. import smtplib
  2. # create an SMTP object
  3. smtp_obj = smtplib.SMTP('smtp.gmail.com', 587)
  4. # start TLS encryption
  5. smtp_obj.starttls()
  6. # login to the email account
  7. smtp_obj.login('codelessearnmore@gmail.com', 'YOUR_PASSWORD')
  8. # construct the email message
  9. message = "Subject: Email sent by ChatGPT\n\nChatGPT rocks!"
  10. # send the email
  11. smtp_obj.sendmail('codelessearnmore@gmail.com', 'frank@codelessearnmore.com', message)
  12. # close the SMTP connection
  13. smtp_obj.quit()

输入代码后,ChatGPT 告诉我们,我们应该在“ YOUR_PASSWORD”占位符中输入我们的密码。

ChatGPT 这次没有提示我们的(有时确实如此)是,我们必须打开2步验证才能获取16个字符的密码,以防我们使用Gmail

无论如何,ChatGPT 已经做得很棒了。输入密码后,我收到了电子邮件:

f99cd3fc9ca4bf77c7000091e8a55d64.png

任务4:从“Books to Scrape”中爬取数据

我给 ChatGPT 的最具挑战性的任务是爬取一个网站。如您所知,网站具有不同的HTML,因此爬取网站的步骤随着网站的变化而变化。

ChatGPT 从 https://books.toscrape.com/ 网站上爬取书的名称和价格

7fb2070c36967883ec6dfba99c2fd805.png

以下是代码:

  1. import requests
  2. from bs4 import BeautifulSoup
  3. # Make a GET request to the website
  4. response = requests.get("https://books.toscrape.com/")
  5. # Parse the HTML content of the website
  6. soup = BeautifulSoup(response.text, "html.parser")
  7. # Extract the data you want using Beautiful Soup's various methods and attributes
  8. titles = soup.find_all("h3")
  9. prices = soup.find_all(class_="price_color")
  10. # Save the extracted data to a file or database, or use it for some other purpose
  11. with open("book_data.txt", "w") as file:
  12. for title, price in zip(titles, prices):
  13. file.write(f"{title.text}: {price.text}\n")

这是爬取的数据:

0b6de03005158c032d1c7876d68c8066.png

爬取数据而无需自己编写代码……令人惊讶!

附加:使用 Python 生成 Excel 表格

我还尝试使用 ChatGPT 生成 Excel 表格

我无法想出一个好主意来帮助我自动化 Excel 表格,所以我只尝试了一下。

使用 Python 生成 Excel 表格

320e2d9077942735aeaad7dcf50d039c.png

以下是生成的代码:

  1. # Import the `openpyxl` library
  2. import openpyxl
  3. # Create a new Excel workbook
  4. workbook = openpyxl.Workbook()
  5. # Get the active sheet in the workbook (which will be the only sheet at this point)
  6. sheet = workbook.active
  7. # Add some data to the sheet
  8. sheet["A1"] = "Product"
  9. sheet["B1"] = "Sales"
  10. # Add some more data to the sheet
  11. products = ["Product A", "Product B", "Product C"]
  12. sales = [100, 200, 300]
  13. for i in range(len(products)):
  14. sheet.cell(row=i+2, column=1).value = products[i]
  15. sheet.cell(row=i+2, column=2).value = sales[i]
  16. # Save the workbook to a file
  17. workbook.save("sales_report.xlsx")

以下是输出:

4b057a79a1ed83f5a314263e1181cf6f.png

如您所见,我只有一个简单的模板,但我相信您只需要给 ChatGPT 提供正确的命令即可获得令人惊讶的结果。

 
 

好消息!

小白学视觉知识星球

开始面向外开放啦

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/238079
推荐阅读
相关标签