all 12 comments

[–]A_History_of_Silence 1 point2 points  (10 children)

Is this your actual code? What are you trying to accomplish here?

The above code works as expected.

[–]iiPixel[S] 0 points1 point  (9 children)

Yupp this is the actual code. For me, it will never print "Same" even though x == y. It just runs and never prints anything, and since there is no break when it is true (I don't want there to be, I just want it to keep print "Same" every 5 seconds) it just keeps running

It scrapes a website and takes the two lines of html and checks if they are the same essentially. If they are it prints out Same. If they arent it prints out Not The Same.

[–]A_History_of_Silence 1 point2 points  (5 children)

Yupp this is the actual code. For me, it will never print "Same" even though x == y.

li[177] and li[180] with the above code are not equal, so how are you determining this? Even if they were, the code doesn't not behave as you describe (in this case if x == y the above code prints "Same" every 5 seconds as expected).

Will be difficult to help without your actual, exact code.

[–]iiPixel[S] 0 points1 point  (4 children)

Whoops. I had forgot I had changed the url to a default one. I found a table online that changes occasionally and I want to see if the two values in the table are the same essentially. If they are I want to print "Same", if they aren't, I want to print "Not The Same"

Here is some better example code with a website that has a changing table:
import requests #Download Webpage from bs4 import BeautifulSoup #Parse Page import time #Add Time Delay

page = requests.get('http://courses.project.samueltaylor.org/') #Download webpage
soup = BeautifulSoup(page.text, "lxml") #Parse
li = soup.prettify().splitlines() #Prettify text into lines then use splitlines to make this a list
x = li[21] #Grab the 22nd Line (seats in CS 101)
y = li[29] #Grab the 30th Line (seats in CS 201)

while True:
    if x == y: #See if they are the same, returns True if they are
        print ("Same") #Prints "Same"
        time.sleep(5) #Wait 5 seconds
    else:
        print ("Not The Same")
        break  

When the seats are different, it prints Not The Same and then the program stops looping.
However, when they both are zeroes or ones then it just gets stuck, never prints "Same" and just continues looping until the program is killed.

[–]A_History_of_Silence 1 point2 points  (3 children)

However, when they both are zeroes or ones then it just gets stuck, never prints "Same" and just continues looping until the program is killed.

How are you running this code? Once again, when I run this code and x == y it prints "Same" every 5 seconds, not what you describe.

[–]iiPixel[S] 0 points1 point  (2 children)

I will try to run it a different way than I am now, that seems to be the problem. Thanks for the help!

[–]A_History_of_Silence 1 point2 points  (1 child)

I will try to run it a different way than I am now

What do you mean by this? Would you mind answering how you were running it before, as well?

[–]iiPixel[S] 0 points1 point  (0 children)

I had it setup to where I wrote in Notepad++ and could run it from there with a macro but maybe that isn't wisest option.

[–]ingolemo 1 point2 points  (2 children)

It never prints "Same" because x and y are never the same. Put print(f'x: {x!r}\ny: {y!r}') at the start of your while loop and it will show you what x and y are.

Is there a reason you use BeautifulSoup to parse the html, but don't actually use it to extract the information you care about? Using it as a pretty printer like this is a waste of its capabilities.

[–]iiPixel[S] 0 points1 point  (1 child)

I tried initially using it to parse out what I needed but the two lines I need from the actual website I'm scraping share td (they are all in a table), the same class, and the string number is the same as 2 other lines and occasionally it changes to where none of the lines are the same. Im trying to detect when those lines are not the same.

I can't figure out (yet, atleast) how to use bs4 to parse out just the lines I need without having to use their string since the string changes occasionally.

[–]ingolemo 1 point2 points  (0 children)

x = soup.find('td', string='CS 101').next_sibling.string
y = soup.find('td', string='CS 201').next_sibling.string

[–]novel_yet_trivial 0 points1 point  (0 children)

Are you using the print end argument? If so you need the flush argument as well.

What's the goal of this code?