I'm trying to extract a character from a website and compare it do a character I have declared in the code.
In my case, I am looping through possible elements in the BeautifulSoup file look for a match to the letter Z when I call .getText(). Whenever I try and compare the characters I get this error message - SyntaxError: invalid syntax.
I'm also confused because when I declare them not in the for loop everything works fine.
Here is my code:
import requests, bs4
res = requests.get('https://www.example.com')
try:
res.raise_for_status()
except Exception as exc:
print('There was a problem: %s' % (exc))
noStarchSoup = bs4.BeautifulSoup(res.text)
ZElems = noStarchSoup.select('li')
TElems = noStarchSoup.select('a')
for x in range (0, 25):
char1 = ZElems[x].getText()
char2 = 'Z'
if char1 == char2
url = TElems[x].get('href')
print(url)
Here is the output I get:
if char1 == char2
^
SyntaxError: invalid syntax
If I declare char1 and char2 outside of the for loop and have char1 be at position 0 in the array, the comparison seems to work fine. Position 0 corresponds to 'A'.
>>> import requests, bs4
>>>
>>> res = requests.get('https://www.example.com')
>>> noStarchSoup = bs4.BeautifulSoup(res.text)
>>> ZElems = noStarchSoup.select('li')
>>> TElems = noStarchSoup.select('a')
>>> char1 = ZElems[0].getText()
>>> char2 = 'A'
>>> char1 == char2
True
Is there something I'm missing about comparisons on characters in Python?
Aucun commentaire:
Enregistrer un commentaire