Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

whats wrong with this code ">>> from openpyxl import Workbook import requests URL of the…

  1. User

    whats wrong with this code ">>> from openpyxl import Workbook

    import requests

    URL of the web page

    url = "http://www.tsetmc.com/InstInfo/65883838195688438"

    Send a GET request to the URL

    response = requests.get(url)

    Check if the request was successful

    if response.status_code == 200: ... # Create a new Excel workbook ... workbook = Workbook() ... sheet = workbook.active ... # Write the content of the web page into the Excel sheet sheet['A1'] = response.text File "", line 1 sheet['A1'] = response.text IndentationError: unexpected indent

    # Save the workbook
    workbook.save("web_content.xlsx")
    

    File "", line 1 workbook.save("web_content.xlsx") IndentationError: unexpected indent

    print("Web content copied to Excel successfully.")
    

    File "", line 1 print("Web content copied to Excel successfully.") IndentationError: unexpected indent

    else: File "", line 1 else: ^ SyntaxError: invalid syntax print("Failed to retrieve the web page.") File "", line 1 print("Failed to retrieve the web page.") IndentationError: unexpected indent"

  2. Assistant

    The indentation is incorrect in the code. The lines after the if statement and the else statement should be indented with a consistent number of spaces. There are also empty lines in the code that should be removed. Here's the corrected code:

    from openpyxl import Workbook
    import requests
    
    # URL of the web page
    url = "http://www.tsetmc.com/InstInfo/65883838195688438"
    
    # Send a GET request to the URL
    response = requests.get(url)
    
    # Check if the request was successful
    if response.status_code == 200:
        # Create a new Excel workbook
        workbook = Workbook()
        sheet = workbook.active
    
        # Write the content of the web page into the Excel sheet
        sheet['A1'] = response.text
    
        # Save the workbook
        workbook.save("web_content.xlsx")
    
        print("Web content copied to Excel successfully.")
    else:
        print("Failed to retrieve the web page.")
    

    With the corrected indentation, the code should work without any errors.