美文网首页
ChatGPT可以帮忙自动生成python代码

ChatGPT可以帮忙自动生成python代码

作者: 追梦人在路上不断追寻 | 来源:发表于2023-01-05 21:53 被阅读0次

    生成图表

    ChatGPT可以帮助我们轻松返回生成图标的python代码。

    image.png
    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()
    

    发送信息

    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也可以发送给我们发送邮件的python代码。

    python_send_email-1024x512.png
    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也可以帮我们生成抓取网页信息的代码。

    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")
    

    导出数据

    同样,chatgpt也可以帮我们生成从excel导出数据的代码。

    image.png
    # 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可以帮忙自动生成python代码

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