0

I am trying to make a bot with the help of chatGPT. I know that ChatGPT is not very good at writing code, but I would like to test it to it's limits and see what it can or can't do.

I have found this website that I use for my job. In the website, I type in some numbers and it tells me if this number is active or not. I want the bot to save this in an excel file and print it in the console such that I don't have to check the excel file every time (at least while I'm testing it).

Right now, the bot searches for some keywords on the website. If it finds them, it should print what I want it to print to the console. For example, if it finds the word "No results found" (in Greek) then it should enter that in the excel beside the corresponding number and print "No" in the console. If it doesn't find it, it should print "yes" and put "Active" in the excel file and so on.

However, the code doesn't seem to work. Could it be because of the greek characters? When I run the bot for test purposes, it just prints "yes" even if the key word is not even in there. I have no idea how to solve this problem and any help would be appreciated.

Here's the code:

# Import necessary modules
import openpyxl
import requests
from lxml import html
import webbrowser
import time

# Open the Excel file and get the active worksheet
wb = openpyxl.load_workbook("numbers.xlsx")
sheet = wb.active

# Loop through the rows of the worksheet
for row in sheet.rows:
    # Get the value of the first cell in the row
    number = row[0].value
    
    # Construct the URL using the number
    url = "https://www.vrisko.gr/afm-etairies/" + str(number)
    
    # Open the URL in the default web browser
    webbrowser.open_new_tab(url)
    
    # Send a request to the URL and get the response
    response = requests.get(url)
    
    # Parse the response using lxml
    tree = html.fromstring(response.text)
    
    # Look for the word "Δε βρέθηκαν αποτελέσματα" in the page
    word = tree.xpath('//*[contains(text(), "Δε βρέθηκαν αποτελέσματα")]')
    
    word1 = tree.xpath('//*[contains(text(), "Έχετε υπερβεί το μέγιστο αριθμό αναζητήσεων ΑΦΜ για σήμερα.")]')

    # If the word is found, write the HTML element containing the word to the next cell in the row
    if word:
        sheet.cell(row=row[0].row, column=row[0].column+1).value = "X"
        print("no")
    elif word1:
        print("spam")
    else: 
        # If the word is not found, write an "ΕΝΕΡΓΟ" to the next cell in the row
        sheet.cell(row=row[0].row, column=row[0].column+1).value = "ΕΝΕΡΓΟ"
        print("yes")
    
    # Wait for 1 minute before continuing to the next column
    time.sleep(30)

# Save the changes to the Excel file
wb.save("numbers.xlsx")
The_spider
  • 1,202
  • 1
  • 8
  • 18
Lliakos
  • 13
  • 4
  • If you check the contents of `response.text` you will see that the website returns _You don't have permission to access https://www.vrisko.gr/afm-etairies/ on this server_. This can be solved by setting a user agent, see https://stackoverflow.com/questions/41982475/scraper-in-python-gives-access-denied. – Marijn Jan 13 '23 at 20:09
  • @Marijn You are correct i set a user agent and it worked! Thanks! – Lliakos Jan 14 '23 at 12:47
  • "# Wait for 1 minute before continuing to the next column" - the `time.sleep(30)` call below only waits for half a minute. – The_spider Aug 28 '23 at 12:17

0 Answers0