赞
踩
点击上方“小白学视觉”,选择加"星标"或“置顶”
重磅干货,第一时间送达
最近 ChatGPT 发行了,这是由 OpenAI 开发的AI聊天机器人,专门研究对话。它的目标是使AI系统更自然地与之互动,但是在编写代码时也可以为您提供帮助。
您可以让 ChatGPT 做你的编程助理,甚至更多!
在过去的几天里,我一直在玩 ChatGPT,并使用它完成了一些小任务。
任务1:绘制线性回归
如果要绘制线性回归,可以告诉 ChatGPT:
使用 Python 的 matplotlib 库绘制线性回归
我们可以通过告诉ChatGPT应该使用的具体库。
这是 ChatGPT 创建的 Python 代码(包括注释!)
- import matplotlib.pyplot as plt
- import numpy as np
-
-
- # Generate some random data
- x = np.random.rand(100)
- y = x + np.random.normal(0, 0.1, size=100)
-
-
- # Fit a linear regression to the data
- m, b = np.polyfit(x, y, 1)
-
-
- # Plot the data and the fitted line
- plt.plot(x, y, 'o')
- plt.plot(x, m*x + b, '-')
- plt.show()

以下是输出:
这很简单,所以让我们尝试一些更困难的事情。
任务2:与 PyWhatKit 上的 WhatsApp 发送消息
我很想知道 ChatGPT 是否可以帮助我们使用 PyWheKit 等第三方库。该库允许我们使用 WhatsApp 发送消息。
使用 Python和 pywhkit 在 whatsapp 上发送消息
为了本文的目的,我没有告诉 ChatGPT 我的电话号码或我想发送的消息来(虽然我为Task3做了类似的告知)。
这是ChatGPT创建的Python代码。
- import pywhatkit
-
-
- # Set the time in 24-hour format (HH:MM:SS) when you want the message to be sent
- time = "HH:MM:SS"
- # Set the phone number that you want to send the message to
- phone_number = "1234567890"
- # Set the message that you want to send
- message = "Your message goes here"
- # Use the sendwhatmsg method to send the message at the specified time
- pywhatkit.sendwhatmsg(time, phone_number, message)
除了ChatGPT生成的代码外,您还必须在浏览器中打开 WhatsApp 会话,并安装 PyWhekit
任务3:使用 Python 发送电子邮件
互联网上有数百个代码段向您展示如何使用 Python 一起发送电子邮件。您也可以使用 ChatGPT 做到:
将来自“ email_1”的电子邮件发送到“ email_2”,并带有“ ChatGPT发送的电子邮件”和“ ChatGPT rocks!”的主题。
以下是代码:
import smtplib # create an SMTP object smtp_obj = smtplib.SMTP('smtp.gmail.com', 587) # start TLS encryption smtp_obj.starttls() # login to the email account smtp_obj.login('codelessearnmore@gmail.com', 'YOUR_PASSWORD') # construct the email message message = "Subject: Email sent by ChatGPT\n\nChatGPT rocks!" # send the email smtp_obj.sendmail('codelessearnmore@gmail.com', 'frank@codelessearnmore.com', message) # close the SMTP connection smtp_obj.quit()
输入代码后,ChatGPT 告诉我们,我们应该在“ YOUR_PASSWORD”占位符中输入我们的密码。
ChatGPT 这次没有提示我们的(有时确实如此)是,我们必须打开2步验证才能获取16个字符的密码,以防我们使用Gmail
无论如何,ChatGPT 已经做得很棒了。输入密码后,我收到了电子邮件:
任务4:从“Books to Scrape”中爬取数据
我给 ChatGPT 的最具挑战性的任务是爬取一个网站。如您所知,网站具有不同的HTML,因此爬取网站的步骤随着网站的变化而变化。
ChatGPT 从 https://books.toscrape.com/ 网站上爬取书的名称和价格
以下是代码:
- import requests
- from bs4 import BeautifulSoup
-
-
- # Make a GET request to the website
- response = requests.get("https://books.toscrape.com/")
-
-
- # Parse the HTML content of the website
- soup = BeautifulSoup(response.text, "html.parser")
-
-
- # Extract the data you want using Beautiful Soup's various methods and attributes
- titles = soup.find_all("h3")
- prices = soup.find_all(class_="price_color")
-
-
- # Save the extracted data to a file or database, or use it for some other purpose
- with open("book_data.txt", "w") as file:
- for title, price in zip(titles, prices):
- file.write(f"{title.text}: {price.text}\n")

这是爬取的数据:
爬取数据而无需自己编写代码……令人惊讶!
附加:使用 Python 生成 Excel 表格
我还尝试使用 ChatGPT 生成 Excel 表格
我无法想出一个好主意来帮助我自动化 Excel 表格,所以我只尝试了一下。
使用 Python 生成 Excel 表格
以下是生成的代码:
# Import the `openpyxl` library import openpyxl # Create a new Excel workbook workbook = openpyxl.Workbook() # Get the active sheet in the workbook (which will be the only sheet at this point) sheet = workbook.active # Add some data to the sheet sheet["A1"] = "Product" sheet["B1"] = "Sales" # Add some more data to the sheet products = ["Product A", "Product B", "Product C"] sales = [100, 200, 300] for i in range(len(products)): sheet.cell(row=i+2, column=1).value = products[i] sheet.cell(row=i+2, column=2).value = sales[i] # Save the workbook to a file workbook.save("sales_report.xlsx")
以下是输出:
如您所见,我只有一个简单的模板,但我相信您只需要给 ChatGPT 提供正确的命令即可获得令人惊讶的结果。
好消息!
小白学视觉知识星球
开始面向外开放啦
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。